Cheat Sheet for Data Visualization in R studio through ggplot2 package.
|
# install.packages("tidyverse")
|
|
library("ggplot2")
|
|
The ggplot2 packages are included in a popular collection of packages called “the tidyverse”.
|
|
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 data values, map variables in the data set to aesthetic properties of the geom like size, color, and x and y locations.
|
|
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.
|
|
ggplot(data = mpg, aes(x = cty, y = hwy)) Begins a plot that you finish by adding layers to. Add one geom function per layer.
|
|
Add a new layer to a plot with a geom_() or stat_() function. Each provides a geom, a set of aesthetic mappings, and a default stat and position adjustment.
|
|
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", <aes> = "New <aes> legend title") t + annotate(geom = "text", x = 8, y = 9, label = "A")
|
|
RGB is the built-in colour space. Instead of "manually" creating a #RRGGBB colour string, a colour can be specified using R's rgb() function that takes three arguments: red, green, and blue (which, by default, all have a range of [0, 1]).
|
|
n + theme(legend.position = "bottom") Place legend at "bottom", "top", "lef", 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.
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.