Geoms - Use a geom to represent data points, use the geom's aesthetic properties to represent variables. Each function returns a layer. One Variable Two Variables Cheatsheet f ^Studio ggplot2 is based on the grammar of graphics, the idea that you can build every graph from the same few components: a data set, a set of geoms—visual marks that represent data points, and a coordinate system. • data geom coordinate »;f system plot To display data values, map variables in the data set to aesthetic properties of the geom like size, color, and x and y locations. data geom coordinate x=f system Build a graph with qplot() orggplot() aesthetic mappings plot qplot(x = cty, y = hwy, color = cyl, data = mpg, geom = "point") Creates a complete plotwith given data, geom, and mappings. Supplies many useful defaults. ggplot(data = mpg, aes(x = cty, y = hwy)) Begins a plot that you finish by adding layers to. No defaults, but provides more control than qplotfj. ggplottmpg, aeslhwy, cty)) geom_point(aes( color = cyU) geom_smootfi (method ="lm") + coord_cartesian() + scale_color_gradient() + theme_bw() Add a new layer to a plotwith a geom_*() orstat_*() function. Each provides a geom, a set of aesthetic mappings, and a default stat and position adjustment. last_plot() Returns the last plot ggsavef'plot.png", width = 5, height = 5) Saves last plot as 5' x 5' file named "plot.png" in working directory. Matches file type to file extension. A □ Continuous a <- ggplot(mpg, aes(hwy)) geom_area(stat= "bin") x, y, alpha, color, fill, linetype, size b + geom_area(aes(y = ..density..), stat = "bin") geom_density(kernel = "gaussian") x, y, alpha, color, fill, linetype, size, weight b + geom_density(aes(y = ..county..)) geom_dotplot() x, y, alpha, color, fill geom_freqpoly() x, y, alpha, color, linetype, size o + geom_freqpoly(aes(y = ..density..)) geom_histogram(binwidth = 5) x, y, alpha, color, fill, linetype, size, weight b + geom_histogram(aes(y = ..density..)) Discrete b <- ggplotfmpg, aes(fl)) geom_bar() x, alpha, color, fill, linetype, size, weight Graphical Primitives c <- ggplot(map, aes(long, lat)) geom_poIygon(aes(group = group)) x, y, alpha, color, fill, linetype, size d <- ggplotfeconomics, acs(date, unemploy)) geom_path(lineend=,,buttM, linejoin-'round', linemitre=l) x, y, alpha, color, linetype, size geom_ribbon(aes(ymin=unemploy-900; ymax=unemploy + 900)) x, ymax, ymin, alpha, color, fill, linetype, size e <- ggplot(seals, aes(x = long, y = lat)) geom_segment(aes( xend = long + deltajong, yend = lat + delta_lat)) x, xend, y, yend, alpha, color, linetype, size geom_rect(aes(xmin = long, ymin = lat, xmax= long + deltajong, ymax = lat + deltajat)) xmax, xmin, ymax, ymin, alpha, color, fill, .inetype, size Continuous X, Continuous Y f <- ggplotfmpg, aesfcty, hwy)) geom_blank() geomJitterO x, y, alpha, color, fill, shape, size geom_point() x, y, alpha, color, fill, shape, size geom_quantile() x, y, alpha, color, linetype, size, weight geom_rug(sides = "bl") alpha, color, linetype, size geom_smooth(model = Im) x, y, alpha, color, fill, linetype, size, weight geom_text(aes(label = cty)) x, y, label, alpha, angle, color, family, fontface, hjust, lineheight, size, vjust Discrete X, Continuous Y g <- ggplotl mpg. acslclass, hwy)) geom_bar(stat = "identity") x, y, alpha, color, fill, linetype, size, weight geom_boxplot() .ower, middle, upper, x, ymax, ymin, alpha, color, fill, linetype, shape, size, weight geom_dotplot(binaxis = "y", stackdir = "center") x, y, alpha, color, fill geom_vioIin(scale = "area") x, y, alpha, color, fill, linetype, size, weight Discrete X, Discrete Y h <- ggplotfdiamonds, aesfcut, color)) mi geomJitterQ x, y, alpha, color, fill, shape, size Continuous Bivariate Distribution i <- ggplotfmovies, aesfyear, rating)) geom_bin2d(binwidth = c(5, 0.5)) xmax, xmin, ymax, ymin, alpha, color, fill linetype, size, weight i + geom_density2d() x, y, alpha, colour, linetype, size geom_hex() x, y, alpha, colour, fill size Continuous Function j <- ggplotfeconomics, aes(date, unemploy)) geom_area() x, y, alpha, color, fill, linetype, size j + geom_line() at x, y, alpha, color, linetype, size _ geom_step(direction = "hv") nJl" x, y, alpha, color, linetype, size Visualizing error df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2) k <- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se)) geom_crossbar(fatten = 2) x, y, ymax, ymin, alpha, color, fill, linetype, size geom_errorbar() x, ymax, ymin, alpha, color, linetype, size, width (also geom_errorbarh()) geom_Iinerange() x, ymin, ymax, alpha, color, linetype, size geom_pointrange() x, y, ymin, ymax, alpha, color, fill, linetype. shape, size Maps data <- data.frame(murder= USArrests$Murder, state = tolower(rownames(USArrests))) map <- map_data("state") I <- ggplot(data, aes(fill = murder)) geom_map(aes(map_id = state), map = map) + expand_limits(x = map$long, y = mapSlat) map_id, alpha, color, fill, linetype, size Three Variables seals$z<-with (seals, sqrt(delta_longA2 + delta_latA2)) m <- ggplot(seals, aes(long, lat)) r geom_raster(aes(fill =z), hjust=0.5, vjust=0.5, interpolate=FALSE) x, y, alpha, fill geom_tiIe(aes(fill = z)) x, y, alpha, color, fill, linetype, size geom_contour(aes(z = z)) x, y, z, alpha, colour, linetype, size, weight r i ^Studio® is a trademark of RStudio, Inc. • CC BY RStudio ■ info(Srstudio.com • 844-448-1212 ■ rstudio.com _earnmoreatdocs.ggplot2.org • ggplot2 0.9.3.1 • Updated: 3/15 Stats - An alternative way to build a layer Scales Some plots visualize a transformation of the original data set. Scales control how a plot maps data values to the visual Use a stat to choose a common transformation to visualize, e.g. a + geom_bar(stat = "bin") ED El EE data stat geom coordinate plot y = *count System Each stat creates additional variables to map aesthetics to. These variables use a common ..name., syntax. stat functions and geom functions both combine a stat with a geom to make a layer, i.e. stat_bin(geom="bar") does the same asgeom_bar(stat="bin") layer specific T variable created stat function \ mappings g_ by transformation values of an aesthetic. To change the mapping, add a custom scale. geom_bar(aes(fi II = fl)) scale ! aestnetic I prepackaged [ scale specific ~ 1 to adjust 1 scale to use 1 arguments n + scale_fill_manual( values = c("skyblue", "royalblue", "blue", "navy") limits = c("d", "e", "p", "r"), breaks =c("d", "e", "p", name = "fuel", labels = c("D", "E", "P", "R")) 1 stat_density2d(aes(fill = ..level., "polygon", n = 100) geom = eom for layer a + stat_bin(binwjdth = 1, origin = 10) x, y | ..count.., ..ncount.., ..density.., ..ndensity.. a + stat_bindot(binwidth = 1, binaxis = "x") x, y, | ..count.., ..ncount.. a + stat_density(adjust = 1, kernel = "gaussian") x, y, | ..count.., ..density.., ..scaled.. f + stat_bin2d(bins = 30, drop = TRU E) x, y, fill | ..count.., ..density., f+ stat_binhex(bins = 30) x, y, fill | ..count.., ..density., f+ stat_density2d(contour = TRUE, n = 100) x, y, color, size | ..level.. 2D distributions n+stat_contour(aes(z = z)) x, y, z, order | ..level.. m+ stat_spoke(aes(radius= z, angle = z)) angle, radius, x, xend, y, yend | ..x.., ..xend.., ..y.., ..yend.. n + stat_summary_hex(aes(z = z), bins = 30, fun = mean) x, y, z, fill | ..value.. stat_summary2d(aes(z = z), bins = 30, fun = mean) x, y, z, fill | ..value.. rangeofvaluesto include in mapping I General Purpose scales Use with any aesthetic: alpha, color, fill, linetype, shape, size scale_*_continuous{) - map cont' values to visual values scale_*_discrete{) - map discrete values to visual values scale_*_identity{) - use data values as visual values scale_*_manual{values = c()) - map discrete values to manually chosen visual values X and Y location scales Use with x ory aesthetics (x shown here) scale_x_date{labels = date_format("%m/%d"), breaks = date_breaks("2 weeks")) - treat x values as dates. See ?strptimefor label formats. scale_x_datetime{) - treat x values as date times. Use same arguments asscale_x_date(). scale_x_loglO{) - Plot x on loglO scale scale_x_reverse{) - Reverse direction of x axis scale_x_sqrt{) - Plot x on square root scale Color and fill scales Discrete Continuous geom_bar( aes(fill = fl)) I scale_fill_brewer( palette = "Blues") ' For palette choices: library(RcolorBrewer) display.brewer.allQ I n + scale_fill_grey( start = 0.2, end =0.8, I na.value = "red") g + stat_boxplot(coef=1.5) x,y | ..lower.., ..middle.., ..upper.., ..outliers., g + stat_ydensity(adjust= 1, kernel = "gaussian", scale = "area") x,y | ..density.., ..scaled.., ..count.., ..n.., ..violinwidth.., ..width.. stat_ecdf(n=40) x,y| ..x.., ..y.. f + stat_quantile(quantiles = c(0.25, 0.5, 0.75), formula =y - log(x), method = "rq") x, y | ..quantile.., ..x.., ..y.. f + stat_smooth(method = "auto", formula =y - x, se = TRUE, n = 80, fullrange = FALSE, level = 0.95) x, y | ..se.., ..x.., ..y.., ..ymin.., ..ymax.. stat_function(aes(x = -3:3), fun = dnorm, n = 101, args= list(sd =0.5)) x| ..y.. f+ stat_identity() () + stat_qq(aes(sample=l:100), distribution = qt, dparams = list(df=5)) sample, x, y | ..x.., ..y.. stat_sum() x, y, size | ..size.. stat_summary(fun.data = "mean_cl_boot") f+ stat_unique() RStudio® is a trademark of RStudio, Inc. • CC BY RStudio ■ info(Srstudio.com • 844-448-1212 ■ rstudio.com o <- a + geom_dotplot( aes(fill = ..x..)) scale_fill_gradient( low = "red", high = "yellow") scale_fill_gradient2( low= "red", hight = "blue", mid = "white", midpoint = 25) o + scale_fill_gradientn( colours = terrain. colors(6)) Also: rainbowQ, heat.colors(), topo.colorsQ, cm.colors(), RColorBrewer::brewer.pal() 4v f + geom_point( aes(shape = fl)) p +scale_shape( solid = FALSE) scale_shape_manual( values = c(3:7)) Shape values shown in chart on right Shape scales Manual shape values Ö--E9--1 O 2Ä 6 V 12ffl 11$ 16« /A 21» 221 24A 25V »0 Size scales f + geom_point( aes(size = cylj) scale_size_area(max = 6) Value mapped to area of circle (not radius) .ill Coordinate Systems r<- b + geom_bar() ' + coord_cartesian(xlim = c(0,5)) xlim, ylim The default cartesian coordinate system coord_fixed(ratio = 1/2) ratio, xlim, ylim Cartesian coordinates with fixed aspect ratio between x and y units ' + coord_flip() xlim, ylim Flipped Cartesian coordinates ' + coord_polar(theta = "x", direction=l) theta, start, direction Polar coordinates ' + coord_trans(ytrans = "sqrt") xtrans, ytrans, limx, limy Transformed cartesian coordinates. Set extras and strains to the name of a window function. ortho", coord_map(projection orientation=c(41,-74, 0)) projection, orientation, xlim, ylim Map projections from the mapproj package (mercator (default), azequalarea, lagrange, etc.] Position Adjustments Position adjustments determine how to arrange geoms that would otherwise occupy the same space, s <- ggplotfmpg, aes(fl, fill = drv)) s + geom_bar(position = "dodge") Arrange elements side by side s + geom_bar(position = "fill") Stack elements on top of one another, normalize height ar(position = "stack") Stack elements on top of one another position = "jitter") Add random noise to X and Y position of each element to avoid overplotting Each position adjustment can be recast as a function with manual width and height arguments i_bar(position = position_dodge(width = 1)) Themes LJ r + theme_bw{) White background with grid lines r + theme_grey{) Grey background (default theme) LJ Package with additional ggplot2 themes Facetin Facets divide a plot into subplots based on the values of one or more discrete variables, t <- ggplotfmpg, aesfcty, hwy)) + geom_point() t + facet_grid(. ~f[) facet into columns based on fl facet_grid(year~.) facet into rows based on year facet_grid(year~ fl) facet into both rows and columns t + facet_wrap(~ fl) wrap facets into a rectangular layout Set scales to let axis limits vary across facets <~x, scales = "free") x and y axis limits adjust to individual facets • "free_x" - x axis limits adjust • "free_y" - y axis limits adjust Set labeller to adjust facet labels fl, labeller = label_both) fl: c fl: d fl: e fl: p fl: r ■id(. - fl, labeller = label_bquote(alpha A (x))) ac ad ae op ar t + facet_grid(. - fl, labeller = label_parsed) Labels ggtitle("New Plot Title") Add a main title above the plot t + xlab("NewX label") Change the label on the X axis t + ylab("NewY label") Change the label on the Y axis : + labs(title =" New title", x = "New x", y = "New y") All of the above Use scale functions to update legend theme_classic{) White background no gridlines h theme_minimal{) Minimal theme Legends themeflegend.position = "bottom") Place legend at "bottom", "top", "left", or "right" t + guidesfcolor = "none") Set legend type for each aesthetic: colorbar, legend, or none (no legend) scale_fill_discrete(name = "Title", labels = c("A", "B", "C")) Set legend title and labels with a scale function. Zoomin Without clipping (preferred) t + coord_cartesian( xlim = c(0,100), ylim = c(10,20)) With clipping (removes unseen data points) t + xlim(0,100) + ylim(10, 20) ^ scale_x_continuous(limits = c(0,100)) + scale_y_continuous(limits = c(0,100)) Learnmoreatdocs.ggplot2.org • ggplot2 0.9.3.1 • Updated: 3/15