Download R Cheat Sheet, Data Visualization with ggplot2 and more Cheat Sheet Advanced Computer Programming in PDF only on Docsity! Geoms - Use a geom function to represent data points, use the geom’s aesthetic properties to represent variables. Each function returns a layer. Three Variables l + geom_contour(aes(z = z)) 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)) 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 Two Variables Discrete X, Discrete Y g <- ggplot(diamonds, aes(cut, color)) g + geom_count() x, y, alpha, color, fill, shape, size, stroke Discrete X, Continuous Y 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 Continuous X, Continuous Y e <- ggplot(mpg, aes(cty, hwy)) 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 AB C A B C Continuous Function i <- ggplot(economics, aes(date, unemploy)) 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 Continuous Bivariate Distribution h <- ggplot(diamonds, aes(carat, price)) 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 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)) 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 Maps 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 Data Visualization with ggplot2 Cheat Sheet 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/16 ggplot(data = mpg, aes(x = cty, y = hwy)) Begins a plot that you finish by adding layers to. Add one geom function per layer. Basics 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 Data Visualization with ggplot2 Cheat Sheet 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/15 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 Bivariat 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(f ll = 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 ge m_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(rowna es(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 = 1 2 3 0 0 1 2 3 4 4 1 2 3 0 0 1 2 3 4 4 + data geom coordinate system plot + F M A = 1 2 3 0 0 1 2 3 4 4 1 2 3 0 0 1 2 3 4 4 data geom coordinate system plot x = F y = A color = F size = A 1 2 3 0 0 1 2 3 4 4 plot + F M A = 1 2 3 0 0 1 2 3 4 4 data geom coordinate systemx = F y = A x = F y = A Graphical Primitives Data Visualization with ggplot2 Cheat Sheet 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/15 Geoms - Use a geom to represent data points, use the geom’s aesthetic properties t represent vari bles Basics One Variable a + geo _area(stat = "bin") x, y, alpha, color, fill, linetype, size b + geo _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 dotpl t() x, y, alpha, color, fill a + geo _freqpoly() x, y, alpha, color, linetype, size b + geom_freqpoly(aes(y = ..density..)) a + geom_histog am(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 <- g lot( ovi s, 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 + d lta_long, yend = lat + delta_lat)) x, xend, y, yend, alpha, color, linetype, size d + geom_r ct(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, siz d<- ggplot(seals, aes(x = long, y = lat)) i + geo _raster(aes(fill = z), hjust=0.5, vjust=0.5, interpolate=FALSE) x, y, alpha, fill i + ge m_tile(aes(fill = z)) x, y, al ha, color, fill, linetype, size e + geom_c ossbar(fatten = 2) x, y, ymax, ymin, alpha, color, fill, linetype, size e + geom_errorbar() x, ymax, y in, 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(econ mics, aes(date, unemploy)) c <- ggplot(map, aes(long, lat)) data <- data.frame(murder = USArrests$Murder, state = tolower(rowna es(USArrests))) map <- map_data("state") e <- ggplot(data, aes(fill = murder)) e + geom_map( es(map_id = state), map = map) + expand_limits(x = map$long, y = map$lat) map_id, alpha, color, fill, linetype, size Maps F M A = 1 2 3 0 0 1 2 3 4 4 1 2 3 0 0 1 2 3 4 4 + data geom coordinate sys em plot + F M A = 1 2 3 0 0 1 2 3 4 4 1 2 3 0 0 1 2 3 4 4 data geom coordinate system plot x = F y = A color = F size = A 1 2 3 0 0 1 2 3 4 4 plot + F M A = 1 2 3 0 0 1 2 3 4 4 data geom coordinate systemx = F y = A 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. qplot(x = cty, y = hwy, data = mpg, geom = "point") 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 = <DATA >) + <GEOM_FUNCTION> ( mapping = aes(<MAPPINGS> ), stat = <STAT> , position = <POSITION> ) + <COORDINATE_FUNCTION> + <FACET_FUNCTION> + <SCALE_FUNCTION> + <THEME_FUNCTION><T E E_F C I <SC U ION> <F FU TION> <C ORDINATE_FUNCTION> <P SI ION> < <MA PI GS> <GE M_FUNCTI <DAT > Required Not required, sensible defaults supplied Graphical Primitives a <- ggplot(economics, aes(date, unemploy)) b <- ggplot(seals, aes(x = long, y = lat)) 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, co or fill linetype, size a + geom_ribbon(aes(ymin= nemploy - 900, ymax unemploy + 900)) - x, ymax, ymin alpha, color, fill, group, linetype, size Line Segments common aesthetics: x, y, alpha, color, linetype, size b + geom_abline(aes(intercept=0, slope=1)) b + geom_hline(aes(yintercept = lat)) b + geom_vline(aes(xintercept = long)) b + geom_segment(aes(yend=lat+1, x nd=long+1)) b + geom_spoke(aes(angle = 1:1155, radius = 1)) One Variabl c + geom_area(stat = "bin") x, y, alpha, color, fill, linetype, size + g om_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 Discrete d <- ggplot(mpg, aes(fl)) d + geom_bar() x, alpha, color, fill, linetype, size, weight Continuous c <- ggplot(mpg, aes(hwy)); c2 <- ggplot(mpg)