Skip to content

ggplot2 Cheat Sheet

melindaleung edited this page Feb 21, 2018 · 1 revision

Data Visualization with ggplot2

You can build graphs with qplot() or ggplot(). Both allow you to create complete plots but ggplot() gives you more creative freedom in creating these plots.

qplot()

qplot(x, y, data=, color=, shape=, size=, alpha=, geom=, method=, formula=, facets=, xlim=, ylim= xlab=, ylab=, main=, sub=)
Option Description
alpha transparency level: 0 (complete transparency) & 1 (complete opacity)
data data frame
geom specifies graph type ("point", "smooth", "boxplot", "line", "histogram")
main, sub title and subtitle
shape numbers represent different shapes
x,y x and y variables
xlab, ylab specifies horizontal and vertical axis labels
xlim, ylim 2-element numeric vector containing min and max values for axes

ggplot()

The base function of creating a ggplot requires you to call the following function:

ggplot(data = twitter, aes(x = lang, y = count)) + 

After the +, you can add new layers to the plot with a geom_*() or stat_*() function. Each new layer must be added with +.
R provides a useful cheat sheet here.