├── .gitignore ├── add_toc.sh ├── cheatsheet.R ├── cheatsheet.Rmd ├── figure ├── unnamed-chunk-10.png ├── unnamed-chunk-11.png ├── unnamed-chunk-121.png ├── unnamed-chunk-122.png ├── unnamed-chunk-123.png ├── unnamed-chunk-13.png ├── unnamed-chunk-14.png ├── unnamed-chunk-16.png ├── unnamed-chunk-17.png ├── unnamed-chunk-18.png ├── unnamed-chunk-20.png ├── unnamed-chunk-21.png ├── unnamed-chunk-22.png ├── unnamed-chunk-23.png ├── unnamed-chunk-24.png ├── unnamed-chunk-25.png ├── unnamed-chunk-26.png ├── unnamed-chunk-27.png ├── unnamed-chunk-28.png ├── unnamed-chunk-29.png ├── unnamed-chunk-291.png ├── unnamed-chunk-292.png ├── unnamed-chunk-293.png ├── unnamed-chunk-30.png ├── unnamed-chunk-301.png ├── unnamed-chunk-302.png ├── unnamed-chunk-31.png ├── unnamed-chunk-311.png ├── unnamed-chunk-312.png ├── unnamed-chunk-32.png ├── unnamed-chunk-321.png ├── unnamed-chunk-322.png ├── unnamed-chunk-33.png ├── unnamed-chunk-331.png ├── unnamed-chunk-332.png ├── unnamed-chunk-333.png ├── unnamed-chunk-341.png ├── unnamed-chunk-342.png ├── unnamed-chunk-343.png ├── unnamed-chunk-351.png ├── unnamed-chunk-352.png ├── unnamed-chunk-353.png ├── unnamed-chunk-354.png ├── unnamed-chunk-355.png ├── unnamed-chunk-356.png ├── unnamed-chunk-36.png ├── unnamed-chunk-371.png ├── unnamed-chunk-372.png ├── unnamed-chunk-381.png ├── unnamed-chunk-382.png ├── unnamed-chunk-383.png ├── unnamed-chunk-384.png ├── unnamed-chunk-385.png ├── unnamed-chunk-391.png ├── unnamed-chunk-392.png ├── unnamed-chunk-393.png ├── unnamed-chunk-4.png ├── unnamed-chunk-401.png ├── unnamed-chunk-402.png ├── unnamed-chunk-41.png ├── unnamed-chunk-5.png ├── unnamed-chunk-6.png ├── unnamed-chunk-7.png ├── unnamed-chunk-8.png └── unnamed-chunk-9.png └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | \#*# 2 | .#* -------------------------------------------------------------------------------- /add_toc.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | doctoc cheatsheet[weaved].md 4 | -------------------------------------------------------------------------------- /cheatsheet.R: -------------------------------------------------------------------------------- 1 | 2 | ## ------------------------------------------------------------------------ 3 | opts_chunk$set(warning=FALSE, message=FALSE, fig.width=8, fig.height=4) 4 | 5 | 6 | ## ------------------------------------------------------------------------ 7 | library(ggplot2) 8 | 9 | 10 | ## ------------------------------------------------------------------------ 11 | set.seed(1410) # make the sample reproducible 12 | head(diamonds) 13 | dsmall <- diamonds[sample(nrow(diamonds), 100), ] 14 | 15 | 16 | ## ------------------------------------------------------------------------ 17 | qplot(carat, price, data=diamonds) 18 | 19 | 20 | ## ------------------------------------------------------------------------ 21 | qplot(carat, price, data=dsmall, color=color, shape=cut, alpha=I(1/2)) 22 | 23 | 24 | ## ------------------------------------------------------------------------ 25 | qplot(carat, price, data=dsmall, geom=c("point", "smooth")) 26 | 27 | 28 | ## ------------------------------------------------------------------------ 29 | qplot(carat, price, data=dsmall, geom=c("point", "smooth"), method="lm") 30 | 31 | 32 | ## ------------------------------------------------------------------------ 33 | qplot(color, price/carat, data=diamonds, geom="jitter") 34 | 35 | 36 | ## ------------------------------------------------------------------------ 37 | qplot(color, price/carat, data=diamonds, geom="boxplot") 38 | 39 | 40 | ## ------------------------------------------------------------------------ 41 | qplot(carat, data=diamonds, geom="histogram", fill=color) 42 | 43 | 44 | ## ------------------------------------------------------------------------ 45 | qplot(carat, data=diamonds, geom="density", color=color) 46 | 47 | 48 | ## ------------------------------------------------------------------------ 49 | qplot(carat, data=diamonds, geom="histogram", binwidth=1) 50 | qplot(carat, data=diamonds, geom="histogram", binwidth=0.1) 51 | qplot(carat, data=diamonds, geom="histogram", binwidth=0.01) 52 | 53 | 54 | ## ------------------------------------------------------------------------ 55 | qplot(color, data=diamonds, geom="bar") 56 | 57 | 58 | ## ------------------------------------------------------------------------ 59 | # bar plot of diamond color weighted by carat 60 | qplot(color, data=diamonds, geom="bar", weight=carat) + 61 | scale_y_continuous("carat") 62 | 63 | 64 | ## ------------------------------------------------------------------------ 65 | head(economics) 66 | 67 | 68 | ## ------------------------------------------------------------------------ 69 | qplot(date, unemploy/pop, data=economics, geom="line") 70 | 71 | 72 | ## ------------------------------------------------------------------------ 73 | qplot(carat, data=diamonds, facets=color~., 74 | geom="histogram", binwidth=0.1, xlim=c(0,3)) 75 | 76 | 77 | ## ------------------------------------------------------------------------ 78 | qplot(carat, price, data=dsmall, 79 | xlab="Price ($)", 80 | ylab="Weight (carats)", 81 | main="Price-weight relationship") 82 | 83 | 84 | ## ------------------------------------------------------------------------ 85 | df <- data.frame(x=c(3, 1, 5), y=c(2, 4, 6), label=c("a", "b", "c")) 86 | p <- ggplot(df, aes(x, y, label=label)) + xlab(NULL) + ylab(NULL) 87 | 88 | 89 | ## ------------------------------------------------------------------------ 90 | p + geom_point() + ggtitle("geom_point") 91 | 92 | 93 | ## ------------------------------------------------------------------------ 94 | p + geom_bar(stat="identity") + ggtitle("geom_bar(stat=\"identity\")") 95 | 96 | 97 | ## ------------------------------------------------------------------------ 98 | p + geom_line() + ggtitle("geom_line") 99 | 100 | 101 | ## ------------------------------------------------------------------------ 102 | p + geom_area() + ggtitle("geom_area") 103 | 104 | 105 | ## ------------------------------------------------------------------------ 106 | p + geom_path() + ggtitle("geom_path") 107 | 108 | 109 | ## ------------------------------------------------------------------------ 110 | p + geom_text() + ggtitle("geom_text") 111 | 112 | 113 | ## ------------------------------------------------------------------------ 114 | p + geom_tile() + ggtitle("geom_tile") 115 | 116 | 117 | ## ------------------------------------------------------------------------ 118 | p + geom_polygon() + ggtitle("geom_polygon") 119 | 120 | 121 | ## ------------------------------------------------------------------------ 122 | depth_dist <- ggplot(diamonds, aes(depth)) + xlim(58, 68) 123 | depth_dist + geom_histogram() 124 | 125 | 126 | ## ------------------------------------------------------------------------ 127 | depth_dist + geom_histogram(aes(y = ..density..), binwidth=0.1) + 128 | facet_grid(cut ~ .) 129 | depth_dist + geom_histogram(aes(fill=cut), binwidth=0.1, position="fill") 130 | depth_dist + geom_freqpoly(aes(y = ..density.., color=cut), binwidth=0.1) 131 | 132 | 133 | ## ------------------------------------------------------------------------ 134 | qplot(cut, depth, data=diamonds, geom="boxplot") 135 | library(plyr) 136 | qplot(carat, depth, data=diamonds, geom="boxplot", 137 | group = round_any(carat, 0.1, floor), xlim=c(0, 3)) 138 | 139 | 140 | ## ------------------------------------------------------------------------ 141 | qplot(class, cty, data=mpg, geom="jitter") 142 | qplot(class, drv, data=mpg, geom="jitter") 143 | 144 | 145 | ## ------------------------------------------------------------------------ 146 | qplot(depth, data=diamonds, geom="density", xlim=c(54, 70)) 147 | qplot(depth, data=diamonds, geom="density", xlim=c(54, 70), fill=cut, alpha=I(0.2)) 148 | 149 | 150 | ## ------------------------------------------------------------------------ 151 | df <- data.frame(x=rnorm(2000), y=rnorm(2000)) 152 | norm <- ggplot(df, aes(x, y)) 153 | norm + geom_point() 154 | norm + geom_point(shape=1) 155 | norm + geom_point(shape = ".") # pixel-sized 156 | 157 | 158 | ## ------------------------------------------------------------------------ 159 | library(scales) 160 | norm + geom_point(color=alpha("black", 1/3)) 161 | norm + geom_point(color=alpha("black", 1/5)) 162 | norm + geom_point(color=alpha("black", 1/10)) 163 | 164 | 165 | ## ------------------------------------------------------------------------ 166 | td <- ggplot(diamonds, aes(table, depth)) + xlim(50, 70) + ylim(50, 70) 167 | td + geom_point() 168 | td + geom_jitter() 169 | jit <- position_jitter(width=0.5) 170 | td + geom_jitter(position=jit) 171 | td + geom_jitter(position=jit, color=alpha("black", 1/10)) 172 | td + geom_jitter(position=jit, color=alpha("black", 1/50)) 173 | td + geom_jitter(position=jit, color=alpha("black", 1/200)) 174 | 175 | 176 | ## ------------------------------------------------------------------------ 177 | library(maps) 178 | data(us.cities) 179 | big_cities <- subset(us.cities, pop>500000) 180 | qplot(long, lat, data=big_cities) + borders("state", size=0.5) 181 | 182 | 183 | ## ------------------------------------------------------------------------ 184 | states <- map_data("state") 185 | arrests <- USArrests 186 | names(arrests) <- tolower(names(arrests)) 187 | arrests$region <- tolower(rownames(USArrests)) 188 | 189 | choro <- merge(states, arrests, by="region") 190 | # reorder the rows because order matters when drawing polygons and merge 191 | # destroys the original ordering 192 | choro <- choro[order(choro$order), ] 193 | qplot(long, lat, data=choro, group=group, fill=assault, geom="polygon") 194 | qplot(long, lat, data=choro, group=group, fill=assault/murder, geom="polygon") 195 | 196 | 197 | ## ------------------------------------------------------------------------ 198 | unemp <- qplot(date, unemploy, data=economics, geom="line", 199 | xlab="", ylab="No. unemployed (1000s)") 200 | 201 | presidential <- presidential[-(1:3), ] 202 | 203 | yrng <- range(economics$unemploy) 204 | xrng <- range(economics$date) 205 | unemp + geom_vline(aes(xintercept=as.numeric(start)), data=presidential) 206 | unemp + geom_rect(aes(NULL, NULL, xmin=start, xmax=end, fill=party), 207 | ymin=yrng[1], ymax=yrng[2], data=presidential) + 208 | scale_fill_manual(values=alpha(c("blue", "red"), 0.2)) 209 | 210 | last_plot() + geom_text(aes(x=start, y=yrng[1], label=name), 211 | data=presidential, size=3, hjust=0, vjust=0) 212 | 213 | caption <- paste(strwrap("Unemployment rates in the US have varied 214 | alot over the years", 40), collapse="\n") 215 | unemp + geom_text(aes(x, y, label=caption), 216 | data=data.frame(x=xrng[2], y=yrng[2]), 217 | hjust=1, vjust=1, size=4) 218 | 219 | highest <- subset(economics, unemploy==max(unemploy)) 220 | unemp + geom_point(data=highest, size=3, color=alpha("red", 0.3)) 221 | 222 | 223 | ## ------------------------------------------------------------------------ 224 | qplot(cty, hwy, data=mpg) + facet_grid(. ~ cyl) 225 | qplot(cty, data=mpg, geom="histogram", binwidth=2) + facet_grid(cyl ~ .) 226 | qplot(cty, hwy, data=mpg) + facet_grid(drv ~ cyl) 227 | 228 | 229 | ## ------------------------------------------------------------------------ 230 | p <- qplot(displ, hwy, data=mpg) + geom_smooth(method="lm", se=F) 231 | p + facet_grid(cyl ~ drv) 232 | p + facet_grid(cyl ~ drv, margins=T) 233 | 234 | 235 | ## ------------------------------------------------------------------------ 236 | library(plyr) 237 | movies$decade <- round_any(movies$year, 10, floor) 238 | qplot(rating, ..density.., data=subset(movies, decade > 1890), 239 | geom="histogram", binwidth=0.5) + 240 | facet_wrap(~ decade, ncol=6) 241 | 242 | 243 | -------------------------------------------------------------------------------- /cheatsheet.Rmd: -------------------------------------------------------------------------------- 1 | 2 | ```{r} 3 | opts_chunk$set(warning=FALSE, message=FALSE, fig.width=8, fig.height=4) 4 | ``` 5 | 6 | # Loading ggplot 7 | 8 | ```{r} 9 | library(ggplot2) 10 | ``` 11 | 12 | # Basic use with qplot 13 | 14 | Load the sample data 15 | 16 | ```{r} 17 | set.seed(1410) # make the sample reproducible 18 | head(diamonds) 19 | dsmall <- diamonds[sample(nrow(diamonds), 100), ] 20 | ``` 21 | 22 | ```{r} 23 | qplot(carat, price, data=diamonds) 24 | ``` 25 | 26 | ## Color, size, shape and other aesthetic attributes 27 | 28 | ```{r} 29 | qplot(carat, price, data=dsmall, color=color, shape=cut, alpha=I(1/2)) 30 | ``` 31 | 32 | ## Plot geoms 33 | 34 | ```{r} 35 | qplot(carat, price, data=dsmall, geom=c("point", "smooth")) 36 | ``` 37 | 38 | ### Adding a smoother 39 | There are many different smoother that can be used with `method` argument. 40 | 41 | ```{r} 42 | qplot(carat, price, data=dsmall, geom=c("point", "smooth"), method="lm") 43 | ``` 44 | 45 | ### Boxplots and jittered points 46 | 47 | ```{r} 48 | qplot(color, price/carat, data=diamonds, geom="jitter") 49 | ``` 50 | 51 | ```{r} 52 | qplot(color, price/carat, data=diamonds, geom="boxplot") 53 | ``` 54 | 55 | ### Histogram and density plots 56 | 57 | ```{r} 58 | qplot(carat, data=diamonds, geom="histogram", fill=color) 59 | ``` 60 | 61 | ```{r} 62 | qplot(carat, data=diamonds, geom="density", color=color) 63 | ``` 64 | 65 | Change the amount of smoothing with `binwidth` argument. 66 | 67 | ```{r} 68 | qplot(carat, data=diamonds, geom="histogram", binwidth=1) 69 | qplot(carat, data=diamonds, geom="histogram", binwidth=0.1) 70 | qplot(carat, data=diamonds, geom="histogram", binwidth=0.01) 71 | ``` 72 | 73 | ### Bar charts 74 | 75 | ```{r} 76 | qplot(color, data=diamonds, geom="bar") 77 | ``` 78 | 79 | ```{r} 80 | # bar plot of diamond color weighted by carat 81 | qplot(color, data=diamonds, geom="bar", weight=carat) + 82 | scale_y_continuous("carat") 83 | ``` 84 | 85 | ### Time series 86 | 87 | ```{r} 88 | head(economics) 89 | ``` 90 | 91 | ```{r} 92 | qplot(date, unemploy/pop, data=economics, geom="line") 93 | ``` 94 | 95 | ## Faceting 96 | 97 | ```{r} 98 | qplot(carat, data=diamonds, facets=color~., 99 | geom="histogram", binwidth=0.1, xlim=c(0,3)) 100 | ``` 101 | 102 | ## Other options 103 | - `xlim` and `ylim`: set limits for x- and y-axis (e.g. `xlim=c(0,20)`) 104 | - `main`: main title for the plot 105 | - `xlab` and `ylab`: labels for x- and y-axis 106 | 107 | ```{r} 108 | qplot(carat, price, data=dsmall, 109 | xlab="Price ($)", 110 | ylab="Weight (carats)", 111 | main="Price-weight relationship") 112 | ``` 113 | 114 | # Build a plot layer by layer 115 | 116 | More complicated, multi-layer plots can be generated using `ggplot()`. 117 | 118 | ## Basic plot types 119 | 120 | ```{r} 121 | df <- data.frame(x=c(3, 1, 5), y=c(2, 4, 6), label=c("a", "b", "c")) 122 | p <- ggplot(df, aes(x, y, label=label)) + xlab(NULL) + ylab(NULL) 123 | ``` 124 | 125 | ### `geom_point` 126 | ```{r} 127 | p + geom_point() + ggtitle("geom_point") 128 | ``` 129 | 130 | ### `geom_bar` 131 | ```{r} 132 | p + geom_bar(stat="identity") + ggtitle("geom_bar(stat=\"identity\")") 133 | ``` 134 | 135 | ### `geom_line` 136 | ```{r} 137 | p + geom_line() + ggtitle("geom_line") 138 | ``` 139 | 140 | ### `geom_area` 141 | ```{r} 142 | p + geom_area() + ggtitle("geom_area") 143 | ``` 144 | 145 | ### `geom_path` 146 | ```{r} 147 | p + geom_path() + ggtitle("geom_path") 148 | ``` 149 | 150 | ### `geom_text` 151 | ```{r} 152 | p + geom_text() + ggtitle("geom_text") 153 | ``` 154 | 155 | ### `geom_tile` 156 | ```{r} 157 | p + geom_tile() + ggtitle("geom_tile") 158 | ``` 159 | 160 | ### `geom_polygon` 161 | ```{r} 162 | p + geom_polygon() + ggtitle("geom_polygon") 163 | ``` 164 | 165 | ## Displaying distributions 166 | 167 | For 1d data, the geom is the histogram. 168 | 169 | ### `geom_histogram` and `geom_freqpoly` 170 | 171 | ```{r} 172 | depth_dist <- ggplot(diamonds, aes(depth)) + xlim(58, 68) 173 | depth_dist + geom_histogram() 174 | ``` 175 | 176 | To compare the distribution between groups, couple of options 177 | 178 | ```{r} 179 | depth_dist + geom_histogram(aes(y = ..density..), binwidth=0.1) + 180 | facet_grid(cut ~ .) 181 | depth_dist + geom_histogram(aes(fill=cut), binwidth=0.1, position="fill") 182 | depth_dist + geom_freqpoly(aes(y = ..density.., color=cut), binwidth=0.1) 183 | ``` 184 | 185 | ### `geom_boxplot` 186 | 187 | ```{r} 188 | qplot(cut, depth, data=diamonds, geom="boxplot") 189 | library(plyr) 190 | qplot(carat, depth, data=diamonds, geom="boxplot", 191 | group = round_any(carat, 0.1, floor), xlim=c(0, 3)) 192 | ``` 193 | 194 | ### `geom_jitter` 195 | 196 | ```{r} 197 | qplot(class, cty, data=mpg, geom="jitter") 198 | qplot(class, drv, data=mpg, geom="jitter") 199 | ``` 200 | 201 | ### `geom_density` 202 | 203 | ```{r} 204 | qplot(depth, data=diamonds, geom="density", xlim=c(54, 70)) 205 | qplot(depth, data=diamonds, geom="density", xlim=c(54, 70), fill=cut, alpha=I(0.2)) 206 | ``` 207 | 208 | ## Deal with overplotting 209 | 210 | - Make the points smaller 211 | 212 | ```{r} 213 | df <- data.frame(x=rnorm(2000), y=rnorm(2000)) 214 | norm <- ggplot(df, aes(x, y)) 215 | norm + geom_point() 216 | norm + geom_point(shape=1) 217 | norm + geom_point(shape = ".") # pixel-sized 218 | ``` 219 | 220 | - Use alpha blending 221 | 222 | ```{r} 223 | library(scales) 224 | norm + geom_point(color=alpha("black", 1/3)) 225 | norm + geom_point(color=alpha("black", 1/5)) 226 | norm + geom_point(color=alpha("black", 1/10)) 227 | ``` 228 | 229 | - Randomly jitter if there is some discreteness 230 | 231 | ```{r} 232 | td <- ggplot(diamonds, aes(table, depth)) + xlim(50, 70) + ylim(50, 70) 233 | td + geom_point() 234 | td + geom_jitter() 235 | jit <- position_jitter(width=0.5) 236 | td + geom_jitter(position=jit) 237 | td + geom_jitter(position=jit, color=alpha("black", 1/10)) 238 | td + geom_jitter(position=jit, color=alpha("black", 1/50)) 239 | td + geom_jitter(position=jit, color=alpha("black", 1/200)) 240 | ``` 241 | 242 | ## Surface plots 243 | 244 | ## Drawing maps 245 | 246 | ```{r} 247 | library(maps) 248 | data(us.cities) 249 | big_cities <- subset(us.cities, pop>500000) 250 | qplot(long, lat, data=big_cities) + borders("state", size=0.5) 251 | ``` 252 | 253 | ### [Choropleth map](http://en.wikipedia.org/wiki/Choropleth_map) 254 | ```{r} 255 | states <- map_data("state") 256 | arrests <- USArrests 257 | names(arrests) <- tolower(names(arrests)) 258 | arrests$region <- tolower(rownames(USArrests)) 259 | 260 | choro <- merge(states, arrests, by="region") 261 | # reorder the rows because order matters when drawing polygons and merge 262 | # destroys the original ordering 263 | choro <- choro[order(choro$order), ] 264 | qplot(long, lat, data=choro, group=group, fill=assault, geom="polygon") 265 | qplot(long, lat, data=choro, group=group, fill=assault/murder, geom="polygon") 266 | ``` 267 | 268 | ## Annotating a plot 269 | 270 | Just extra data 271 | 272 | - adding one at a time 273 | - many at once 274 | 275 | ```{r} 276 | unemp <- qplot(date, unemploy, data=economics, geom="line", 277 | xlab="", ylab="No. unemployed (1000s)") 278 | 279 | presidential <- presidential[-(1:3), ] 280 | 281 | yrng <- range(economics$unemploy) 282 | xrng <- range(economics$date) 283 | unemp + geom_vline(aes(xintercept=as.numeric(start)), data=presidential) 284 | unemp + geom_rect(aes(NULL, NULL, xmin=start, xmax=end, fill=party), 285 | ymin=yrng[1], ymax=yrng[2], data=presidential) + 286 | scale_fill_manual(values=alpha(c("blue", "red"), 0.2)) 287 | 288 | last_plot() + geom_text(aes(x=start, y=yrng[1], label=name), 289 | data=presidential, size=3, hjust=0, vjust=0) 290 | 291 | caption <- paste(strwrap("Unemployment rates in the US have varied 292 | alot over the years", 40), collapse="\n") 293 | unemp + geom_text(aes(x, y, label=caption), 294 | data=data.frame(x=xrng[2], y=yrng[2]), 295 | hjust=1, vjust=1, size=4) 296 | 297 | highest <- subset(economics, unemploy==max(unemploy)) 298 | unemp + geom_point(data=highest, size=3, color=alpha("red", 0.3)) 299 | ``` 300 | 301 | # Faceting 302 | 303 | ## Facet grid 304 | 305 | ```{r} 306 | qplot(cty, hwy, data=mpg) + facet_grid(. ~ cyl) 307 | qplot(cty, data=mpg, geom="histogram", binwidth=2) + facet_grid(cyl ~ .) 308 | qplot(cty, hwy, data=mpg) + facet_grid(drv ~ cyl) 309 | ``` 310 | 311 | ### Margins 312 | 313 | ```{r} 314 | p <- qplot(displ, hwy, data=mpg) + geom_smooth(method="lm", se=F) 315 | p + facet_grid(cyl ~ drv) 316 | p + facet_grid(cyl ~ drv, margins=T) 317 | ``` 318 | 319 | ## Facet wrap 320 | 321 | ```{r} 322 | library(plyr) 323 | movies$decade <- round_any(movies$year, 10, floor) 324 | qplot(rating, ..density.., data=subset(movies, decade > 1890), 325 | geom="histogram", binwidth=0.5) + 326 | facet_wrap(~ decade, ncol=6) 327 | ``` 328 | 329 | -------------------------------------------------------------------------------- /figure/unnamed-chunk-10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-10.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-11.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-121.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-121.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-122.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-122.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-123.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-123.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-13.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-14.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-16.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-17.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-18.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-18.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-20.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-20.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-21.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-21.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-22.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-22.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-23.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-23.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-24.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-24.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-25.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-25.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-26.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-26.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-27.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-27.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-28.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-28.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-29.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-29.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-291.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-291.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-292.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-292.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-293.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-293.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-30.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-30.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-301.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-301.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-302.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-302.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-31.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-31.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-311.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-311.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-312.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-312.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-32.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-321.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-321.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-322.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-322.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-33.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-33.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-331.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-331.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-332.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-332.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-333.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-333.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-341.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-341.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-342.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-342.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-343.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-343.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-351.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-351.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-352.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-352.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-353.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-353.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-354.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-354.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-355.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-355.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-356.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-356.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-36.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-36.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-371.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-371.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-372.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-372.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-381.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-381.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-382.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-382.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-383.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-383.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-384.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-384.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-385.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-385.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-391.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-391.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-392.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-392.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-393.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-393.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-4.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-401.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-401.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-402.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-402.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-41.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-41.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-5.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-6.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-7.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-8.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sefakilic/ggplot-cheatsheet/e1b456d6d133b20230989b23ca72380befd306b7/figure/unnamed-chunk-9.png -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ggplot-cheatsheet 2 | ================= 3 | 4 | cheatsheet for [ggplot2](http://ggplot2.org/), compiled mostly from the book, 5 | [ggplot2: Elegant Graphics for Data Analysis](http://www.amazon.com/dp/0387981403/ref=cm_sw_su_dp?tag=ggplot2-20) 6 | 7 | Documentation for ggplot is available [here](http://docs.ggplot2.org/current/index.html). 8 | 9 | **Table of Contents** 10 | 11 | - [Loading ggplot](#loading-ggplot) 12 | - [Basic use with qplot](#basic-use-with-qplot) 13 | - [Color, size, shape and other aesthetic attributes](#color-size-shape-and-other-aesthetic-attributes) 14 | - [Plot geoms](#plot-geoms) 15 | - [Adding a smoother](#adding-a-smoother) 16 | - [Boxplots and jittered points](#boxplots-and-jittered-points) 17 | - [Histogram and density plots](#histogram-and-density-plots) 18 | - [Bar charts](#bar-charts) 19 | - [Time series](#time-series) 20 | - [Faceting](#faceting) 21 | - [Other options](#other-options) 22 | - [Build a plot layer by layer](#build-a-plot-layer-by-layer) 23 | - [Basic plot types](#basic-plot-types) 24 | - [`geom_point`](#geom_point) 25 | - [`geom_bar`](#geom_bar) 26 | - [`geom_line`](#geom_line) 27 | - [`geom_area`](#geom_area) 28 | - [`geom_path`](#geom_path) 29 | - [`geom_text`](#geom_text) 30 | - [`geom_tile`](#geom_tile) 31 | - [`geom_polygon`](#geom_polygon) 32 | - [Displaying distributions](#displaying-distributions) 33 | - [`geom_histogram` and `geom_freqpoly`](#geom_histogram-and-geom_freqpoly) 34 | - [`geom_boxplot`](#geom_boxplot) 35 | - [`geom_jitter`](#geom_jitter) 36 | - [`geom_density`](#geom_density) 37 | - [Deal with overplotting](#deal-with-overplotting) 38 | - [Surface plots](#surface-plots) 39 | - [Drawing maps](#drawing-maps) 40 | - [[Choropleth map](http://en.wikipedia.org/wiki/Choropleth_map)](#choropleth-maphttpenwikipediaorgwikichoropleth_map) 41 | - [Annotating a plot](#annotating-a-plot) 42 | - [Faceting](#faceting-1) 43 | - [Facet grid](#facet-grid) 44 | - [Margins](#margins) 45 | - [Facet wrap](#facet-wrap) 46 | 47 | 48 | 49 | 50 | 51 | ```r 52 | opts_chunk$set(warning=FALSE, message=FALSE, fig.width=8, fig.height=4) 53 | ``` 54 | 55 | # Loading ggplot 56 | 57 | 58 | ```r 59 | library(ggplot2) 60 | ``` 61 | 62 | # Basic use with qplot 63 | 64 | Load the sample data 65 | 66 | 67 | ```r 68 | set.seed(1410) # make the sample reproducible 69 | head(diamonds) 70 | ``` 71 | 72 | ``` 73 | ## carat cut color clarity depth table price x y z 74 | ## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43 75 | ## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31 76 | ## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31 77 | ## 4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63 78 | ## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75 79 | ## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48 80 | ``` 81 | 82 | ```r 83 | dsmall <- diamonds[sample(nrow(diamonds), 100), ] 84 | ``` 85 | 86 | 87 | ```r 88 | qplot(carat, price, data=diamonds) 89 | ``` 90 | 91 | ![plot of chunk unnamed-chunk-4](figure/unnamed-chunk-4.png) 92 | 93 | ## Color, size, shape and other aesthetic attributes 94 | 95 | 96 | ```r 97 | qplot(carat, price, data=dsmall, color=color, shape=cut, alpha=I(1/2)) 98 | ``` 99 | 100 | ![plot of chunk unnamed-chunk-5](figure/unnamed-chunk-5.png) 101 | 102 | ## Plot geoms 103 | 104 | 105 | ```r 106 | qplot(carat, price, data=dsmall, geom=c("point", "smooth")) 107 | ``` 108 | 109 | ![plot of chunk unnamed-chunk-6](figure/unnamed-chunk-6.png) 110 | 111 | ### Adding a smoother 112 | There are many different smoother that can be used with `method` argument. 113 | 114 | 115 | ```r 116 | qplot(carat, price, data=dsmall, geom=c("point", "smooth"), method="lm") 117 | ``` 118 | 119 | ![plot of chunk unnamed-chunk-7](figure/unnamed-chunk-7.png) 120 | 121 | ### Boxplots and jittered points 122 | 123 | 124 | ```r 125 | qplot(color, price/carat, data=diamonds, geom="jitter") 126 | ``` 127 | 128 | ![plot of chunk unnamed-chunk-8](figure/unnamed-chunk-8.png) 129 | 130 | 131 | ```r 132 | qplot(color, price/carat, data=diamonds, geom="boxplot") 133 | ``` 134 | 135 | ![plot of chunk unnamed-chunk-9](figure/unnamed-chunk-9.png) 136 | 137 | ### Histogram and density plots 138 | 139 | 140 | ```r 141 | qplot(carat, data=diamonds, geom="histogram", fill=color) 142 | ``` 143 | 144 | ![plot of chunk unnamed-chunk-10](figure/unnamed-chunk-10.png) 145 | 146 | 147 | ```r 148 | qplot(carat, data=diamonds, geom="density", color=color) 149 | ``` 150 | 151 | ![plot of chunk unnamed-chunk-11](figure/unnamed-chunk-11.png) 152 | 153 | Change the amount of smoothing with `binwidth` argument. 154 | 155 | 156 | ```r 157 | qplot(carat, data=diamonds, geom="histogram", binwidth=1) 158 | ``` 159 | 160 | ![plot of chunk unnamed-chunk-12](figure/unnamed-chunk-121.png) 161 | 162 | ```r 163 | qplot(carat, data=diamonds, geom="histogram", binwidth=0.1) 164 | ``` 165 | 166 | ![plot of chunk unnamed-chunk-12](figure/unnamed-chunk-122.png) 167 | 168 | ```r 169 | qplot(carat, data=diamonds, geom="histogram", binwidth=0.01) 170 | ``` 171 | 172 | ![plot of chunk unnamed-chunk-12](figure/unnamed-chunk-123.png) 173 | 174 | ### Bar charts 175 | 176 | 177 | ```r 178 | qplot(color, data=diamonds, geom="bar") 179 | ``` 180 | 181 | ![plot of chunk unnamed-chunk-13](figure/unnamed-chunk-13.png) 182 | 183 | 184 | ```r 185 | # bar plot of diamond color weighted by carat 186 | qplot(color, data=diamonds, geom="bar", weight=carat) + 187 | scale_y_continuous("carat") 188 | ``` 189 | 190 | ![plot of chunk unnamed-chunk-14](figure/unnamed-chunk-14.png) 191 | 192 | ### Time series 193 | 194 | 195 | ```r 196 | head(economics) 197 | ``` 198 | 199 | ``` 200 | ## date pce pop psavert uempmed unemploy 201 | ## 1 1967-06-30 507.8 198712 9.8 4.5 2944 202 | ## 2 1967-07-31 510.9 198911 9.8 4.7 2945 203 | ## 3 1967-08-31 516.7 199113 9.0 4.6 2958 204 | ## 4 1967-09-30 513.3 199311 9.8 4.9 3143 205 | ## 5 1967-10-31 518.5 199498 9.7 4.7 3066 206 | ## 6 1967-11-30 526.2 199657 9.4 4.8 3018 207 | ``` 208 | 209 | 210 | ```r 211 | qplot(date, unemploy/pop, data=economics, geom="line") 212 | ``` 213 | 214 | ![plot of chunk unnamed-chunk-16](figure/unnamed-chunk-16.png) 215 | 216 | ## Faceting 217 | 218 | 219 | ```r 220 | qplot(carat, data=diamonds, facets=color~., 221 | geom="histogram", binwidth=0.1, xlim=c(0,3)) 222 | ``` 223 | 224 | ![plot of chunk unnamed-chunk-17](figure/unnamed-chunk-17.png) 225 | 226 | ## Other options 227 | - `xlim` and `ylim`: set limits for x- and y-axis (e.g. `xlim=c(0,20)`) 228 | - `main`: main title for the plot 229 | - `xlab` and `ylab`: labels for x- and y-axis 230 | 231 | 232 | ```r 233 | qplot(carat, price, data=dsmall, 234 | xlab="Price ($)", 235 | ylab="Weight (carats)", 236 | main="Price-weight relationship") 237 | ``` 238 | 239 | ![plot of chunk unnamed-chunk-18](figure/unnamed-chunk-18.png) 240 | 241 | # Build a plot layer by layer 242 | 243 | More complicated, multi-layer plots can be generated using `ggplot()`. 244 | 245 | ## Basic plot types 246 | 247 | 248 | ```r 249 | df <- data.frame(x=c(3, 1, 5), y=c(2, 4, 6), label=c("a", "b", "c")) 250 | p <- ggplot(df, aes(x, y, label=label)) + xlab(NULL) + ylab(NULL) 251 | ``` 252 | 253 | ### `geom_point` 254 | 255 | ```r 256 | p + geom_point() + ggtitle("geom_point") 257 | ``` 258 | 259 | ![plot of chunk unnamed-chunk-20](figure/unnamed-chunk-20.png) 260 | 261 | ### `geom_bar` 262 | 263 | ```r 264 | p + geom_bar(stat="identity") + ggtitle("geom_bar(stat=\"identity\")") 265 | ``` 266 | 267 | ![plot of chunk unnamed-chunk-21](figure/unnamed-chunk-21.png) 268 | 269 | ### `geom_line` 270 | 271 | ```r 272 | p + geom_line() + ggtitle("geom_line") 273 | ``` 274 | 275 | ![plot of chunk unnamed-chunk-22](figure/unnamed-chunk-22.png) 276 | 277 | ### `geom_area` 278 | 279 | ```r 280 | p + geom_area() + ggtitle("geom_area") 281 | ``` 282 | 283 | ![plot of chunk unnamed-chunk-23](figure/unnamed-chunk-23.png) 284 | 285 | ### `geom_path` 286 | 287 | ```r 288 | p + geom_path() + ggtitle("geom_path") 289 | ``` 290 | 291 | ![plot of chunk unnamed-chunk-24](figure/unnamed-chunk-24.png) 292 | 293 | ### `geom_text` 294 | 295 | ```r 296 | p + geom_text() + ggtitle("geom_text") 297 | ``` 298 | 299 | ![plot of chunk unnamed-chunk-25](figure/unnamed-chunk-25.png) 300 | 301 | ### `geom_tile` 302 | 303 | ```r 304 | p + geom_tile() + ggtitle("geom_tile") 305 | ``` 306 | 307 | ![plot of chunk unnamed-chunk-26](figure/unnamed-chunk-26.png) 308 | 309 | ### `geom_polygon` 310 | 311 | ```r 312 | p + geom_polygon() + ggtitle("geom_polygon") 313 | ``` 314 | 315 | ![plot of chunk unnamed-chunk-27](figure/unnamed-chunk-27.png) 316 | 317 | ## Displaying distributions 318 | 319 | For 1d data, the geom is the histogram. 320 | 321 | ### `geom_histogram` and `geom_freqpoly` 322 | 323 | 324 | ```r 325 | depth_dist <- ggplot(diamonds, aes(depth)) + xlim(58, 68) 326 | depth_dist + geom_histogram() 327 | ``` 328 | 329 | ![plot of chunk unnamed-chunk-28](figure/unnamed-chunk-28.png) 330 | 331 | To compare the distribution between groups, couple of options 332 | 333 | 334 | ```r 335 | depth_dist + geom_histogram(aes(y = ..density..), binwidth=0.1) + 336 | facet_grid(cut ~ .) 337 | ``` 338 | 339 | ![plot of chunk unnamed-chunk-29](figure/unnamed-chunk-291.png) 340 | 341 | ```r 342 | depth_dist + geom_histogram(aes(fill=cut), binwidth=0.1, position="fill") 343 | ``` 344 | 345 | ![plot of chunk unnamed-chunk-29](figure/unnamed-chunk-292.png) 346 | 347 | ```r 348 | depth_dist + geom_freqpoly(aes(y = ..density.., color=cut), binwidth=0.1) 349 | ``` 350 | 351 | ![plot of chunk unnamed-chunk-29](figure/unnamed-chunk-293.png) 352 | 353 | ### `geom_boxplot` 354 | 355 | 356 | ```r 357 | qplot(cut, depth, data=diamonds, geom="boxplot") 358 | ``` 359 | 360 | ![plot of chunk unnamed-chunk-30](figure/unnamed-chunk-301.png) 361 | 362 | ```r 363 | library(plyr) 364 | qplot(carat, depth, data=diamonds, geom="boxplot", 365 | group = round_any(carat, 0.1, floor), xlim=c(0, 3)) 366 | ``` 367 | 368 | ![plot of chunk unnamed-chunk-30](figure/unnamed-chunk-302.png) 369 | 370 | ### `geom_jitter` 371 | 372 | 373 | ```r 374 | qplot(class, cty, data=mpg, geom="jitter") 375 | ``` 376 | 377 | ![plot of chunk unnamed-chunk-31](figure/unnamed-chunk-311.png) 378 | 379 | ```r 380 | qplot(class, drv, data=mpg, geom="jitter") 381 | ``` 382 | 383 | ![plot of chunk unnamed-chunk-31](figure/unnamed-chunk-312.png) 384 | 385 | ### `geom_density` 386 | 387 | 388 | ```r 389 | qplot(depth, data=diamonds, geom="density", xlim=c(54, 70)) 390 | ``` 391 | 392 | ![plot of chunk unnamed-chunk-32](figure/unnamed-chunk-321.png) 393 | 394 | ```r 395 | qplot(depth, data=diamonds, geom="density", xlim=c(54, 70), fill=cut, alpha=I(0.2)) 396 | ``` 397 | 398 | ![plot of chunk unnamed-chunk-32](figure/unnamed-chunk-322.png) 399 | 400 | ## Deal with overplotting 401 | 402 | - Make the points smaller 403 | 404 | 405 | ```r 406 | df <- data.frame(x=rnorm(2000), y=rnorm(2000)) 407 | norm <- ggplot(df, aes(x, y)) 408 | norm + geom_point() 409 | ``` 410 | 411 | ![plot of chunk unnamed-chunk-33](figure/unnamed-chunk-331.png) 412 | 413 | ```r 414 | norm + geom_point(shape=1) 415 | ``` 416 | 417 | ![plot of chunk unnamed-chunk-33](figure/unnamed-chunk-332.png) 418 | 419 | ```r 420 | norm + geom_point(shape = ".") # pixel-sized 421 | ``` 422 | 423 | ![plot of chunk unnamed-chunk-33](figure/unnamed-chunk-333.png) 424 | 425 | - Use alpha blending 426 | 427 | 428 | ```r 429 | library(scales) 430 | norm + geom_point(color=alpha("black", 1/3)) 431 | ``` 432 | 433 | ![plot of chunk unnamed-chunk-34](figure/unnamed-chunk-341.png) 434 | 435 | ```r 436 | norm + geom_point(color=alpha("black", 1/5)) 437 | ``` 438 | 439 | ![plot of chunk unnamed-chunk-34](figure/unnamed-chunk-342.png) 440 | 441 | ```r 442 | norm + geom_point(color=alpha("black", 1/10)) 443 | ``` 444 | 445 | ![plot of chunk unnamed-chunk-34](figure/unnamed-chunk-343.png) 446 | 447 | - Randomly jitter if there is some discreteness 448 | 449 | 450 | ```r 451 | td <- ggplot(diamonds, aes(table, depth)) + xlim(50, 70) + ylim(50, 70) 452 | td + geom_point() 453 | ``` 454 | 455 | ![plot of chunk unnamed-chunk-35](figure/unnamed-chunk-351.png) 456 | 457 | ```r 458 | td + geom_jitter() 459 | ``` 460 | 461 | ![plot of chunk unnamed-chunk-35](figure/unnamed-chunk-352.png) 462 | 463 | ```r 464 | jit <- position_jitter(width=0.5) 465 | td + geom_jitter(position=jit) 466 | ``` 467 | 468 | ![plot of chunk unnamed-chunk-35](figure/unnamed-chunk-353.png) 469 | 470 | ```r 471 | td + geom_jitter(position=jit, color=alpha("black", 1/10)) 472 | ``` 473 | 474 | ![plot of chunk unnamed-chunk-35](figure/unnamed-chunk-354.png) 475 | 476 | ```r 477 | td + geom_jitter(position=jit, color=alpha("black", 1/50)) 478 | ``` 479 | 480 | ![plot of chunk unnamed-chunk-35](figure/unnamed-chunk-355.png) 481 | 482 | ```r 483 | td + geom_jitter(position=jit, color=alpha("black", 1/200)) 484 | ``` 485 | 486 | ![plot of chunk unnamed-chunk-35](figure/unnamed-chunk-356.png) 487 | 488 | ## Surface plots 489 | 490 | ## Drawing maps 491 | 492 | 493 | ```r 494 | library(maps) 495 | data(us.cities) 496 | big_cities <- subset(us.cities, pop>500000) 497 | qplot(long, lat, data=big_cities) + borders("state", size=0.5) 498 | ``` 499 | 500 | ![plot of chunk unnamed-chunk-36](figure/unnamed-chunk-36.png) 501 | 502 | ### [Choropleth map](http://en.wikipedia.org/wiki/Choropleth_map) 503 | 504 | ```r 505 | states <- map_data("state") 506 | arrests <- USArrests 507 | names(arrests) <- tolower(names(arrests)) 508 | arrests$region <- tolower(rownames(USArrests)) 509 | 510 | choro <- merge(states, arrests, by="region") 511 | # reorder the rows because order matters when drawing polygons and merge 512 | # destroys the original ordering 513 | choro <- choro[order(choro$order), ] 514 | qplot(long, lat, data=choro, group=group, fill=assault, geom="polygon") 515 | ``` 516 | 517 | ![plot of chunk unnamed-chunk-37](figure/unnamed-chunk-371.png) 518 | 519 | ```r 520 | qplot(long, lat, data=choro, group=group, fill=assault/murder, geom="polygon") 521 | ``` 522 | 523 | ![plot of chunk unnamed-chunk-37](figure/unnamed-chunk-372.png) 524 | 525 | ## Annotating a plot 526 | 527 | Just extra data 528 | 529 | - adding one at a time 530 | - many at once 531 | 532 | 533 | ```r 534 | unemp <- qplot(date, unemploy, data=economics, geom="line", 535 | xlab="", ylab="No. unemployed (1000s)") 536 | 537 | presidential <- presidential[-(1:3), ] 538 | 539 | yrng <- range(economics$unemploy) 540 | xrng <- range(economics$date) 541 | unemp + geom_vline(aes(xintercept=as.numeric(start)), data=presidential) 542 | ``` 543 | 544 | ![plot of chunk unnamed-chunk-38](figure/unnamed-chunk-381.png) 545 | 546 | ```r 547 | unemp + geom_rect(aes(NULL, NULL, xmin=start, xmax=end, fill=party), 548 | ymin=yrng[1], ymax=yrng[2], data=presidential) + 549 | scale_fill_manual(values=alpha(c("blue", "red"), 0.2)) 550 | ``` 551 | 552 | ![plot of chunk unnamed-chunk-38](figure/unnamed-chunk-382.png) 553 | 554 | ```r 555 | last_plot() + geom_text(aes(x=start, y=yrng[1], label=name), 556 | data=presidential, size=3, hjust=0, vjust=0) 557 | ``` 558 | 559 | ![plot of chunk unnamed-chunk-38](figure/unnamed-chunk-383.png) 560 | 561 | ```r 562 | caption <- paste(strwrap("Unemployment rates in the US have varied 563 | alot over the years", 40), collapse="\n") 564 | unemp + geom_text(aes(x, y, label=caption), 565 | data=data.frame(x=xrng[2], y=yrng[2]), 566 | hjust=1, vjust=1, size=4) 567 | ``` 568 | 569 | ![plot of chunk unnamed-chunk-38](figure/unnamed-chunk-384.png) 570 | 571 | ```r 572 | highest <- subset(economics, unemploy==max(unemploy)) 573 | unemp + geom_point(data=highest, size=3, color=alpha("red", 0.3)) 574 | ``` 575 | 576 | ![plot of chunk unnamed-chunk-38](figure/unnamed-chunk-385.png) 577 | 578 | # Faceting 579 | 580 | ## Facet grid 581 | 582 | 583 | ```r 584 | qplot(cty, hwy, data=mpg) + facet_grid(. ~ cyl) 585 | ``` 586 | 587 | ![plot of chunk unnamed-chunk-39](figure/unnamed-chunk-391.png) 588 | 589 | ```r 590 | qplot(cty, data=mpg, geom="histogram", binwidth=2) + facet_grid(cyl ~ .) 591 | ``` 592 | 593 | ![plot of chunk unnamed-chunk-39](figure/unnamed-chunk-392.png) 594 | 595 | ```r 596 | qplot(cty, hwy, data=mpg) + facet_grid(drv ~ cyl) 597 | ``` 598 | 599 | ![plot of chunk unnamed-chunk-39](figure/unnamed-chunk-393.png) 600 | 601 | ### Margins 602 | 603 | 604 | ```r 605 | p <- qplot(displ, hwy, data=mpg) + geom_smooth(method="lm", se=F) 606 | p + facet_grid(cyl ~ drv) 607 | ``` 608 | 609 | ![plot of chunk unnamed-chunk-40](figure/unnamed-chunk-401.png) 610 | 611 | ```r 612 | p + facet_grid(cyl ~ drv, margins=T) 613 | ``` 614 | 615 | ![plot of chunk unnamed-chunk-40](figure/unnamed-chunk-402.png) 616 | 617 | ## Facet wrap 618 | 619 | 620 | ```r 621 | library(plyr) 622 | movies$decade <- round_any(movies$year, 10, floor) 623 | qplot(rating, ..density.., data=subset(movies, decade > 1890), 624 | geom="histogram", binwidth=0.5) + 625 | facet_wrap(~ decade, ncol=6) 626 | ``` 627 | 628 | ![plot of chunk unnamed-chunk-41](figure/unnamed-chunk-41.png) 629 | 630 | --------------------------------------------------------------------------------