Geoms Data Visualization Graphical Primitives with ggplot2 with ggplot2 ggplot2 is based on the grammar Cheat Sheet of graphics, the idea that you can build every graph from the same Basics few components: a data set, a set of geoms—visual marks that represent data points, and a coordinate system. F M A 3 + F MA = 2 1 0 4 1 y, alpha, color, fill, size x, xend, y, yend, alpha, color, curvature, Two Variablesh +x,geom_bin2d(binwidth = c(5,shape, 0.5)) f + angle, geom_blank() One Variable a + geom_area(stat = "bin") linetype, size xmax, xmin, ymax, ymin, alpha, color, fill, Continuous X, Continuous Y Continuous Bivariate Distribution x, y, alpha, Continuous color, fill, linetype, size b + geom_area(aes(y = ..density..), stat = "bin") a <- ggplot(mpg, aes(hwy)) 4 0 3 1 3 4 geom_point() he<-+ggplot(movies, aes(year, rating)) h + geom_density2d() hx,+x, geom_bin2d(binwidth = c(5, 0.5)) size, stroke y, alpha, shape, y, alpha, colour, color, linetype, fill, size linetype, size, weight f <- ggplot(mpg, aes(cty, hwy)) f + geom_jitter() f x, + geom_blank() y, alpha, color, fill, shape, size b += "gaussian") geom_path(lineend="butt", a + geom_density(kernal ax,+y,geom_area(stat = "bin") linejoin="round’ , linemitre=1) alpha, color, fill, linetype, size, weight xmax, xmin, ymax, ymin, alpha, color, fill, alpha, color, fill, linetype, size bx,+y,geom_density(aes(y = ..county..)) size, weight h +linetype, geom_hex() ehx,++y,geom_density2d() geom_quantile() alpha, colour, fill size f + geom_point() x, y, alpha, color, group, linetype, size f x, + geom_jitter() y, alpha, color, fill, shape, size b + geom_area(aes(y = ..density..), stat = "bin") a+ geom_dotplot() ax,+y,geom_density(kernal = "gaussian") alpha, color, fill x, y, alpha, colour, linetype, size x, y, alpha, color, fill, shape, size x,Continuous y, alpha, color, group, linetype, size, weight Function h + geom_hex() aes(date, unemploy)) g <- ggplot(economics, b + geom_polygon(aes(group = group)) f + geom_quantile() x, y, alpha, color, fill, linetype, size, weight b + geom_density(aes(y = ..county..)) f x, + geom_point() y, alpha, color, linetype, size, weight x, y, alpha, color, fill, group, linetype, sizesize x, y, alpha, color, fill, shape, x, y, alpha, color, linetype, size x, y, alpha, color, fill b + geom_freqpoly(aes(y = ..density..)) 1 y, alpha, colour, fill size ge+x,+ geom_area() geom_rug(sides f + geom_rug(sides = "bl") a + geom_rect(aes(xmin =color, long, ymin f alpha, + geom_quantile() linetype, size = lat, a + geom_histogram(binwidth = 5) geom x=F y=A + 2 2 3 4 coordinate system 1 = + = + = 2 2 plot = "bl") + h + geom_hex() x, y, alpha, colour, fill, size Continuous Function i <- ggplot(economics, aes(date, unemploy)) i + geom_area() i + geom_line() x, y, alpha, color, group, linetype, size 1 AB C = = set of aesthetic mappings, and a default stat and position adjustment. linetype, size RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • info@rstudio.com • 844-448-1212 • rstudio.com aesthetic mappings x, y, alpha, colour, group, linetype, size x, y, alpha, color, fill, linetype, size Function x, Continuous y, alpha, color, linetype, size One Variable + h + geom_density2d() x, y, alpha, color, fill, linetype, size g <- ggplot(economics, aes(date, unemploy)) x, y,ymax alpha, color, size, weight g + geom_line() ax,+y,geom_freqpoly() xmax= long + delta_long, = latlinetype, + delta_lat) ) alpha, color, fill, linetype, size, weight f + geom_smooth(model = lm) gx,+y,geom_area() alpha, color, linetype, size x, y, alpha, color, linetype, size xmax, xmin, ymax, ymin, alpha, color, fill, linetype, size b + geom_histogram(aes(y = ..density..)) f x, + geom_rug(sides = "bl") size, weight e x,+y,geom_smooth(method = lm) y, alpha, color, fill, linetype, alpha, color, fill, linetype, size b + geom_freqpoly(aes(y = ..density..)) 0 0 alpha, color, linetype, size Discrete 1 0 1 0 3 4 3 4 g + x, geom_step(direction = "hv") 2 2 variables To display data values, map in the data set a + geom_histogram(binwidth = 5) y, alpha, color, fill, group, linetype, size, weight baes(fl)) + geom_ribbon( aes(ymin=unemploy - 900, data geom coordinate plot f + geom_text(aes(label = cty)) gx,+y,geom_line() alpha, color, linetype, size <- ggplot(mpg, 4 x, y,aalpha, color, fill, linetype, size, weight F M A x = F 4 ofsystem to aesthetic properties the geom like size, color, f x, + )geom_smooth(model = lm) y, label, alpha, angle, color, family, fontface, y=A x, y, alpha, color, linetype, size ymax=unemploy + 900) 3 3 + geom_histogram(aes(y = ..density..)) b +bgeom_bar() hjust, lineheight, vjust size, weight x, y, alpha, color,size, fill, linetype, and x and y locations.2 e + geom_text(aes(label = cty), nudge_x = 1, Visualizing error x, ymax, ymin, alpha, color, fill, group, linetype, size 2 x, alpha, color, fill, linetype, size, weight Discrete g + geom_step(direction = "hv") df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2) 1 1 f + geom_text(aes(label = cty)) nudge_y = 1, check_overlap x, y, alpha, color, linetype, size a <ggplot(mpg, aes(fl)) e <- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se)) = TRUE) F MA 04 04 a + geom_segment(aes(yend=lat + delta_lat, Discrete X, Continuous Y x, y, label, alpha, angle, color, family, fontface, 1 0 1 0 3 4 3 4 2 2 3 x, y, label, alpha, angle, color, family, fontface, 3 b + geom_bar() g <ggplot(mpg, Graphical Primitives hjust, lineheight,aes(class, size, vjust hwy)) data geom coordinate plot xend = long + delta_long)) e + geom_crossbar(fatten 2) Visualizing error=size, 2 2 x, alpha, color, fill, linetype, size, weight system x=F hjust, lineheight, vjust y=A df <- data.frame(grp = c("A", "B"), fit =fill,4:5, se = 1:2) x, y, ymax, ymin, alpha, color, linetype, 1 g +color, geom_bar(stat = "identity") x, xend, y, yend, alpha, linetype, size color = F 1 size c <ggplot(map, aes(long, lat)) size = A e <ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se)) 0 0 x,Discrete y, alpha, color, fill, linetype, size, weight X, Continuous Y 1 0 1 0 3 4 3 4 2 2 e + geom_errorbar() c + geom_polygon(aes(group = group)) g <ggplot(mpg, aes(class, hwy)) Graphical Primitives data geom coordinate plot a + geom_spoke(aes(yend = lat + delta_lat, Discrete Y ex,+ymax, geom_crossbar(fatten = 2) ymin, alpha,X, color,Continuous linetype, size, g + geom_boxplot() x, y, alpha, color, fill, linetype, size system x=F y=A (also ymin, geom_errorbarh()) x,<y, ymax, alpha, color, fill, linetype, xend = long + delta_long)) glower, + geom_bar(stat fwidth ggplot(mpg, aes(class, hwy)) middle, upper,=x,"identity") ymax, ymin, alpha, color = F c <- ggplot(map, aes(long, lat)) size = A e +size geom_linerange() color, fill, linetype, shape, size, weight x, y,color, alpha, color, fill, linetype, size, weight x, y, angle, radius, alpha, linetype, size ex,+ymin, geom_errorbar() ymax, alpha, color, linetype, size c + geom_polygon(aes(group = group)) g + geom_dotplot(binaxis = "y", g <- ggplot(economics, aes(date, unemploy)) f +x, ymax, geom_bar(stat = "identity") ymin, alpha, color, linetype, size, gstackdir + geom_boxplot() Build a graph with ggplot() or qplot() x, y, alpha, color, fill, linetype, size = "center") (also geom_errorbarh()) e +width geom_pointrange() x,lower, y, alpha, color,upper, fill x, ymax, ymin, alpha, g + geom_path(lineend="butt", middle, y, alpha, color, linetype, size, weight ex,+x, fill, linetype, shape, size, weight y,geom_linerange() ymin, ymax, alpha, color,fill, fill, linetype, linejoin="round’, linemitre=1) g +color, geom_violin(scale = "area") ggplot(data = mpg, aes(x = cty, y = hwy)) shape, x, ymin,size ymax, alpha, color, linetype, size x, y, alpha, color, linetype, size gx,+y,geom_dotplot(binaxis =size, "y", weight alpha, color, fill, linetype, g <- ggplot(economics, aes(date, unemploy)) 4 4 geom_ribbon(aes(ymin=unemploy - 900, F M A you finish stackdir = "center") Maps Begins a plot that by adding layers to. No gg+ymax=unemploy f +geom_pointrange() geom_boxplot() Continuous x, y, alpha, color, fill + 900)) + geom_path(lineend="butt", 3 3 data e<-+data.frame(murder = USArrests$Murder, defaults, but provides more control2 than qplot(). x,linejoin="round’ ymax, ymin, alpha, color, fill, linetype, size ymin, ymax, alpha, color, fill, linetype,ymax, ymin, alpha, , linemitre=1) statex,=x,y,tolower(rownames(USArrests))) gaes(hwy)) + geom_violin(scale = "area") 2 y, lower, middle, upper, Discrete X, Discrete Y c <ggplot(mpg, map <map_data("state") shape, size x, y, alpha, color, linetype, size 1 y, alpha, color, fill, linetype, weight 1 h <-x,ggplot(diamonds, aes(cut, size, color)) color, fill, group, linetype, shape, size, weight e <- ggplot(data, aes(fill = murder)) dataF M A g + geom_ribbon(aes(ymin=unemploy - 900, 04 04 add layers, d<Maps = state), map = map) + e + geom_map(aes(map_id c + geom_area(stat = "bin") 1 0 1 0 3 4 3 4 2 2 ggplot(seals, aes(x = long, y = lat)) h + geom_jitter() ymax=unemploy + 900) ) 3 dataf<-+data.frame(murder = USArrests$Murder, = "y", geom_dotplot(binaxis elements with + x, ymax, ymin, alpha, color, fill, linetype, size data geom 3 coordinate plot expand_limits( x = map$long, y = map$lat) state = tolower(rownames(USArrests))) x, y,Discrete alpha, color, shape, size ggplot(mpg, aes(hwy, cty)) 2 x=F X,fill, Discrete Y system + 2 size d + geom_segment(aes( x, y, alpha, color, fill, linetype, alpha, color, fill, linetype, size mapmap_id, <-stackdir map_data("state") y=A = "center") 1 h <- ggplot(diamonds, aes(cut, color)) geom_point(aes(color = cyl)) 1 + layer = geom + xend = long + delta_long, e <- ggplot(data, aes(fill = murder)) = lat + delta_lat)) a + geom_area(aes(y = ..density..), stat = "bin") stat + d<-yend geom_smooth(method 0 ="lm") + 0 0 1 2 default x, y, alpha, color,= state), fill, map group e + geom_map( aes(map_id = map) + Three Variables 1 0 3 4 3 4 2 ggplot(seals, aes(x = long, y = lat)) h + geom_jitter() data geom + coordinate plotlayer specific x, xend, y, yend, alpha, color, linetype, size coord_cartesian() expand_limits( x = map$long, y = map$lat) x, y, alpha, color, fill, shape, size+ delta_lat^2)) i + geom_raster(aes(fill = z), hjust=0.5, seals$z <- with(seals, sqrt(delta_long^2 f + geom_violin(scale = "area") x=F system geom_segment(aes( mappings dd++geom_rect(aes(xmin c =+long, geom_density(kernel = "gaussian") map_id, alpha, color, fill, linetype, size ymin = lat, y=A scale_color_gradient() + i <- ggplot(seals, aes(long, lat)) vjust=0.5, interpolate=FALSE) xend =long long++delta_long, delta_long, xmax= x, y, alpha, color, fill, group, linetype, size, theme_bw() yend == lat lat ++ delta_lat)) delta_lat)) x, y, alpha, color, fill, group, linetype, size, weight additional ymax Three Variables x, y, alpha, fill i + geom_contour(aes(z = z)) weight elements i + geom_tile(aes(fill = z)) x, xend, y, yend, alpha, color, linetype, size xmax, xmin, ymax, ymin, alpha, color, fill, i +x, geom_raster(aes(fill = z),size hjust=0.5, seals$z <-x,with(seals, + delta_lat^2)) y, z, alpha, sqrt(delta_long^2 colour, linetype, size, weight y, alpha, color, fill, linetype, size dlinetype, + geom_rect(aes(xmin = long, ymin = lat, c + geom_dotplot() i <- ggplot(seals, aes(long, lat)) vjust=0.5, interpolate=FALSE) Add a new layer to a plot with a geom_*() xmax= long + delta_long, x, y, alpha, fill = lat + delta_lat)) x, y, alpha, color, fill RStudio® is a function. trademark of RStudio, Inc. • provides CC BY RStudio • info@rstudio.com • rstudio.com Learn more at docs.ggplot2.org • ggplot2 • Updated: 3/15Y Discrete X, 0.9.3.1 Discrete or stat_*() Each a geom, a• 844-448-1212ymax i + geom_contour(aes(z = z)) i + geom_tile(aes(fill = z)) xmax, xmin, ymax, ymin, alpha, color, fill, data x, y, alpha, color, fill, linetype, size, weight a <- ggplot(mpg, aes(hwy)) a + geom_freqpoly() a+ geom_dotplot() 2 0 0 3 a + geom_curve(aes(yend = lat + delta_lat, Continuous X, Continuous Y Continuous Bivariate Distribution= 2, width = 2) erepresent +ggplot(movies, geom_jitter(height Geomsxend - Use=a geom data points, use the geom’s aesthetic properties to variables longto+represent delta_long, curvature =hwy)) z)) h <aes(year, rating)) f <- ggplot(mpg, aes(cty, h + geom_bin2d(binwidth = c(0.25, 500)) Continuous 4 3 Continuous Bivariate Distribution h <- ggplot(diamonds, aes(carat, price)) A C e + geom_label(aes(label = cty), nudge_x = 1, nudge_y = 1, check_overlap = TRUE) B Geoms(Useful - Use a geom to represent data points, use the geom’s aesthetic properties to represent variables for expanding limits) x, y, label, alpha, angle, color, family, fontface, One Variable Two Variables hjust, lineheight, size, vjust with ggplot2 Cheat Sheet Data Visualization Basics Continuous X, Continuous Y e <- ggplot(mpg, aes(cty, hwy)) a + geom_blank() Data Visualization Basics Two Variables a <- ggplot(seals, aes(x = long, y = lat)) b <- ggplot(economics, aes(date, unemploy)) Cheat Sheet 4 - Use a geom to represent data points, use the geom’s aesthetic properties to represent variables. Each function returns a layer. data geom qplot(x = cty, y = hwy, color = cyl, data = mpg, geom = "point") Creates a complete plot with given data, geom, and mappings. Supplies many useful defaults. last_plot() Returns the last plot ggsave("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. c + geom_freqpoly() x, y, z, alpha, colour, linetype, size, weight x, y, alpha, color, group, linetype, size a + geom_freqpoly(aes(y = ..density..)) g <-x, ggplot(diamonds, y, alpha, color, fill, linetype, size aes(cut, color)) g + geom_count() Learn more at docs.ggplot2.org • ggplot2 0.9.3.1 • Updated: 3/15 x, y, alpha, color, fill, shape, size, stroke c + geom_histogram(binwidth = 5) x, y, alpha, color, fill, linetype, size, weight a + geom_histogram(aes(y = ..density..)) Discrete d <- ggplot(mpg, aes(fl)) d + geom_bar() x, alpha, color, fill, linetype, size, weight RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • info@rstudio.com • 844-448-1212 • rstudio.com i + geom_step(direction = "hv") x, y, alpha, color, group, linetype, size Visualizing error df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2) j <- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se)) j + geom_crossbar(fatten = 2) x, y, ymax, ymin, alpha, color, fill, group, linetype, size j + geom_errorbar() x, ymax, ymin, alpha, color, group, linetype, size, width (also geom_errorbarh()) j + geom_linerange() x, ymin, ymax, alpha, color, group, linetype, size j + geom_pointrange() x, y, ymin, ymax, alpha, color, fill, group, linetype, shape, size Maps data <- data.frame(murder = USArrests$Murder, state = tolower(rownames(USArrests))) map <- map_data("state") k <- ggplot(data, aes(fill = murder)) k + geom_map(aes(map_id = state), map = map) + expand_limits(x = map$long, y = map$lat) map_id, alpha, color, fill, linetype, size Three Variables seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2)) l <- ggplot(seals, aes(long, lat)) l + geom_contour(aes(z = z)) x, y, z, alpha, colour, group, linetype, size, weight l + geom_raster(aes(fill = z), hjust=0.5, vjust=0.5, interpolate=FALSE) x, y, alpha, fill l + geom_tile(aes(fill = z)) x, y, alpha, color, fill, linetype, size, width Learn more at docs.ggplot2.org • ggplot2 2.0.0 • Updated: 12/15 Stats - An alternative way to build a layer Scales Some plots visualize a transformation of the original data set. Use a stat to choose a common transformation to visualize, e.g. a + geom_bar(stat = "count") Scales control how a plot maps data values to the visual values of an aesthetic. To change the mapping, add a custom scale. = 2 1 stat geom 2 1 scale_ 0 0 data n <- b + geom_bar(aes(fill = fl)) n 0 1 2 3 4 coordinate x=x system y = ..count.. 0 1 2 plot 3 4 Each stat creates additional variables to map aesthetics to. These variables use a common ..name.. syntax. stat and geom functions both combine a stat with a geom to make a layer, i.e. stat_count(geom="bar") does the same as geom_bar(stat="count") stat function layer mappings i + stat_density2d(aes(fill = ..level..), geom = "polygon", n = 100) variable geom for layer parameters for stat created by transformation c + stat_bin(binwidth = 1, origin = 10) 1D distributions x, y | ..count.., ..ncount.., ..density.., ..ndensity.. c + stat_count(width = 1) x, y, | ..count.., ..prop.. c + stat_density(adjust = 1, kernel = "gaussian") x, y, | ..count.., ..density.., ..scaled.. e + stat_bin_2d(bins = 30, drop = TRUE) 2D distributions x, y, fill | ..count.., ..density.. e + stat_bin_hex(bins = 30) x, y, fill | ..count.., ..density.. e + stat_density_2d(contour = TRUE, n = 100) x, y, color, size | ..level.. e + stat_ellipse(level = 0.95, segments = 51, type = "t") l + stat_contour(aes(z = z)) 3 Variables x, y, z, order | ..level.. l + stat_summary_hex(aes(z = z), bins = 30, fun = mean) x, y, z, fill | ..value.. l + stat_summary_2d(aes(z = z), bins = 30, fun = mean) x, y, z, fill | ..value.. f + stat_boxplot(coef = 1.5) Comparisons x, y | ..lower.., ..middle.., ..upper.., ..width.. , ..ymin.., ..ymax.. f + stat_ydensity(adjust = 1, kernel = "gaussian", scale = "area") x, y | ..density.., ..scaled.., ..count.., ..n.., ..violinwidth.., ..width.. e + stat_ecdf(n = 40) Functions x, y | ..x.., ..y.. e + stat_quantile(quantiles = c(0.25, 0.5, 0.75), formula = y ~ log(x), method = "rq") x, y | ..quantile.. e + stat_smooth(method = "auto", formula = y ~ x, se = TRUE, n = 80, fullrange = FALSE, level = 0.95) x, y | ..se.., ..x.., ..y.., ..ymin.., ..ymax.. ggplot() + stat_function(aes(x = -3:3), General Purpose fun = dnorm, n = 101, args = list(sd=0.5)) x | ..x.., ..y.. e + stat_identity(na.rm = TRUE) ggplot() + stat_qq(aes(sample=1:100), distribution = qt, dparams = list(df=5)) sample, x, y | ..sample.., ..theoretical.. e + stat_sum() x, y, size | ..n.., ..prop.. e + stat_summary(fun.data = "mean_cl_boot") h + stat_summary_bin(fun.y = "mean", geom = "bar") e + stat_unique() aesthetic prepackaged to adjust scale to use title to use in legend/axis labels to use in legend/axis xlim, ylim Flipped Cartesian coordinates breaks to use in legend/axis r + coord_polar(theta = "x", direction=1 ) theta, start, direction Polar coordinates Use with any aesthetic: alpha, color, fill, linetype, shape, size r + coord_trans(ytrans = "sqrt") xtrans, ytrans, limx, limy Transformed cartesian coordinates. Set xtrans and ytrans to the name of a window function. 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 π + coord_map(projection = "ortho", orientation=c(41, -74, 0)) 60 Use with x or y aesthetics (x shown here) scale_x_date(date_labels = "%m/%d"), date_breaks = "2 weeks") - treat x values as dates. See ?strptime for label formats. scale_x_datetime() - treat x values as date times. Use same arguments as scale_x_date(). scale_x_log10() - Plot x on log10 scale scale_x_reverse() - Reverse direction of x axis scale_x_sqrt() - Plot x on square root scale Position Adjustments Position adjustments determine how to arrange geoms that would otherwise occupy the same space. s <- ggplot(mpg, aes(fl, fill = drv)) s + geom_bar(position = "dodge") Arrange elements side by side Color and fill scales s + geom_bar(position = "fill") Continuous o <- c + geom_dotplot( aes(fill = ..x..)) n + scale_fill_brewer( palette = "Blues") For palette choices: library(RColorBrewer) display.brewer.all() o + scale_fill_gradient( low = "red", high = "yellow") o + scale_fill_gradient2( low = "red", high = "blue", mid = "white", midpoint = 25) n + scale_fill_grey( start = 0.2, end = 0.8, na.value = "red") Stack elements on top of one another, normalize height e + geom_point(position = "jitter") A B s + geom_bar(position = position_dodge(width = 1)) Manual Shape values 0 6 12 18 24 0 p + scale_shape( solid = FALSE) 1 7 13 19 25 + 2 8 14 20 * - 3 9 15 21 . 10 5 11 16 17 22 23 0 + * | o % O # o O Themes r + theme_bw() 150 | 100 50 c % d e p r fl r + theme_gray() 150 # 100 50 c d e p Grey background (default theme) r fl Size scales RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • info@rstudio.com • 844-448-1212 • rstudio.com p + scale_size_area( max_scale = 6) Maps to area of circle (not radius) t + facet_grid(year ~ .) facet into rows based on year t + facet_grid(year ~ fl) facet into both rows and columns 150 r + theme_dark() 100 50 0 c d e fl p r dark for contrast 100 50 0 c d e p r fl 150 100 50 0 c d e fl p r t + facet_wrap(~ fl) wrap facets into a rectangular layout Set scales to let axis limits vary across facets t + facet_grid(drv ~ fl, 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 t + facet_grid(. ~ fl, labeller = label_both) fl: d fl: e fl: p fl: r t + facet_grid(fl ~ ., labeller = label_bquote(alpha ^ .(fl))) ↵c ↵e ↵p ↵r ↵d t + facet_grid(. ~ fl, labeller = label_parsed) c d e p r Labels t + ggtitle("New Plot Title") Add a main title above the plot t + xlab("New X label") Change the label on the X axis t + ylab("New Y label") Use scale functions to update legend labels Change the label on the Y axis t + labs(title =" New title", x = "New x", y = "New y") All of the above Legends n + theme(legend.position = "bottom") Place legend at "bottom", "top", "left", or "right" n + guides(fill = "none") Set legend type for each aesthetic: colorbar, legend, or none (no legend) n + scale_fill_discrete(name = "Title", labels = c("A", "B", "C", "D", "E")) Set legend title and labels with a scale function. Zooming r + theme_classic() r + theme_light() r + theme_linedraw() r + theme_minimal() 150 White background with grid lines 0 0 p + scale_radius( range=c(1,6)) p + scale_size() Nudge labels away from points Each position adjustment can be recast as a function with manual width and height arguments Manual shape values 4 e + geom_label(position = "nudge") Stack elements on top of one another p <- e + geom_point(aes( shape = fl, size = cyl)) p + scale_shape_manual( values = c(3:7)) Shape values shown in chart on right Add random noise to X and Y position of each element to avoid overplotting s + geom_bar(position = "stack") o + scale_fill_gradientn( colours = terrain.colors(6)) Also: rainbow(), heat.colors(), topo.colors(), cm.colors(), RColorBrewer::brewer.pal() Shape scales facet into columns based on fl fl: c projection, orientation, xlim, ylim Map projections from the mapproj package long (mercator (default), azequalarea, lagrange, etc.) X and Y location scales n <- d + geom_bar( aes(fill = fl)) t + facet_grid(. ~ fl) r + coord_flip() General Purpose scales Discrete t <- ggplot(mpg, aes(cty, hwy)) + geom_point() ratio, xlim, ylim Cartesian coordinates with fixed aspect ratio between x and y units n + scale_fill_manual( values = c("skyblue", "royalblue", "blue", "navy"), limits = c("d", "e", "p", "r"), breaks =c("d", "e", "p", "r"), name = "fuel", labels = c("D", "E", "P", "R")) range of values to include in mapping Facets divide a plot into subplots based on the values of one or more discrete variables. r + coord_fixed(ratio = 1/2) scale specific arguments lat 3 count 3 count + 4 xlim, ylim The default cartesian coordinate system count ..count.. r + coord_cartesian(xlim = c(0, 5)) count x r <- d + geom_bar() count fl cty cyl 4 Faceting Coordinate Systems Minimal themes r + theme_void() Empty theme 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) t + scale_x_continuous(limits = c(0, 100)) + scale_y_continuous(limits = c(0, 100)) Learn more at docs.ggplot2.org • ggplot2 2.0.0 • Updated: 12/15