Ggplot2 Cheatsheet PDF
Ggplot2 Cheatsheet PDF
Data Visualization
- Use a geom to represent data points, use the geoms aesthetic properties to represent variables. Each function returns a layer.
One Variable
with ggplot2
Two Variables
Continuous
Cheat Sheet
Continuous X, Continuous Y
f <- ggplot(mpg, aes(cty, hwy))
with ggplot2
Cheat Sheet
Data Visualization
Basics
f + geom_blank()
a + geom_area(stat = "bin")
Data Visualization
i + geom_density2d()
X, Continuous Y
Continuous Bivariate Distribution
= "gaussian")
Geoms geom_density(kernel
- Use a geom to represent Continuous
data
points,
use the
geoms
aesthetic properties to
represent
variables
x,
y, alpha,
color, fill,
shape, size
h <ggplot(movies,
aes(year,
rating))
f <- ggplot(mpg,
aes(cty,
hwy))
a + geom_histogram(binwidth
= 5)
a + geom_histogram(binwidth
= 5)
g <- ggplot(economics, aes(date, unemploy))
x, y, alpha, color, linetype, size, weight
g + geom_line()
ax,+y,geom_freqpoly()
alpha, color, fill, linetype, size, weight
f
+
geom_smooth(model
= lm)
x,
y,
alpha,
color,
fill,
linetype,
size,
weight
+y,geom_area()
alpha, color, linetype, size
x,
y,
alpha,
color,
linetype,
size
1
1
fgx,+
geom_smooth(model
= lm)
b + geom_histogram(aes(y = ..density..))
f x,
+ geom_rug(sides
= "bl") size, weight
y, alpha, color, fill, linetype,
x, y, alpha, color, fill, linetype, size
b + geom_freqpoly(aes(yb
= ..density..))
0
0
+
geom_histogram(aes(y
=
..density..))
alpha,
color,
linetype,
size
Discrete
1
0
1
0
3
4
3
4
g + x,
geom_step(direction
= "hv")
2
2 variables
To display data
values,
map
in
the
data
set
y,
alpha,
color,
fill,
linetype,
size, weight
a + geom_histogram(binwidth
= 5)
data geom coordinate
plot
f + geom_text(aes(label = cty))
gx,+y,geom_line()
alpha, color, linetype, size
<- ggplot(mpg,
aes(fl))size, weight
4
x, y,aalpha,
color, fill, linetype,
F M
A x = F 4 ofsystem
to aesthetic
properties
the
geom
like
size,
color,
f x,
+ geom_smooth(model
= lm)
Discrete
y, label, alpha, angle, color,
family, fontface,
y=A
x, y, alpha, color, linetype, size
3
3
+ geom_histogram(aes(y = ..density..))
b +bgeom_bar()
hjust,
lineheight,
vjust size, weight
x, y, alpha,
color,size,
fill, linetype,
and x and y locations.2
f
+
geom_text(aes(label
= cty))
Visualizing error
2
x, alpha, color,
fill,
linetype,
size,
weight
b <- ggplot(mpg, aes(fl))
Discrete
g + geom_step(direction
= "hv")
df <- data.frame(grp
= c("A", "B"), fit
= 4:5, se = 1:2)
1
1
f + geom_text(aes(label = cty))
x,
y,
label,
alpha,
angle,
color,
x,
y,
alpha,
color,
linetype,
size
a
<ggplot(mpg,
aes(fl))
e <- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se)) family, fontface,
F MA
04
04
Discrete
X, Continuous
Y
x, y, label, alpha,
angle, color, family,
fontface,
b + geom_bar()
1
0
1
0
3
4
3
4
2
2
hjust, lineheight, size, vjust
3
3
b
+
geom_bar()
g <ggplot(mpg,
Graphical Primitives
hjust,
lineheight,aes(class,
size, vjust hwy))
data geom coordinate
plot
e + geom_crossbar(fatten
Visualizing error= 2)
2
2
x, alpha, color, fill, linetype,
size,
weightcolor, fill, linetype, size, weight
system
x=F
x,
alpha,
y=A
df <- data.frame(grp
= c("A",
"B"),
fit =fill,4:5,
se = 1:2)
x, y, ymax, ymin,
alpha,
color,
linetype,
1
g + geom_bar(stat = "identity")
color = F 1
size aes(grp, fit, ymin = fit-se, ymax = fit+se))
c <- ggplot(map, aes(long, lat))
size = A
e <- ggplot(df,
0
0
x,Discrete
y, alpha, color,
fill, linetype, size,
weight
X,
Continuous
Y
1
0
1
0
3
4
3
4
2
2
e + geom_errorbar()
c + geom_polygon(aes(group
g <- ggplot(mpg, aes(class, hwy))
Graphical Primitives= group))
data geom coordinate
plot
Discrete
Continuous
Y
ex,+ymax,
geom_crossbar(fatten
= 2)
ymin, alpha,X,
color,
linetype,
size,
g + geom_boxplot()
x, y, alpha, color, fill, linetype, size
system
x=F
y=A
(also
geom_errorbarh())
x, <y, ymax,
ymin,
alpha, color,aes(class,
fill, linetype,
gwidth
ggplot(mpg,
hwy))
glower,
+ geom_bar(stat
middle, upper,=x,"identity")
ymax, ymin, alpha,
color = F
c <- ggplot(map, aes(long, lat))
size = A
e +size
geom_linerange()
color,
fill, linetype,
shape,
size, size,
weight
x, y, alpha,
color, fill,
linetype,
weight
ex,+ymin,
geom_errorbar()
ymax, alpha, color, linetype, size
c + geom_polygon(aes(group = group))
g + geom_dotplot(binaxis = "y",
g <- ggplot(economics, aes(date, unemploy))
g x,+ymax,
geom_bar(stat
= "identity")
ymin, alpha, color, linetype,
size,
gstackdir
+ geom_boxplot()
Build a graph with ggplot() or qplot()
x, y, alpha, color, fill, linetype, size
= "center")
(also geom_errorbarh())
e +width
geom_pointrange()
x,lower,
y, alpha,
color,
fill x, ymax, ymin, alpha,
g + geom_path(lineend="butt",
c <- ggplot(map, aes(long,
lat))
middle,
upper,
y, alpha,
color,
linetype, size, weight
ex,+x,
fill, linetype, shape,
size, weight
y,geom_linerange()
ymin,
ymax, alpha,
color,fill,
fill, linetype,
linejoin="round, linemitre=1)
g +color,
geom_violin(scale
= "area")
ggplot(data = mpg, aes(x = cty, y = hwy))
shape,
x, ymin,size
ymax, alpha, color, linetype, size
c
+
geom_polygon(aes(group
=
group))
x, y, alpha, color, linetype,
size
gx,+y,geom_dotplot(binaxis
=size,
"y", weight
alpha,
color,
fill,
linetype,
g <- ggplot(economics, aes(date, unemploy))
4
4
- 900,
F M
A you finish
stackdir = "center")
Begins a plot
that
by adding
layers to. No g + geom_ribbon(aes(ymin=unemploy
Maps
g +geom_pointrange()
geom_boxplot()
sizefill
x, y, alpha, color,
+ 900)) x, y, alpha, color, fill, linetype,
gymax=unemploy
+ geom_path(lineend="butt",
3
3
data e<-+data.frame(murder
= USArrests$Murder,
defaults, but provides more
control2 than qplot().
x,linejoin="round
ymax, ymin, alpha,
color, fill, linetype, size
y,tolower(rownames(USArrests)))
ymin, ymax,
alpha, color,
fill, linetype,
, linemitre=1)
statex,=lower,
g + geom_violin(scale
= "area")
2
middle,
upper,
x, ymax, ymin, alpha,
Discrete X, Discrete
Y
map
<map_data("state")
shape,
size
x, y, alpha, color, linetype, size
1
y, alpha, color, fill, linetype,
weight
1
h <-x,ggplot(diamonds,
aes(cut, size,
color))
data
e <- ggplot(data,
aes(fill
= murder)) shape, size, weight
color,
fill,
linetype,
g + geom_ribbon(aes(ymin=unemploy - 900,
F MA
04
04
add
layers, d<Maps = state), map = map) +
e + geom_map(aes(map_id
1
0
1
0
3
4
3
4
2
2
ggplot(seals, aes(x
= long,
y = lat))
h + geom_jitter()
+ 900)
)
3
datag<-+
data.frame(murder
= USArrests$Murder, = "y",
geom_dotplot(binaxis
elements with + ymax=unemploy
data geom 3 coordinate
plot
expand_limits(
x = map$long, y = map$lat)
x,
ymax,
ymin,
alpha,
color,
fill,
linetype,
size
state
=
tolower(rownames(USArrests)))
x,
y,
alpha,
color,
fill,
shape,
size
ggplot(mpg, aes(hwy,
cty))
+
d <- ggplot(economics, aes(date,
unemploy))
2
2
x=F
Discrete
X, Discrete Y
system
d + geom_segment(aes(
alpha, color, fill, linetype, size
mapmap_id,
<-stackdir
map_data("state")
y=A
= "center")
1
geom_point(aes(color
= cyl)) 1 +
h <- ggplot(diamonds, aes(cut, color))
layer = geom + xend = long + delta_long,
e <- ggplot(data, aes(fill = murder))
= lat + delta_lat)) + geom_path(lineend="butt",
stat + d<-yend
geom_smooth(method 0 ="lm")
+ 0 0 1 2 default
x,
y,
alpha,
color,= state),
fill map = map) +
e
+
geom_map(
aes(map_id
Three
Variables
1
0
3
4
3
4
2
ggplot(seals, aes(x d
= long, y = lat))
h + geom_jitter()
linetype, size
data geom + coordinate
coord_cartesian()
plotlayer specific x, xend, y, yend, alpha, color,
expand_limits(
x
=
map$long,
= map$lat)
linejoin="round
, linemitre=1)
x, y, alpha, color,
fill, shape, size+ delta_lat^2))
++
geom_raster(aes(fill
= z),yhjust=0.5,
seals$z
<- with(seals,
sqrt(delta_long^2
gi map_id,
geom_violin(scale
= "area")
x=F
system
mappings dd++geom_rect(aes(xmin
geom_segment(aes(= long, ymin = lat,
alpha, color, fill, linetype, size
scale_color_gradient()
+
y=A
i
<ggplot(seals,
aes(long,
lat))
vjust=0.5,
interpolate=FALSE)
xend =long
long++delta_long,
delta_long,
x,
y,
alpha,
color,
linetype,
size
xmax=
x, y, alpha, color, fill, linetype, size, weight
theme_bw()
yend == lat
lat ++ delta_lat))
delta_lat))
additional ymax
Three Variables x, y, alpha, fill
i + geom_contour(aes(z = z))
i
+
geom_tile(aes(fill = z))
x,
xend,
y,
yend,
alpha,
color,
linetype,
size
d
+
geom_ribbon(
aes(ymin=unemploy
900,
elements
xmax, xmin, ymax, ymin, alpha, color, fill,
i +x, geom_raster(aes(fill
= z),size
hjust=0.5,
seals$z <-x,with(seals,
+ delta_lat^2))
y, z, alpha, sqrt(delta_long^2
colour, linetype, size,
weight
y, alpha, color, fill, linetype,
size
ymax=unemploy
+
900)
)
dlinetype,
+ geom_rect(aes(xmin
= long, ymin = lat,
i <- ggplot(seals, aes(long, lat))
vjust=0.5, interpolate=FALSE)
Add a new layer to a plot with a geom_*()
xmax= long + delta_long,
x, ymax, ymin, alpha, color, fill, linetype, size
x, y, alpha, fill
= lat + delta_lat))
RStudio is afunction.
trademark of RStudio,
Inc. provides
CC BY RStudio [email protected]
rstudio.com
Learn more at docs.ggplot2.org
ggplot2
Updated: 3/15Y
Discrete
X, 0.9.3.1
Discrete
or stat_*()
Each
a geom, a 844-448-1212ymax
i + geom_contour(aes(z = z))
i + geom_tile(aes(fill = z))
xmax, xmin, ymax, ymin, alpha, color, fill,
data
x=F
y=A
0
3
geom
i + geom_hex()
x, y, alpha, colour, fill size
Continuous Function
j <- ggplot(economics, aes(date, unemploy))
j + geom_area()
x, y, alpha, color, fill, linetype, size
1
0
0
3
coordinate
system
plot
AB
Graphical Primitives
aesthetic mappings
data
geom
last_plot()
Returns the last plot
linetype, size
e + geom_segment(aes(
xend = long + delta_long,
yend = lat + delta_lat))
x, xend, y, yend, alpha, color, linetype, size
h <-x, ggplot(diamonds,
y, alpha, color, fill, linetype, size aes(cut, color))
j + geom_line()
x, y, alpha, color, linetype, size
j + geom_step(direction = "hv")
x, y, alpha, color, linetype, size
Visualizing error
k + geom_crossbar(fatten = 2)
x, y, ymax, ymin, alpha, color, fill, linetype,
size
k + geom_errorbar()
x, ymax, ymin, alpha, color, linetype, size,
width (also geom_errorbarh())
k + geom_linerange()
x, ymin, ymax, alpha, color, linetype, size
k + geom_pointrange()
x, y, ymin, ymax, alpha, color, fill, linetype,
shape, size
Maps
h + geom_jitter()
Three Variables
seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2))
m <- ggplot(seals, aes(long, lat))
m + geom_contour(aes(z = z))
x, y, z, alpha, colour, linetype, size, weight
m + geom_tile(aes(fill = z))
x, y, alpha, color, fill, linetype, size (slow)
Learn more at docs.ggplot2.org ggplot2 1.0.0 Updated: 4/15
Scales
2
1
stat
geom
2
1
scale_
data
coordinate
x=x
system
y = ..count..
plot
layer specific
mappings
variable created
by transformation
i + stat_density2d(aes(fill = ..level..),
geom = "polygon", n = 100)
geom for layer
2D distributions
m + stat_contour(aes(z = z))
3 Variables
x, y, z, order | ..level..
m+ stat_spoke(aes(radius= z, angle = z))
angle, radius, x, xend, y, yend | ..x.., ..xend.., ..y.., ..yend..
m + stat_summary_hex(aes(z = z), bins = 30, fun = mean)
x, y, z, fill | ..value..
m + stat_summary2d(aes(z = z), bins = 30, fun = mean)
x, y, z, fill | ..value..
r + coord_trans(ytrans = "sqrt")
xtrans, ytrans, limx, limy
Transformed cartesian coordinates. Set
xtrans and ytrans to the name
of a window function.
z + coord_map(projection = "ortho",
orientation=c(41, -74, 0))
s + geom_bar(position = "dodge")
Arrange elements side by side
s + geom_bar(position = "fill")
Continuous
g + stat_boxplot(coef = 1.5)
Comparisons
x, y | ..lower.., ..middle.., ..upper.., ..outliers..
g + stat_ydensity(adjust = 1, kernel = "gaussian", scale = "area")
x, y | ..density.., ..scaled.., ..count.., ..n.., ..violinwidth.., ..width..
n + scale_fill_brewer(
palette = "Blues")
For palette choices:
library(RcolorBrewer)
display.brewer.all()
f + stat_ecdf(n = 40)
Functions
x, y | ..x.., ..y..
f + stat_quantile(quantiles = c(0.25, 0.5, 0.75), formula = y ~ log(x),
method = "rq")
x, y | ..quantile.., ..x.., ..y..
f + stat_smooth(method = "auto", formula = y ~ x, se = TRUE, n = 80,
fullrange = FALSE, level = 0.95)
x, y | ..se.., ..x.., ..y.., ..ymin.., ..ymax..
n + scale_fill_grey(
start = 0.2, end = 0.8,
na.value = "red")
o + scale_fill_gradient(
low = "red",
high = "yellow")
o + scale_fill_gradient2(
low = "red", high = "blue",
mid = "white", midpoint = 25)
s + geom_bar(position = "stack")
Stack elements on top of one another
f + geom_point(position = "jitter")
Add random noise to X and Y position
of each element to avoid overplotting
Each position adjustment can be recast as a function
with manual width and height arguments
s + geom_bar(position = position_dodge(width = 1))
p + scale_shape(
solid = FALSE)
13
10
4
5
11
0
+
* |
o %
O #
24
19
25
14
20
15
21
12
16
17
18
22
23
Themes
r + theme_bw()
150
100
50
0
c
fl
150
100
50
Size scales
q <- f + geom_point(
aes(size = cyl))
q + scale_size_area(max = 6)
Value mapped to area of circle
(not radius)
t + facet_grid(year ~ .)
t + facet_grid(year ~ fl)
White background
with grid lines
0
c
fl
Grey background
(default theme)
r + theme_classic()
100
50
0
c
fl
White background
no gridlines
r + theme_minimal()
150
Minimal theme
100
50
0
c
t + facet_wrap(~ fl)
fl: e
fl: p
fl: r
Labels
t + xlab("New X label")
Change the label on the X axis
t + ylab("New Y label")
Legends
t + theme(legend.position = "bottom")
Place legend at "bottom", "top", "left", or "right"
t + guides(color = "none")
Set legend type for each aesthetic: colorbar, legend,
or none (no legend)
t + scale_fill_discrete(name = "Title",
labels = c("A", "B", "C"))
Set legend title and labels with a scale function.
Zooming
150
r + theme_grey()
count
p + scale_shape_manual(
values = c(3:7))
Shape values shown in
chart on right
o + scale_fill_gradientn(
colours = terrain.colors(6))
Also: rainbow(), heat.colors(),
topo.colors(), cm.colors(),
RColorBrewer::brewer.pal()
p <- f + geom_point(
aes(shape = fl))
fl: c
Position Adjustments
Shape scales
60
o <- a + geom_dotplot(
aes(fill = ..x..))
xlim, ylim
Flipped Cartesian coordinates
breaks to use in
legend/axis
n <- b + geom_bar(
aes(fill = fl))
t + facet_grid(. ~ fl)
r + coord_flip()
Discrete
n + scale_fill_manual(
values = c("skyblue", "royalblue", "blue", "navy"),
limits = c("d", "e", "p", "r"), breaks =c("d", "e", "p", "r"),
name = "fuel", labels = c("D", "E", "P", "R"))
range of values to
include in mapping
r + coord_fixed(ratio = 1/2)
scale specific
arguments
aesthetic prepackaged
to adjust scale to use
lat
count
..count..
xlim, ylim
The default cartesian coordinate system
count
r <- b + geom_bar()
r + coord_cartesian(xlim = c(0, 5))
count
fl cty cyl
Faceting
Coordinate Systems
fl