

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
R color cheat sheet or ggplot2 function in R language. Explore data visualization with grammar of graphics function in R base
Typology: Cheat Sheet
1 / 2
This page cannot be seen from the preview
Don't miss anything!
x, y, z, alpha, colour, group, linetype, size, weight seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2)) l <- ggplot(seals, aes(long, lat))
x, y, alpha, fill
x, y, alpha, color, fill, linetype, size, width
x, y, alpha, color, fill, shape, size, stroke
x, y, alpha, color, fill, group, linetype, size
x, y, lower, middle, upper, ymax, ymin, alpha, color, fill, group, linetype, shape, size, weight
x, y, alpha, color, fill, group
x, y, alpha, color, fill, group, linetype, size, weight
x, y, label, alpha, angle, color, family, fontface, hjust, lineheight, size, vjust
x, y, alpha, color, fill, shape, size
x, y, alpha, color, fill, shape, size, stroke
x, y, alpha, color, group, linetype, size, weight
x, y, alpha, color, linetype, size
x, y, alpha, color, fill, group, linetype, size, weight
x, y, label, alpha, angle, color, family, fontface, hjust, lineheight, size, vjust
x, y, alpha, color, fill, linetype, size
x, y, alpha, color, group, linetype, size
x, y, alpha, color, group, linetype, size
x, y, ymax, ymin, alpha, color, fill, group, linetype, size
x, ymax, ymin, alpha, color, group, linetype, size, width (also geom_errorbarh())
x, ymin, ymax, alpha, color, group, linetype, size
x, y, ymin, ymax, alpha, color, fill, group, linetype, shape, size
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)) data <- data.frame(murder = USArrests$Murder, state = tolower(rownames(USArrests))) map <- map_data("state") k <- ggplot(data, aes(fill = murder))
map_id, alpha, color, fill, linetype, size
x, y, alpha, color, fill, linetype, size, weight
x, y, alpha, colour, group, linetype, size
x, y, alpha, colour, fill, size
RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • info@rstudio.com • 844-448-1212 • rstudio.com Learn more at docs.ggplot2.org and www.ggplot2-exts.org • ggplot2 2.1.0 • Updated: 11/1 6 ggplot(data = mpg, aes(x = cty, y = hwy)) Begins a plot that you finish by adding layers to. Add one geom function per layer.
Complete the template below to build a graph. 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. To display values, map variables in the data to visual properties of the geom (aesthetics) like size, color, and x and y locations. Graphical Primitives
RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • info@rstudio.com • 844-448-1212 • rstudio.com Learn more at docs.ggplot2.org • ggplot2 0.9.3.1 • Updated: 3/1 5 Geoms - Use a geom to represent data points, use the geom’s aesthetic properties to represent variables Basics One Variable a + geom_area(stat = "bin") x, y, alpha, color, fill, linetype, size b + geom_area(aes(y = ..density..), stat = "bin") a + geom_density( kernal = "gaussian" ) x, y, alpha, color, fill, linetype, size, weight b + geom_density(aes(y = ..county..)) a+ geom_dotplot() x, y, alpha, color, fill a + geom_freqpoly() x, y, alpha, color, linetype, size b + geom_freqpoly(aes(y = ..density..)) a + geom_histogram( binwidth = 5 ) x, y, alpha, color, fill, linetype, size, weight b + geom_histogram(aes(y = ..density..)) Discrete a <- ggplot(mpg, aes(fl)) b + geom_bar() x, alpha, color, fill, linetype, size, weight Continuous a <- ggplot(mpg, aes(hwy)) Two Variables Discrete X, Discrete Y h <- ggplot(diamonds, aes(cut, color)) h + geom_jitter() x, y, alpha, color, fill, shape, size Discrete X, Continuous Y g <- ggplot(mpg, aes(class, hwy)) g + geom_bar(stat = "identity") x, y, alpha, color, fill, linetype, size, weight g + geom_boxplot() lower, middle, upper, x, ymax, ymin, alpha, color, fill, linetype, shape, size, weight g + geom_dotplot( binaxis = "y", stackdir = "center" ) x, y, alpha, color, fill g + geom_violin( scale = "area" ) x, y, alpha, color, fill, linetype, size, weight Continuous X, Continuous Y f <- ggplot(mpg, aes(cty, hwy)) f + geom_blank() f + geom_jitter() x, y, alpha, color, fill, shape, size f + geom_point() x, y, alpha, color, fill, shape, size f + geom_quantile() x, y, alpha, color, linetype, size, weight f + geom_rug( sides = "bl" ) alpha, color, linetype, size f + geom_smooth( model = lm ) x, y, alpha, color, fill, linetype, size, weight f + geom_text( aes(label = cty) ) x, y, label, alpha, angle, color, family, fontface, hjust, lineheight, size, vjust Three Variables i + geom_contour( aes(z = z) ) x, y, z, alpha, colour, linetype, size, weight seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2)) i <- ggplot(seals, aes(long, lat)) g <- ggplot(economics, aes(date, unemploy)) Continuous Function g + geom_area() x, y, alpha, color, fill, linetype, size g + geom_line() x, y, alpha, color, linetype, size g + geom_step( direction = "hv" ) x, y, alpha, color, linetype, size Continuous Bivariate Distribution h <- ggplot(movies, aes(year, rating)) h + geom_bin2d( binwidth = c(5, 0.5) ) xmax, xmin, ymax, ymin, alpha, color, fill, linetype, size, weight h + geom_density2d() x, y, alpha, colour, linetype, size h + geom_hex() x, y, alpha, colour, fill size d + geom_segment( aes( xend = long + delta_long, yend = lat + delta_lat) ) x, xend, y, yend, alpha, color, linetype, size d + geom_rect( aes(xmin = long, ymin = lat, xmax= long + delta_long, ymax = lat + delta_lat) ) xmax, xmin, ymax, ymin, alpha, color, fill, linetype, size c + geom_polygon( aes(group = group) ) x, y, alpha, color, fill, linetype, size d<- ggplot(seals, aes(x = long, y = lat)) i + geom_raster( aes(fill = z), hjust=0.5, vjust=0.5, interpolate=FALSE ) x, y, alpha, fill i + geom_tile( aes(fill = z) ) x, y, alpha, color, fill, linetype, size e + geom_crossbar( fatten = 2 ) x, y, ymax, ymin, alpha, color, fill, linetype, size e + geom_errorbar() x, ymax, ymin, alpha, color, linetype, size, width (also geom_errorbarh() ) e + geom_linerange() x, ymin, ymax, alpha, color, linetype, size e + geom_pointrange() x, y, ymin, ymax, alpha, color, fill, linetype, shape, size Visualizing error df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2) e <- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se)) g + geom_path( lineend="butt", linejoin="round’, linemitre=1 ) x, y, alpha, color, linetype, size g + geom_ribbon( aes(ymin=unemploy - 900, ymax=unemploy + 900) ) x, ymax, ymin, alpha, color, fill, linetype, size g <- ggplot(economics, aes(date, unemploy)) c <- ggplot(map, aes(long, lat)) data <- data.frame(murder = USArrests$Murder, state = tolower(rownames(USArrests))) map <- map_data("state") e <- ggplot(data, aes(fill = murder)) e + geom_map( aes(map_id = state), map = map ) + expand_limits( x = map$long, y = map$lat ) map_id, alpha, color, fill, linetype, size Maps F M A = 12 3 (^00 1 2 3 ) 4 1 2 3 (^00 1 2 3 ) 4
data geom coordinate system plot
F M A = 12 3 (^00 1 2 3 ) 4 1 2 3 (^00 1 2 3 ) 4 data geom coordinate system plot x = F y = A color = F size = A 1 2 3 (^00 1 2 3 ) 4 plot
1 2 3 (^00 1 2 3 ) 4 data geom coordinate x = F y = A system x = F y = A Graphical Primitives
RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • info@rstudio.com • 844-448-1212 • rstudio.com Learn more at docs.ggplot2.org • ggplot2 0.9.3.1 • Updated: 3/1 5 Geoms - Use a geom to represent data points, use the geom’s aesthetic properties to represent variables Basics One Variable a + geom_area(stat = "bin") x, y, alpha, color, fill, linetype, size b + geom_area(aes(y = ..density..), stat = "bin") a + geom_density( kernal = "gaussian" ) x, y, alpha, color, fill, linetype, size, weight b + geom_density(aes(y = ..county..)) a+ geom_dotplot() x, y, alpha, color, fill a + geom_freqpoly() x, y, alpha, color, linetype, size b + geom_freqpoly(aes(y = ..density..)) a + geom_histogram( binwidth = 5 ) x, y, alpha, color, fill, linetype, size, weight b + geom_histogram(aes(y = ..density..)) Discrete a <- ggplot(mpg, aes(fl)) b + geom_bar() x, alpha, color, fill, linetype, size, weight Continuous a <- ggplot(mpg, aes(hwy)) Two Variables Discrete X, Discrete Y h <- ggplot(diamonds, aes(cut, color)) h + geom_jitter() x, y, alpha, color, fill, shape, size Discrete X, Continuous Y g <- ggplot(mpg, aes(class, hwy)) g + geom_bar(stat = "identity") x, y, alpha, color, fill, linetype, size, weight g + geom_boxplot() lower, middle, upper, x, ymax, ymin, alpha, color, fill, linetype, shape, size, weight g + geom_dotplot( binaxis = "y", stackdir = "center" ) x, y, alpha, color, fill g + geom_violin( scale = "area" ) x, y, alpha, color, fill, linetype, size, weight Continuous X, Continuous Y f <- ggplot(mpg, aes(cty, hwy)) f + geom_blank() f + geom_jitter() x, y, alpha, color, fill, shape, size f + geom_point() x, y, alpha, color, fill, shape, size f + geom_quantile() x, y, alpha, color, linetype, size, weight f + geom_rug( sides = "bl" ) alpha, color, linetype, size f + geom_smooth( model = lm ) x, y, alpha, color, fill, linetype, size, weight f + geom_text( aes(label = cty) ) x, y, label, alpha, angle, color, family, fontface, hjust, lineheight, size, vjust Three Variables i + geom_contour( aes(z = z) ) x, y, z, alpha, colour, linetype, size, weight seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2)) i <- ggplot(seals, aes(long, lat)) g <- ggplot(economics, aes(date, unemploy)) Continuous Function g + geom_area() x, y, alpha, color, fill, linetype, size g + geom_line() x, y, alpha, color, linetype, size g + geom_step( direction = "hv" ) x, y, alpha, color, linetype, size Continuous Bivariate Distribution h <- ggplot(movies, aes(year, rating)) h + geom_bin2d( binwidth = c(5, 0.5) ) xmax, xmin, ymax, ymin, alpha, color, fill, linetype, size, weight h + geom_density2d() x, y, alpha, colour, linetype, size h + geom_hex() x, y, alpha, colour, fill size d + geom_segment( aes( xend = long + delta_long, yend = lat + delta_lat) ) x, xend, y, yend, alpha, color, linetype, size d + geom_rect( aes(xmin = long, ymin = lat, xmax= long + delta_long, ymax = lat + delta_lat) ) xmax, xmin, ymax, ymin, alpha, color, fill, linetype, size c + geom_polygon( aes(group = group) ) x, y, alpha, color, fill, linetype, size d<- ggplot(seals, aes(x = long, y = lat)) i + geom_raster( aes(fill = z), hjust=0.5, vjust=0.5, interpolate=FALSE ) x, y, alpha, fill i + geom_tile( aes(fill = z) ) x, y, alpha, color, fill, linetype, size e + geom_crossbar( fatten = 2 ) x, y, ymax, ymin, alpha, color, fill, linetype, size e + geom_errorbar() x, ymax, ymin, alpha, color, linetype, size, width (also geom_errorbarh() ) e + geom_linerange() x, ymin, ymax, alpha, color, linetype, size e + geom_pointrange() x, y, ymin, ymax, alpha, color, fill, linetype, shape, size Visualizing error df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2) e <- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se)) g + geom_path( lineend="butt", linejoin="round’, linemitre=1 ) x, y, alpha, color, linetype, size g + geom_ribbon( aes(ymin=unemploy - 900, ymax=unemploy + 900) ) x, ymax, ymin, alpha, color, fill, linetype, size g <- ggplot(economics, aes(date, unemploy)) c <- ggplot(map, aes(long, lat)) data <- data.frame(murder = USArrests$Murder, state = tolower(rownames(USArrests))) map <- map_data("state") e <- ggplot(data, aes(fill = murder)) e + geom_map( aes(map_id = state), map = map ) + expand_limits( x = map$long, y = map$lat ) map_id, alpha, color, fill, linetype, size Maps F M A = 12 3 (^00 1 2 3 ) 4 1 2 3 (^00 1 2 3 ) 4
data geom coordinate system plot
F M A = 12 3 (^00 1 2 3 ) 4 1 2 3 (^00 1 2 3 ) 4 data geom coordinate system plot x = F y = A color = F size = A 1 2 3 (^00 1 2 3 ) 4 plot
F M A 12 = 3 (^00 1 2 3 ) 4 data geom coordinate x = F y = A system x = F y = A 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.
Creates a complete plot with given data, geom, and mappings. Supplies many useful defaults. aesthetic mappings data geom last_plot() Returns the last plot ggplot(data = ) + <GEOM_FUNCTION> ( mapping = aes(
(Useful for expanding limits)
alpha, angle, color, curvature, linetype, size
x, y, alpha, color, group, linetype, size
x, y, alpha, color, fill, group, linetype, size
alpha, color, fill, group, linetype, size
x, y, alpha, color, fill, linetype, size
x, y, alpha, color, fill, group, linetype, size, weight
x, y, alpha, color, fill
x, y, alpha, color, group, linetype, size
x, y, alpha, color, fill, linetype, size, weight
x, y, alpha, color, fill, linetype, size, weight
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
60 long lat
projection, orientation, xlim, ylim Map projections from the mapproj package (mercator (default), azequalarea, lagrange, etc.)
xlim, ylim The default cartesian coordinate system
ratio, xlim, ylim Cartesian coordinates with fixed aspect ratio between x and y units
xlim, ylim Flipped Cartesian coordinates
theta, start, direction Polar coordinates
xtrans, ytrans, limx, limy Transformed cartesian coordinates. Set xtrans and ytrans to the name of a window function.
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
Position adjustments determine how to arrange geoms that would otherwise occupy the same space. 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
r + theme_classic() r + theme_light() r + theme_linedraw() r + theme_minimal() Minimal themes r + theme_void() Empty theme 0 50 100 150 c d e fl p r count 0 50 100 150 c d e fl p r count 0 50 100 150 c d e fl p r count 0 50 100 150 c d e fl p r r + theme_bw() count White background with grid lines r + theme_gray() Grey background (default theme) r + theme_dark() (^0) dark for contrast 50 100 150 c d e fl p r count
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)) Without clipping (preferred)
Place legend at "bottom", "top", "left", or "right"
Set legend type for each aesthetic: colorbar, legend, or none (no legend)
Set legend title and labels with a scale function.
Facets divide a plot into subplots based on the values of one or more discrete variables.
facet into columns based on fl
facet into rows based on year
facet into both rows and columns
wrap facets into a rectangular layout Set scales to let axis limits vary across facets
x and y axis limits adjust to individual facets
x (^) ..count..
3 (^00 1 2 3 ) 4 1 2 3 (^00 1 2 3 ) 4 data geom^ coordinate system x = x plot y = ..count.. fl cty cyl stat A stat builds new variables to plot (e.g., count, prop). Visualize a stat by changing the default stat of a geom function, geom_bar(stat="count") or by using a stat function, stat_count(geom="bar"), which calls a default geom to make a layer (equivalent to a geom function). Use ..name.. syntax to map stat variables to aesthetics.
stat function geom mappings variable created by stat geom to use 1D distributions 2D distributions 3 Variables Comparisons Functions General Purpose
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",
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 = "2 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") p + scale_shape() + scale_size() p + scale_shape_manual(values = c(3:7))
o <- c + geom_dotplot(aes(fill = ..x..)) p <- e + geom_point(aes(shape = fl, size = cyl)) p + scale_radius(range = c(1,6)) p + scale_size_area(max_size = 6) Maps to radius of circle, or area c(-1, 26) 0: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Manual shape values