The Simpsons as a Chart

[This article was first published on You Know, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Inspired by this clever image, I thought I would whip it up in R.
Results:
Below is the R code:
1:  # Prepare -----------------------------------------------------------------  
2:  rm(list=ls());gc()  
3:  pkg <- c("ggplot2")  
4:  inst <- pkg %in% installed.packages()  
5:  if(length(pkg[!inst]) > 0) install.packages(pkg[!inst])  
6:  lapply(pkg,library,character.only=TRUE)  
7:  rm(inst,pkg)  
8:  # Create dataset ----------------------------------------------------------  
9:  d1 <- data.frame(member=c(rep("Homer",3),  
10:               rep("Marge",3),  
11:               rep("Bart",3),  
12:               rep("Lisa",2),  
13:               rep("Maggie",2)),  
14:           shade=c("HomerPants","HomerShirt","Skin",  
15:               "MargeDress","Skin","MargeHair",  
16:               "BartShorts","BartShirt","Skin",  
17:               "LisaDress","Skin",  
18:               "MaggieOnesie","Skin"),  
19:           height=c(20,20,25,  
20:               40,20,40,  
21:               15,15,18,  
22:               28,15,  
23:               18,11))  
24:  d1$member <- ordered(d1$member,levels=c("Homer","Marge","Bart","Lisa","Maggie"))  
25:  d1$shade <- ordered(d1$shade,levels=c("HomerPants","HomerShirt","Skin",  
26:                     "MargeDress","MargeHair",  
27:                     "BartShorts","BartShirt",  
28:                     "LisaDress",  
29:                     "MaggieOnesie"))  
30:  # Chart the data ----------------------------------------------------------  
31:  g1 <- ggplot(d1,aes(x=member,y=height,fill=shade)) +   
32:   geom_bar(stat="identity") +   
33:   scale_fill_manual(values=c("#3333FF","#FFFFFF","#FFFF33",  
34:                 "#66CC33","#000099",  
35:                 "#6633CC","#CC0000",  
36:                 "#FF6600",  
37:                 "#0099CC")) +   
38:   theme(legend.position="none",  
39:      axis.title.x=element_blank(),  
40:      axis.title.y=element_blank(),  
41:      axis.text.x=element_blank(),  
42:      axis.text.y=element_blank()) +   
43:   ggtitle("Moe's Bar Chart")  
44:  g1  
45:  # Save image --------------------------------------------------------------  
46:  png("Simpsons.png")  
47:  g1  
48:  dev.off()  

To leave a comment for the author, please follow the link and comment on their blog: You Know.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)