ggplot Cheat Sheet

In ggplot, R

ggplot is a powerful data visualization package in R that allows users to create stunning and informative graphs. It is based on the Grammar of Graphics, a theory that describes how to build complex visualizations by combining simple building blocks. With ggplot, users can create a wide range of graphs, including scatter plots, line charts, bar charts, histograms, and more.

With ggplot you can easily layer different elements on top of each other. For example, users can add multiple data sets, different types of plots, and various annotations to create a rich and informative visualization. Additionally, ggplot provides a wide range of customization options, allowing users to adjust the colors, fonts, and other visual elements to match their needs.

This cheat sheet provides an overview of the most commonly used functions and arguments in ggplot. For more detailed information, please refer to the ggplot documentation.

Data Manipulation

FunctionDescription
ggplot(data, aes(x, y))Create a new ggplot object with specified data and aesthetics
geom_*()Add a geometric object to the plot
scale_*()Adjust the scale of an aesthetic
coord_*()Adjust the coordinate system
facet_*()Create a faceted plot
theme_*()Adjust the plot theme

Geometric Objects

FunctionDescription
geom_point()Scatter plot
geom_line()Line plot
geom_bar()Bar plot
geom_histogram()Histogram
geom_density()Density plot
geom_boxplot()Box plot
geom_violin()Violin plot
geom_smooth()Add a smoothed line to the plot

Aesthetics

AestheticDescription
xX-axis variable
yY-axis variable
colorColor of the points/lines/bars
fillFill color of the bars
alphaTransparency of the points/lines/bars
shapeShape of the points
sizeSize of the points
linetypeLine type of the lines
groupGrouping variable for lines/points

Scales

FunctionDescription
scale_x_continuous()Continuous X-axis scale
scale_y_continuous()Continuous Y-axis scale
scale_x_discrete()Discrete X-axis scale
scale_y_discrete()Discrete Y-axis scale
scale_color_*()Adjust color scale
scale_fill_*()Adjust fill scale
scale_alpha_*()Adjust alpha scale
scale_shape_*()Adjust shape scale
scale_size_*()Adjust size scale
scale_linetype_*()Adjust linetype scale

Coordinates

FunctionDescription
coord_cartesian()Set limits for X and Y axes
coord_flip()Flip X and Y axes
coord_polar()Create a polar coordinate system
coord_quickmap()Create a quick map projection

Facets

FunctionDescription
facet_wrap()Wrap the plot into multiple panels
facet_grid()Create a grid of panels

Themes

FunctionDescription
theme_bw()Black and white theme
theme_classic()Classic theme
theme_void()No theme
theme_set()Set the default theme
theme()Customize the theme

Reference

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

# #