├── README.md ├── buzzfeed_linkbait_headlines.csv ├── ggplot_tutorial_1.R ├── tutorial_1.png ├── tutorial_2.png ├── tutorial_3.png ├── tutorial_4.png ├── tutorial_5.png ├── tutorial_6.png ├── tutorial_7.png └── tutorial_8.png /README.md: -------------------------------------------------------------------------------- 1 | # ggplot-tutorial 2 | Repository containing files and image output for my ggplot2 tutorial: [An Introduction on How to Make Beautiful Charts With R and ggplot2](http://minimaxir.com/2015/02/ggplot-tutorial/) 3 | -------------------------------------------------------------------------------- /ggplot_tutorial_1.R: -------------------------------------------------------------------------------- 1 | install.packages(c("ggplot2","RColorBrewer","scales")) 2 | library(ggplot2); library(scales); library(grid); library(RColorBrewer) 3 | 4 | # Theme created at start of file for orgazinational purposes 5 | 6 | fte_theme <- function() { 7 | 8 | # Generate the colors for the chart procedurally with RColorBrewer 9 | palette <- brewer.pal("Greys", n=9) 10 | color.background = palette[2] 11 | color.grid.major = palette[3] 12 | color.axis.text = palette[6] 13 | color.axis.title = palette[7] 14 | color.title = palette[9] 15 | 16 | # Begin construction of chart 17 | theme_bw(base_size=9) + 18 | 19 | # Set the entire chart region to a light gray color 20 | theme(panel.background=element_rect(fill=color.background, color=color.background)) + 21 | theme(plot.background=element_rect(fill=color.background, color=color.background)) + 22 | theme(panel.border=element_rect(color=color.background)) + 23 | 24 | # Format the grid 25 | theme(panel.grid.major=element_line(color=color.grid.major,size=.25)) + 26 | theme(panel.grid.minor=element_blank()) + 27 | theme(axis.ticks=element_blank()) + 28 | 29 | # Format the legend, but hide by default 30 | theme(legend.position="none") + 31 | theme(legend.background = element_rect(fill=color.background)) + 32 | theme(legend.text = element_text(size=7,color=color.axis.title)) + 33 | 34 | # Set title and axis labels, and format these and tick marks 35 | theme(plot.title=element_text(color=color.title, size=10, vjust=1.25)) + 36 | theme(axis.text.x=element_text(size=7,color=color.axis.text)) + 37 | theme(axis.text.y=element_text(size=7,color=color.axis.text)) + 38 | theme(axis.title.x=element_text(size=8,color=color.axis.title, vjust=0)) + 39 | theme(axis.title.y=element_text(size=8,color=color.axis.title, vjust=1.25)) + 40 | 41 | # Plot margins 42 | theme(plot.margin = unit(c(0.35, 0.2, 0.3, 0.35), "cm")) 43 | } 44 | 45 | df <- read.csv("buzzfeed_linkbait_headlines.csv", header=T) 46 | 47 | ### 48 | ### Chart 1-1: Histogram of Listicle sizes 49 | ### 50 | 51 | ggplot(df, aes(listicle_size)) + 52 | geom_histogram(binwidth=1) 53 | 54 | ggsave("tutorial_1.png", dpi=300, width=4, height=3) 55 | 56 | ### 57 | ### Chart 1-2: + Theme 58 | ### 59 | 60 | ggplot(df, aes(listicle_size)) + 61 | geom_histogram(binwidth=1) + 62 | fte_theme() 63 | 64 | ggsave("tutorial_2.png", dpi=300, width=4, height=3) 65 | 66 | ### 67 | ### Chart 1-3: + Axis Labels 68 | ### 69 | 70 | ggplot(df, aes(listicle_size)) + 71 | geom_histogram(binwidth=1) + 72 | fte_theme() + 73 | labs(title="Distribution of Listicle Sizes for BuzzFeed Listicles", x="# of Entries in Listicle", y="# of Listicles") 74 | 75 | ggsave("tutorial_3.png", dpi=300, width=4, height=3) 76 | 77 | ### 78 | ### Chart 1-4: + Final Tweaks 79 | ### 80 | 81 | ggplot(df, aes(listicle_size)) + 82 | geom_histogram(binwidth=1, fill="#c0392b", alpha=0.75) + 83 | fte_theme() + 84 | labs(title="Distribution of Listicle Sizes for BuzzFeed Listicles", x="# of Entries in Listicle", y="# of Listicles") + 85 | scale_x_continuous(breaks=seq(0,50, by=5)) + 86 | scale_y_continuous(labels=comma) + 87 | geom_hline(yintercept=0, size=0.4, color="black") 88 | 89 | ggsave("tutorial_4.png", dpi=300, width=4, height=3) 90 | 91 | 92 | ### 93 | ### Chart 2-1: Scatterplot of Shares vs. Likes 94 | ### 95 | 96 | ggplot(df, aes(x=listicle_size, y=num_fb_shares)) + 97 | geom_point() 98 | 99 | ggsave("tutorial_5.png", dpi=300, width=4, height=3) 100 | 101 | ### 102 | ### Chart 2-2: Transparency + Log 103 | ### 104 | 105 | ggplot(df, aes(x=listicle_size, y=num_fb_shares)) + 106 | geom_point(alpha=0.05) + 107 | scale_y_log10(labels=comma) 108 | 109 | ggsave("tutorial_6.png", dpi=300, width=4, height=3) 110 | 111 | ### 112 | ### Chart 2-3: Theme + Axis 113 | ### 114 | 115 | ggplot(df, aes(x=listicle_size, y=num_fb_shares)) + 116 | geom_point(alpha=0.05) + 117 | scale_y_log10(labels=comma) + 118 | fte_theme() + 119 | labs(x="# of Entries in Listicle", y="# of Facebook Shares", title="FB Shares vs. Listicle Size for BuzzFeed Listicles") 120 | 121 | ggsave("tutorial_7.png", dpi=300, width=4, height=3) 122 | 123 | ### 124 | ### Chart 2-4: Tidy Up 125 | ### 126 | 127 | ggplot(df, aes(x=listicle_size, y=num_fb_shares)) + 128 | geom_point(alpha=0.05, color="#c0392b") + 129 | scale_x_continuous(breaks=seq(0,50, by=5)) + 130 | scale_y_log10(labels=comma, breaks=10^(0:6)) + 131 | geom_hline(yintercept=1, size=0.4, color="black") + 132 | geom_smooth(alpha=0.25, color="black", fill="black") + 133 | fte_theme() + 134 | labs(x="# of Entries in Listicle", y="# of Facebook Shares", title="FB Shares vs. Listicle Size for BuzzFeed Listicles") 135 | 136 | ggsave("tutorial_8.png", dpi=300, width=4, height=3) -------------------------------------------------------------------------------- /tutorial_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/minimaxir/ggplot-tutorial/9206886de98ca00ecd55bb3cfc57c503f92da6f8/tutorial_1.png -------------------------------------------------------------------------------- /tutorial_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/minimaxir/ggplot-tutorial/9206886de98ca00ecd55bb3cfc57c503f92da6f8/tutorial_2.png -------------------------------------------------------------------------------- /tutorial_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/minimaxir/ggplot-tutorial/9206886de98ca00ecd55bb3cfc57c503f92da6f8/tutorial_3.png -------------------------------------------------------------------------------- /tutorial_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/minimaxir/ggplot-tutorial/9206886de98ca00ecd55bb3cfc57c503f92da6f8/tutorial_4.png -------------------------------------------------------------------------------- /tutorial_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/minimaxir/ggplot-tutorial/9206886de98ca00ecd55bb3cfc57c503f92da6f8/tutorial_5.png -------------------------------------------------------------------------------- /tutorial_6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/minimaxir/ggplot-tutorial/9206886de98ca00ecd55bb3cfc57c503f92da6f8/tutorial_6.png -------------------------------------------------------------------------------- /tutorial_7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/minimaxir/ggplot-tutorial/9206886de98ca00ecd55bb3cfc57c503f92da6f8/tutorial_7.png -------------------------------------------------------------------------------- /tutorial_8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/minimaxir/ggplot-tutorial/9206886de98ca00ecd55bb3cfc57c503f92da6f8/tutorial_8.png --------------------------------------------------------------------------------