Ggplot2 Cheat Sheet
Ggplot2 Cheat Sheet
Cheat Sheet
# Create a scatter plot with ggplot2
Scatter plot
# Change the outline color of a histogram geom
geom_point()
geom_histogram(color = "red")
ggplot(diamonds, aes(price)) +
geom_histogram(fill = "blue")
geom_col()
Swap geom_col() for geom_bar() to calculate the bar heights from counts of the x values.
geom_point() +
# Change to other native color scales
The grammar of graphics is a framework for specifying the components of a plot. This approach of
geom_segment(aes(x = x_column, xend = x_column, y = 0, yend = y_column))
building plots in a modular way allows a high level of flexibility for creating a wide range of
geom_point(size = 4) +
visualizations.
scale_color_brewer(palette = "Spectral")
geom_point(alpha =
scale_size_area()
0.7) +
Changing shape
In a bubble plot, "bubbles" can overlap, which can be solved by adjusting the transparency attribute, # Change the shape of markers
# Change the shape radius
> Creating your first ggplot2 plot alpha. scale_size_area() makes the points to be proportional to the values in size_column.
+
base_plot +
A single line plot Visualize distributions shape = 1 makes the points circles. Run # Change max shape
example(points) to see the shape for each number.
base_plot +
area size
scale_size_area(max_size = 4)
Histogram
# Create a histogram with ggplot2
# Change the shape of markers based on a
# Create a lineplot in ggplot2
ggplot(data, aes(x_column)) +
third column
base_plot +
Box plot
ggplot() creates a canvas to draw on. aes() matches columns of data to aesthetics of the # Create a box plot with ggplot2
plot. Here, x_column is used for the x-axis and ggplot(data, aes(x = x_column, y = y_column)) +
geom_line() adds a line geometry. That is, it draws Violin plot Changing fonts
a line plot connecting each data point in the dataset.
geom_violin()
base_plot +
geom_density()
base_plot +
Geometries are visual representations of the data. Common geometries are points, lines, bars, histograms, boxes, and
theme(text = element_text(size = 20))
maps. The visual properties of geometries such as color, size and shape can be defined as attributes or aesthetics
Attributes are fixed values of visual properties of geometries. For example, if you want to set the color of all the # Change text angle
points to red, then you would be setting the color attribute to red. Attributes must always be defined inside the
geometry function.
Aesthetics are values of visual properties of geometries that depend on data values. For example, if you want the ggplot(data, aes(x = x_column, y = y_column)) +
ggplot(data, aes(x = x_column, y = y_column)) +
color of the points to depend on values in z_column then you would be mapping z_column to the color aesthetic. geom_point() +
scale_x_log10() # or scale_y_log10
geom_point() +
Changing themes
Aesthetics can be defined inside the geometry function or inside ggplot(). The latter makes the aesthetics apply to
ylim = c(min, max),
# Minimal theme
# Dark theme (high contrast)
base_plot + theme_dark()
geom_line(aes(color = z_column))
geom_point() +
# White background
# Classic theme
geom_point() +
x set or map the x-axis coordinat fill set or map the interior (fill) colo scale_x_sqrt()
y set or map the x-axis coordinat
color set or map the color or edge color
For example, you can show data for each category of a categorical variable in its own panel.
geom_point()
facet_wrap()
base_plot + labs(x = 'X Axis Label', y = 'Y Axis Label', title = 'Plot title',
geom_line()
Swap the color aesthetic for the group aesthetic to make all lines the same color.
base_plot + facet_wrap(vars(cut), nrow = 2)
# Facet into a rectangular layout but give axes free ranges (variable plot dimensions):
# Change legend position outside of the plot — You can also pass "top", "right", or "left"
geom_area()
base_plot +
geom_area()