ggplot2 Cheat Sheet

In ggplot, R

ggplot2 is a popular data visualization package in R that allows users to create stunning and informative graphics. Developed by Hadley Wickham, ggplot2 is based on the grammar of graphics, which provides a systematic way of mapping data to visual elements such as points, lines, and bars.

Basic Elements

ElementDescription
ggplot()Initializes a plot
aes()Defines aesthetic mappings
geom_*()Adds geometric objects to the plot
scale_*()Modifies the scale of an aesthetic
labs()Adds labels to the plot
theme()Modifies the appearance of the plot

Geometric Objects

ObjectDescription
geom_point()Adds points to the plot
geom_line()Adds lines to the plot
geom_path()Adds a path connecting points
geom_bar()Adds bars to the plot
geom_histogram()Adds a histogram to the plot
geom_density()Adds a density plot to the plot
geom_boxplot()Adds a boxplot to the plot
geom_violin()Adds a violin plot to the plot
geom_smooth()Adds a smoothed line to the plot

Aesthetic Mappings

MappingDescription
xX-axis variable
yY-axis variable
colorColor of the object
fillFill color of the object
sizeSize of the object
shapeShape of the object
linetypeLine type of the object
alphaTransparency of the object

Scales

ScaleDescription
scale_x_continuous()Modifies the X-axis scale
scale_y_continuous()Modifies the Y-axis scale
scale_color_*()Modifies the color scale
scale_fill_*()Modifies the fill color scale
scale_size_*()Modifies the size scale
scale_shape_*()Modifies the shape scale
scale_linetype_*()Modifies the line type scale
scale_alpha_*()Modifies the transparency scale

Labels

LabelDescription
xlab()X-axis label
ylab()Y-axis label
ggtitle()Plot title
labs()Adds multiple labels

Themes

Theme Elements

ElementDescription
axis.titleAxis title
axis.textAxis text
axis.lineAxis line
axis.ticksAxis ticks
panel.backgroundPanel background
panel.borderPanel border
panel.gridPanel grid
plot.backgroundPlot background
plot.titlePlot title
legend.backgroundLegend background
legend.titleLegend title
legend.textLegend text

Theme Functions

FunctionDescription
theme_bw()Black and white theme
theme_classic()Classic theme
theme_dark()Dark theme
theme_light()Light theme
theme_minimal()Minimal theme
theme_void()No theme

Reference

ggplot2 documentation: https://ggplot2.tidyverse.org/