

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Basics, Geoms, Stats, Scales, Coordinate Systems, Faceting, Position Adjustments in ggplot2 using R programming
Typology: Cheat Sheet
1 / 3
This page cannot be seen from the preview
Don't miss anything!
ggplot2 is based on the grammar of graphics, the idea that you can build every graph from the same components: a data set, a coordinate system, and geoms—visual marks that represent data points.
a + geom_blank() (Useful for expanding limits) b + geom_curve(aes(yend = lat + 1, xend=long+1,curvature=z)) - x, xend, y, yend, alpha, angle, color, curvature, linetype, size a + geom_path(lineend="butt", linejoin="round", linemitre=1) x, y, alpha, color, group, linetype, size a + geom_polygon(aes(group = group)) x, y, alpha, color, fill, group, linetype, size b + geom_rect(aes(xmin = long, ymin=lat, xmax= long + 1, ymax = lat + 1)) - xmax, xmin, ymax, ymin, alpha, color, fill, linetype, size
To display values, map variables in the data to visual properties of the geom (aesthetics) like size, color, and x and y locations.
data geom x = F · y = A coordinate system plot data geom x = F · y = A color = F size = A coordinate system plot Complete the template below to build a graph. required ggplot(data = mpg, aes(x = cty, y = hwy)) Begins a plot that you finish by adding layers to. Add one geom function per layer. qplot(x = cty, y = hwy, 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. F M A F M A aesthetic mappings (^) data geom
b + geom_abline(aes(intercept=0, slope=1)) b + geom_hline(aes(yintercept = lat)) b + geom_vline(aes(xintercept = long)) common aesthetics: x, y, alpha, color, linetype, size b + geom_segment(aes(yend=lat+1, xend=long+1)) b + geom_spoke(aes(angle = 1:1155, radius = 1)) a <- ggplot(economics, aes(date, unemploy)) b <- ggplot(seals, aes(x = long, y = lat))
c <- ggplot(mpg, aes(hwy)); c2 <- ggplot(mpg) c + geom_area(stat = "bin") x, y, alpha, color, fill, linetype, size c + geom_density(kernel = "gaussian") x, y, alpha, color, fill, group, linetype, size, weight c + geom_dotplot() x, y, alpha, color, fill c + geom_freqpoly() x, y, alpha, color, group, linetype, size c + geom_histogram(binwidth = 5) x, y, alpha, color, fill, linetype, size, weight c2 + geom_qq(aes(sample = hwy)) x, y, alpha, color, fill, linetype, size, weight
d <- ggplot(mpg, aes(fl)) d + geom_bar() x, alpha, color, fill, linetype, size, weight e + geom_label(aes(label = cty), nudge_x = 1, nudge_y = 1, check_overlap = TRUE) x, y, label, alpha, angle, color, family, fontface, hjust, lineheight, size, vjust e + geom_jitter(height = 2, width = 2) x, y, alpha, color, fill, shape, size e + geom_point(), x, y, alpha, color, fill, shape, size, stroke e + geom_quantile(), x, y, alpha, color, group, linetype, size, weight e + geom_rug(sides = "bl"), x, y, alpha, color, linetype, size e + geom_smooth(method = lm), x, y, alpha, color, fill, group, linetype, size, weight e + geom_text(aes(label = cty), nudge_x = 1, nudge_y = 1, check_overlap = TRUE), x, y, label, alpha, angle, color, family, fontface, hjust, lineheight, size, vjust
f <- ggplot(mpg, aes(class, hwy)) f + geom_col(), x, y, alpha, color, fill, group, linetype, size f + geom_boxplot(), x, y, lower, middle, upper, ymax, ymin, alpha, color, fill, group, linetype, shape, size, weight f + geom_dotplot(binaxis = "y", stackdir = "center"), x, y, alpha, color, fill, group f + geom_violin(scale = "area"), x, y, alpha, color, fill, group, linetype, size, weight
g <- ggplot(diamonds, aes(cut, color)) g + geom_count(), x, y, alpha, color, fill, shape, size, stroke
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 h + geom_bin2d(binwidth = c(0.25, 500)) x, y, alpha, color, fill, linetype, size, weight h + geom_density2d() x, y, alpha, colour, group, linetype, size h + geom_hex() x, y, alpha, colour, fill, size i + geom_area() x, y, alpha, color, fill, linetype, size i + geom_line() x, y, alpha, color, group, linetype, size i + geom_step(direction = "hv") x, y, alpha, color, group, linetype, size 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
i <- ggplot(economics, aes(date, unemploy))
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)
e <- ggplot(mpg, aes(cty, hwy))
h <- ggplot(diamonds, aes(carat, price)) RStudio® is a trademark of RStudio, Inc. • CC BY SA RStudio • info@rstudio.com • 844-448-1212 • rstudio.com • Learn more at http://ggplot2.tidyverse.org • ggplot2 3.1.0 • Updated: 2018- ggplot (data = ) + <GEOM_FUNCTION> (mapping = aes(
Scales Coordinate Systems A stat builds new variables to plot (e.g., count, prop). Stats An alternative way to build a layer
Use with most aesthetics scale_continuous() - map cont’ values to visual ones scalediscrete() - map discrete values to visual ones scaleidentity() - use data values as visual ones scalemanual(values = c()) - map discrete values to manually chosen visual ones scaledate(date_labels = "%m/%d"), date_breaks = " weeks") - treat data values as dates. scale_datetime() - treat data x values as date times. Use same arguments as scale_x_date(). See ?strptime for label formats.
Use with x or y aesthetics (x shown here) 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
n <- d + geom_bar(aes(fill = fl)) n + scale_fill_brewer(palette = "Blues") For palette choices: RColorBrewer::display.brewer.all() n + scale_fill_grey(start = 0.2, end = 0.8, na.value = "red")
o <- c + geom_dotplot(aes(fill = ..x..)) o + scale_fill_distiller(palette = "Blues") o + scale_fill_gradient(low="red", high="yellow") o + scale_fill_gradient2(low="red", high=“blue", mid = "white", midpoint = 25) o + scale_fill_gradientn(colours=topo.colors(6)) Also: rainbow(), heat.colors(), terrain.colors(), cm.colors(), RColorBrewer::brewer.pal()
p <- e + geom_point(aes(shape = fl, size = cyl)) p + scale_shape() + scale_size() p + scale_shape_manual(values = c(3:7)) p + scale_radius(range = c(1,6)) p + scale_size_area(max_size = 6) r <- d + geom_bar() r + coord_cartesian(xlim = c(0, 5)) xlim, ylim The default cartesian coordinate system r + coord_fixed(ratio = 1/2) ratio, xlim, ylim Cartesian coordinates with fixed aspect ratio between x and y units r + coord_flip() xlim, ylim Flipped Cartesian coordinates r + coord_polar(theta = "x", direction=1 ) theta, start, direction Polar coordinates r + coord_trans(ytrans = “sqrt") xtrans, ytrans, limx, limy Transformed cartesian coordinates. Set xtrans and ytrans to the name of a window function. π + coord_quickmap() π + coord_map(projection = "ortho", orientation=c(41, -74, 0))projection, orienztation, 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 <- ggplot(mpg, 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 e + geom_point(position = "jitter") Add random noise to X and Y position of each element to avoid overplotting e + geom_label(position = "nudge") Nudge labels away from points s + geom_bar(position = "stack") Stack elements on top of one another Each position adjustment can be recast as a function with manual width and height arguments s + geom_bar(position = position_dodge(width = 1)) A B Themes r + theme_bw() White background with grid lines r + theme_gray() Grey background (default theme) r + theme_dark() dark for contrast r + theme_classic() r + theme_light() r + theme_linedraw() r + theme_minimal() Minimal themes r + theme_void() Empty theme Faceting Facets divide a plot into subplots based on the values of one or more discrete variables. t <- ggplot(mpg, aes(cty, hwy)) + geom_point() t + facet_grid(cols = vars(fl)) facet into columns based on fl t + facet_grid(rows = vars(year)) facet into rows based on year t + facet_grid(rows = vars(year), cols = vars(fl)) facet into both rows and columns t + facet_wrap(vars(fl)) wrap facets into a rectangular layout Set scales to let axis limits vary across facets t + facet_grid(rows = vars(drv), cols = vars(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(cols = vars(fl), labeller = label_both) t + facet_grid(rows = vars(fl), labeller = label_bquote(alpha ^ .(fl))) fl: c fl: d fl: e fl: p fl: r ↵c^ ↵d^ ↵e^ ↵p^ ↵r Labels t + labs( x = "New x axis label", y = "New y axis label", title ="Add a title above the plot", subtitle = "Add a subtitle below title", caption = "Add a caption below plot",