Sei sulla pagina 1di 2

Geoms

Data Visualization

Graphical Primitives

with ggplot2

with ggplot2

Cheat Sheet
Data Visualization
Basics

with ggplot2

Cheat
Sheet of graphics, the
ggplot2 is based on the
grammar
idea that you can build every graph from the same
Basics
components: a data set,
a coordinate system, and
geomsvisual marks that represent data points.

Basics

F MA

1
0

data

geom

30

coordinate
system
1

4
30

plot

xmax, xmin, Bivariate


ymax, ymin, Distribution
alpha, color, fill,
Continuous
linetype,
size, weight
he<aes(year, rating))
+ggplot(movies,
geom_point()
hh++geom_density2d()
geom_bin2d(binwidth = c(5, 0.5))

x,y, alpha,
y, alpha,
color, fill,
shape,
x,xmax,
colour,
size color,
xmin,
ymax,linetype,
ymin, alpha,
fill,
linetype, size, weight

x,x,y,y,alpha,
alpha,color,
color,fill,
fill,linetype,
linetype,size,
size weight
bb++geom_density(aes(y
= ..county..))
geom_area(aes(y = ..density..),
stat = "bin")

f + geom_point()
a + geom_polygon(aes(group
= group))
f + geom_jitter()

a+
a +geom_dotplot()
geom_density(kernal = "gaussian")

x, y,size,
alpha,
x,x,y,y,alpha,
alpha,color,
color,fill
fill, linetype,
weight
b + geom_density(aes(y = ..county..))

color, fill,

x, y, alpha, color, group, linetype, size, weight

ff++geom_quantile()
geom_point()

Continuous Function
h + geom_hex()
g <- ggplot(economics, aes(date, unemploy))
x, y, alpha, colour, fill size

geom_rug(sides = "bl")
ge++
geom_area()
x, y, alpha,
color, fill, Function
linetype, size
Continuous
x,
y, alpha,
color,
linetype, size
g <- ggplot(economics, aes(date, unemploy))
g + geom_line()
g + geom_area()

a + geom_freqpoly()

x,x,y,y,alpha,
linetype,
alpha,color,
color,fill,
linetype,
sizesize, weight
bb++geom_histogram(aes(y
..density..))
geom_freqpoly(aes(y ==..density..))

1
0

x=F
system
propertiesFofMthe
(aesthetics)
like size, color,
A geom
y=A
and x and y locations.

F MA

1
0

data

data

geom

x=F
y=A
color = F
size = A

2
1

30

coordinate
system
1

x=F
y=A
color = F
size = A

30

plot

plot

Complete the template below to build a graph.

x, y, label, alpha, angle, color, family, fontface,


x, y, alpha,
color,size,
fill, linetype,
hjust,
lineheight,
vjust size, weight

hjust,
lineheight,aes(class,
size, vjust hwy))
g <ggplot(mpg,

b + geom_abline(aes(intercept=0, slope=1))
g + geom_bar(stat = "identity")
c <- ggplot(map, aes(long,
lat))
X,=Continuous
b + geom_hline(
aes(yintercept
lat)
) size,Y weight
x,Discrete
y, alpha, color,
fill,
linetype,
g <- ggplot(mpg, aes(class, hwy))
Graphical Primitives= group))
c + geom_polygon(aes(group
b
+
geom_vline(
aes(xintercept
=
long)
)
g + geom_boxplot()
x, y, alpha, color, fill, linetype, size
g + geom_bar(stat = "identity")
lower, middle,
upper, x, ymax, ymin, alpha,
b + geom_segment(
aes(yend=lat+1,
xend=long+1))
c <- ggplot(map,
aes(long, lat))
x, y, alpha,
color, fill,
linetype,
weight
color,
fill, linetype,
shape,
size, size,
weight
c + geom_polygon(aes(group
= group))
b + geom_spoke(
aes(angle =g1:1155,
radius
= 1))= "y",
+ geom_dotplot(binaxis
g + geom_boxplot()

g <- ggplot(economics,
aes(date,
x, y, alpha, color, fill,
linetype,unemploy))
size

stackdir
= "center")
lower, middle, upper, x, ymax, ymin, alpha,

x, y, alpha, color, linetype, size


x, y, alpha, color, linetype, size

geom_text(aes(label
= cty), nudge_x = 1,
Visualizing error= "hv")
C eg ++geom_step(direction
df <- data.frame(grp
= c("A",
"B"), fit
= 4:5, se = 1:2)
x,
y,
alpha,
color,
linetype,
size
nudge_y
=
1,
check_overlap
AeB<- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se)) = TRUE)

Segments
f + geom_text(aes(label = cty))
common aesthetics: x, y, alpha,
linetype,
x, y, color,
label, alpha,
angle, color,size
family,
fontface,
Discrete
X, Continuous
Y

Graphical
x, alpha,
color, fill,Primitives
linetype, size, weight

coordinate
system

b + geom_bar()
Discrete
x, alpha, color,
fill, linetype, size, weightLine
a <- ggplot(mpg, aes(fl))

One Variable

df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2)


x, y, ymax, ymin, alpha, color, fill, linetype,
e <- ggplot(df,
size aes(grp, fit, ymin = fit-se, ymax = fit+se))

e + geom_errorbar()
e + Discrete
geom_crossbar(fatten
= 2)
X, Continuous

Y
x, ymax, ymin, alpha, color, linetype, size,
x,<y, ymax,
alpha, color,
fill, linetype,
(also ymin,
geom_errorbarh())
fwidth
ggplot(mpg,
aes(class,
hwy))
size

linetype, size

RStudio is a trademark of RStudio, Inc. CC BY RStudio info@rstudio.com 844-448-1212 rstudio.com

aesthetic mappings

data

geom

qplot(x = cty, y = hwy, data = mpg, geom = "point")


Creates a complete plot with given data, geom, and
mappings. Supplies many useful defaults.

last_plot()

Returns the last plot

ggsave("plot.png", width = 5, height = 5)


Saves last plot as 5 x 5 file named "plot.png" in
working directory. Matches file type to file extension.

c + geom_freqpoly() x, y, z, alpha, colour, linetype, size, weight


x, y, alpha, color, group, linetype, size

c + geom_histogram(binwidth = 5)
x, y, alpha, color, fill, linetype, size, weight

Discrete
d <- ggplot(mpg, aes(fl))
d + geom_bar()
x, alpha, color, fill, linetype, size, weight

RStudio is a trademark of RStudio, Inc. CC BY RStudio info@rstudio.com 844-448-1212 rstudio.com

Continuous Function
i <- ggplot(economics, aes(date, unemploy))
i + geom_area()

x, y, alpha, color, group, linetype, size

i + geom_step(direction = "hv")

x, y, alpha, color, group, linetype, size

Visualizing error

x, y, ymax, ymin, alpha, color, fill, group,


linetype, size

x, ymin, ymax, alpha, color, linetype, size


x, ymax, ymin, alpha, color, linetype, size,
width (also geom_errorbarh())

f + geom_col()

e + geom_pointrange()
y, alpha, color, fill, group, linetype, size
e +x,geom_linerange()

y, alpha, color, fill, linetype, size aes(cut, color))


g <-x, ggplot(diamonds,

g + geom_count()

Learn more at docs.ggplot2.org ggplot2 0.9.3.1 Updated: 3/15

x, y, alpha, color, fill, shape, size, stroke

j + geom_errorbar()

x, ymax, ymin, alpha, color, group, linetype,


size, width (also geom_errorbarh())

j + geom_linerange()

x, ymin, ymax, alpha, color, group, linetype, size

j + geom_pointrange()

x, y, ymin, ymax, alpha, color, fill, group,


linetype, shape, size

Maps

data <- data.frame(murder = USArrests$Murder,


state = tolower(rownames(USArrests)))
map <- map_data("state")
k <- ggplot(data, aes(fill = murder))

k + geom_map(aes(map_id = state), map = map) +


expand_limits(x = map$long, y = map$lat)
map_id, alpha, color, fill, linetype, size

Three Variables

c2 + geom_qq(aes(sample = hwy))

x, y, alpha, color, fill, linetype, size, weight

x, y, alpha, colour, fill, size

j + geom_crossbar(fatten = 2)

e + geom_linerange()
e + geom_errorbar()

Begins a plot that you finish by adding layers to.


Add one geom function per layer.

h + geom_hex()

df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2)


j <- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se))

ggplot(data = mpg, aes(x = cty, y = hwy))

x, y, alpha, colour, group, linetype, size

x, y, label, alpha, angle, color, family, fontface,

Visualizing error= 2)
e + hjust,
geom_crossbar(fatten
lineheight, size, vjust

x, y, alpha, color, fill


g + geom_path(lineend="butt",
color, fill, linetype, shape, size, weight
x, y, ymin, ymax, alpha, color, fill, linetype,
linejoin="round, linemitre=1)
gg++geom_violin(scale
= "area")
x, ymin,size
ymax, alpha, color, linetype, size
geom_dotplot(binaxis
= "y",
shape,
x, y, alpha, color, linetype,
size unemploy))
g <- ggplot(economics,
aes(date,
x, y, alpha, color, fill, linetype, size, weight
stackdir
=
"center")
<GEOM_FUNCTION>
(
<GEOM_FUNCTION>
4
4
gg++geom_ribbon(
aes(ymin=unemploy - 900,
F MA
fe ++ geom_pointrange()
geom_boxplot()
Maps
Continuous
x, y, alpha, color, fill
geom_path(lineend="butt",
ymax=unemploy + 900))
3
data <-x,data.frame(murder
= USArrests$Murder,
mapping = aes(<MAPPINGS>
<MAPPINGS>3 ),
y, ymin, ymax, alpha,
color, fill, linetype,
linejoin="round
, linemitre=1)
g + geom_violin(scale = "area")
x,
ymax,
ymin,
alpha,
color,
fill,
linetype,
size
x,
y,
lower,
middle,
upper, ymax, ymin, alpha,
state
=
tolower(rownames(USArrests)))
2
2
c <-linetype,
ggplot(mpg,
aes(hwy)); x,c2
<- ggplot(mpg)
Discrete
X, Discrete
Ysize, weight
shape, size
x, y, alpha, color,
size
y,
alpha,
color,
fill,
linetype,
map
<map_data("state")
stat = <STAT>
<STAT>
1
1,
color,
fill,
group,
linetype,
shape, size, weight
h
<ggplot(diamonds,
aes(cut,
color))
Not
e <- ggplot(data, aes(fill
= murder))
4
4
g + geom_ribbon(aes(ymin=unemploy - 900,
F MA
Maps
0
0
+ 900)
)
e
+
geom_map(
aes(map_id
=
state),
map
=
map
)+
30
position = <POSITION>
30
1
1
data
<data.frame(murder
=
USArrests$Murder,
3 required,
4
3
4
2
2
<POSITION>
d<-ymax=unemploy
ggplot(seals, aes(x
=clong,
y
=
lat))
h=+ "bin")
geom_jitter()
+fill,geom_area(stat
fexpand_limits(
+= geom_dotplot(binaxis
= "y",
x, ymax, ymin, alpha, color,
linetype, size
state
tolower(rownames(USArrests)))
data geom 2 coordinate
plot
x = map$long, y = map$lat)
2
Discrete
X,fill,
Discrete
Y
x,
y,
alpha,
color,
shape,
size
x
=
F
map
<map_data("state")
system
) +
sensible d + geom_segment(aes( x, y, alpha, color, fill,h linetype,
map_id,
alpha,
color,
fill,
linetype,
size
size aes(cut, color))
1
1
<- ggplot(diamonds,
y=A
stackdir
= "center")
e <- ggplot(data,
aes(fill
= murder))
xend = long + delta_long,
<COORDINATE_FUNCTION>
0
<COORDINATE_FUNCTION>
+ 4 0 0 1 2 3 defaults
e + x,
geom_map(
aes(map_id
= map) +
y, alpha,
color,= state),
fill, map
group
yend
= lat + delta_lat))
1
0
4
3
2
d<ggplot(seals,
aes(x = long, y = lat))
h + geom_jitter()
Three
Variables
data geom coordinate
plot supplied
expand_limits(x = map$long, y = map$lat)
c
+
geom_density(kernel
=
"gaussian")
x,
xend,
y,
yend,
alpha,
color,
linetype,
size
<FACET_FUNCTION>
x,
y,
alpha,
color,
fill,
shape,
size
<FACET_FUNCTION>
+ system
x=F
d + geom_segment(aes(
fi map_id,
geom_violin(scale
++geom_raster(aes(fill
= z), hjust=0.5,
seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2))
alpha, color, fill, linetype,
size = "area")
y=A
d +xend
geom_rect(aes(xmin
=x,long,
ymin = lat,
= long + delta_long,
y, alpha,
color,i <-fill,
group, linetype,
ggplot(seals,
aes(long, lat))size, weight
vjust=0.5, interpolate=FALSE)
<SCALE_FUNCTION>
<SCALE_FUNCTION> +
alpha, color, fill, group, linetype, size,
xmax=
delta_long,
yend =long
lat ++delta_lat))
Three Variables x,x,y, y,
alpha, fill
ymax
= lat
+ delta_lat))
x, xend,
y, yend,
alpha, color, linetype, size
<THEME_FUNCTION>
weight
<THEME_FUNCTION>
i
+
geom_contour(aes(z
=
z))
i
+
geom_tile(aes(fill
= z))= z), hjust=0.5,
+ geom_dotplot()
i + geom_raster(aes(fill
seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2))
xmax, xmin, ymax, ymin,calpha,
color, fill,
x, y, z, alpha, colour, linetype, size, weight
dlinetype,
+ geom_rect(aes(xmin
= long, ymin = lat,
x,vjust=0.5,
y, alpha, color,
fill, linetype, size
size
i <- ggplot(seals, aes(long, lat))
interpolate=FALSE)
x, y, alpha, color, fill
xmax= long + delta_long,
x, y, alpha, fill
ymax = lat + delta_lat))
Discrete
Discrete
i + geom_contour(aes(z = z))
RStudio is a trademark of RStudio, Inc. CC BY RStudio info@rstudio.com 844-448-1212 rstudio.com
Learn more
at
docs.ggplot2.org
ggplot2
Updated: 3/15Y
i
+
geom_tile(aes(fill
=X,
z)) 0.9.3.1
xmax, xmin, ymax, ymin, alpha, color, fill,

Required

ggplot(data = <DATA
<DATA> >) +

h + geom_density2d()

i + geom_line()

gg++x,
geom_step(direction
= "hv")
y, alpha, color, fill,
group, linetype, size, weight
geom_line()

ff++geom_text(aes(label
fill, group,
linetype,
size = cty))
geom_smooth(model
= lm)

b + geom_bar()

geom

x, y,aalpha,
color, fill, linetype,
size, weight
alpha,
color,
<- ggplot(mpg,
aes(fl))
b + geom_histogram(aes(y = ..density..))

x, y, alpha, color, fill, linetype, size, weight

x, y, alpha, color, fill, linetype, size

color, linetype, size


ex,x,+y,y,alpha,
geom_smooth(method
= lm)
alpha, color, fill, linetype, size

h + geom_bin2d(binwidth = c(0.25, 500))

size, stroke

x, y, alpha, colour, fill size


x, y, alpha, colour, linetype, size

ff++geom_smooth(model
a + geom_ribbon(aes(ymin=unemploy
900,
geom_rug(sides = "bl")=-lm)
x,alpha,
color,
fill, linetype,
linetype,
size size, weight
ymax=unemploy
+ 900))y, -alpha,
x,color,
ymax,
ymin
To display values,
map
variables
in
the
data
to
visual
a + geom_histogram(binwidth
= 5)
Discrete
data geom coordinate
plot
x=F
y=A

Continuous Bivariate Distribution


h <- ggplot(diamonds, aes(carat, price))

h + geom_hex()
eh ++geom_density2d()
geom_quantile()

x, y, alpha, color, fill, shape, size


x, y, alpha,
color, fill, shape,
group,
linetype,
sizesize
x,x,y,y,alpha,
size,
alpha,color,
color,linetype,
fill, shape,
sizeweight

aa+
+ geom_freqpoly()
geom_dotplot()

f + geom_blank()
a + geom_area(stat =a
"bin")
Continuous+ geom_path(lineend="butt",
Continuous X, Continuous Y
x, y, alpha, color, fill, linetype, size
f <- ggplot(mpg, aes(cty, hwy))
linejoin="round
, linemitre=1)
a <- ggplot(mpg,
aes(hwy))
b + geom_area(aes(y
= ..density..),
stat = "bin")
ff++geom_jitter()
geom_blank()size
aa++geom_density(kernal
x,= "gaussian")
y, alpha, color, group,
geom_area(stat = "bin")
x, y,linetype,
alpha, color, fill, shape, size

b + geom_rect(aes(xmin = long, ymin=lat,


x,x,y,y,alpha,
size
alpha,color,
color,linetype,
fill
xmax=
long + 1, ymax =f +lat
+ 1)) - xmax, xmin,
b + geom_freqpoly(aes(y = ..density..))
f +geom_rug(sides
geom_quantile()= "bl")
ymax,
ymin,
alpha,
color,
fill,linetype,
linetype,
alpha, color,
size size
x, y, alpha, color, linetype, size, weight
a + geom_histogram(binwidth = 5)

Continuous X, Continuous Y
e <- ggplot(mpg, aes(cty, hwy))

A C e + geom_label(aes(label = cty), nudge_x = 1,


nudge_y = 1, check_overlap = TRUE)
(Useful for expanding limits)
B
Geoms - Use a geom to represent data points, use the geoms aesthetic properties
to represent variables
x, y, label, alpha, angle, color, family, fontface,
b + geom_curve(aes(yend = lat + 1,
One Variable
Two Variables hjust, lineheight, size, vjust
) - x,X,xend,
y, yend,
Continuous
Continuous
Y
Bivariate
Geomsxend=long+1,curvature=z)
- Use a geom to represent Continuous
data points, use
the geoms aesthetic
propertiesContinuous
toerepresent
variables Distribution= 2, width = 2)
+ geom_jitter(height
alpha,
angle,
color,
curvature,
linetype,
h <- ggplot(movies, aes(year, rating))
f <- ggplot(mpg,
aes(cty, size
hwy))
a <- ggplot(mpg, aes(hwy))
y, alpha, color, fill, shape, size
One Variable
Two Variablesh +x,
geom_bin2d(binwidth = c(5, 0.5))

Data Visualization

Two Variables

a <- ggplot(economics, aes(date, unemploy))


b <- ggplot(seals, aes(x = long, y = lat))
a + geom_blank()

Cheat Sheet

F MA

- Use a geom function to represent data points, use the geoms aesthetic properties to represent variables. Each function returns a layer.

seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2))


l <- ggplot(seals, aes(long, lat))

l + geom_contour(aes(z = z))
x, y, z, alpha, colour, group, linetype, size,
weight

l + geom_raster(aes(fill = z), hjust=0.5,


vjust=0.5, interpolate=FALSE)
x, y, alpha, fill

l + geom_tile(aes(fill = z))

x, y, alpha, color, fill, linetype, size, width

Learn more at docs.ggplot2.org and www.ggplot2-exts.org ggplot2 2.1.0 Updated: 11/16

2
1

stat

geom

1
0

data

coordinate
x=x
system
y = ..count..

plot

Visualize a stat by changing the default stat of a geom


function, geom_bar(stat="count") or by using a stat
function, stat_count(geom="bar"), which calls a default
geom to make a layer (equivalent to a geom function).
Use ..name.. syntax to map stat variables to aesthetics.
geom to use

stat function

geom mappings

i + stat_density2d(aes(fill = ..level..),
geom = "polygon") variable created by stat
1D distributions

c + stat_bin(binwidth = 1, origin = 10)


x, y | ..count.., ..ncount.., ..density.., ..ndensity..
c + stat_count(width = 1) x, y, | ..count.., ..prop..
c + stat_density(adjust = 1, kernel = "gaussian")
x, y, | ..count.., ..density.., ..scaled..

e + stat_bin_2d(bins = 30, drop = T) 2D distributions


x, y, fill | ..count.., ..density..
e + stat_bin_hex(bins=30) x, y, fill | ..count.., ..density..
e + stat_density_2d(contour = TRUE, n = 100)
x, y, color, size | ..level..
e + stat_ellipse(level = 0.95, segments = 51, type = "t")
l + stat_contour(aes(z = z)) x, y, z, order | ..level..
l + stat_summary_hex(aes(z = z), bins = 30, fun = max)
x, y, z, fill | ..value..
l + stat_summary_2d(aes(z = z), bins = 30, fun = mean)
x, y, z, fill | ..value..
3 Variables
f + stat_boxplot(coef = 1.5)

xlim, ylim
The default cartesian coordinate system

(n <- d + geom_bar(aes(fill = fl)))

Comparisons

x, y | ..lower.., ..middle.., ..upper.., ..width.. , ..ymin.., ..ymax..

f + stat_ydensity(kernel = "gaussian", scale = "area")

x, y | ..density.., ..scaled.., ..count.., ..n.., ..violinwidth.., ..width..

Functions
e + stat_ecdf(n = 40) x, y | ..x.., ..y..
e + stat_quantile(quantiles = c(0.1, 0.9),
formula = y ~ log(x), method = "rq") x, y | ..quantile..
e + stat_smooth(method = "lm", formula = y ~ x,
se=T, level=0.95) x, y | ..se.., ..x.., ..y.., ..ymin.., ..ymax..

ggplot() + stat_function(aes(x = -3:3), n = 99,


fun = dnorm, args = list(sd=0.5)) x | ..x.., ..y..
e + stat_identity(na.rm = TRUE)
ggplot() + stat_qq(aes(sample=1:100), dist = qt,
dparam=list(df=5)) sample, x, y | ..sample.., ..theoretical..

0:1

e + stat_sum() x, y, size | ..n.., ..prop..


e + stat_summary(fun.data = "mean_cl_boot")
h + stat_summary_bin(fun.y = "mean", geom = "bar")
e + stat_unique()
General Purpose

scale_

aesthetic prepackaged
to adjust scale to use

title to use in
legend/axis

labels to use in
legend/axis

r + coord_polar(theta = "x", direction=1 )


theta, start, direction
Polar coordinates

Use with most aesthetics

facet into rows based on year

t + facet_grid(year ~ fl)

facet into both rows and columns

xtrans, ytrans, limx, limy


Transformed cartesian coordinates. Set
xtrans and ytrans to the name
of a window function.

+ coord_quickmap()
+ coord_map(projection = "ortho",
orientation=c(41, -74, 0))

60

X and Y location scales

t + facet_grid(. ~ fl, labeller = label_both)


fl: d

fl: e

fl: p

fl: r

t + facet_grid(fl ~ ., labeller = label_bquote(alpha ^ .(fl)))


c
e
p
r
d
t + facet_grid(. ~ fl, labeller = label_parsed)
c

t + labs( x = "New x axis label", y = "New y axis label",


title ="Add a title above the plot",
Use scale
functions
subtitle = "Add a subtitle below title",
to update
caption = "Add a caption below plot", legend
labels
<aes>
<AES> = "New <aes>
<AES> legend title")

Arrange elements side by side

n <- d + geom_bar(aes(fill = fl))


n + scale_fill_brewer(palette = "Blues")

s + geom_bar(position = "fill")

n + scale_fill_grey(start = 0.2, end = 0.8, na.value


= "red")

e + geom_point(position = "jitter")

Stack elements on top of one another,


normalize height

For palette choices: RColorBrewer::display.brewer.all()

Color and fill scales (Continuous)

o <- c + geom_dotplot(aes(fill = ..x..))


o + scale_fill_distiller(palette = "Blues")

Add random noise to X and Y position of each


element to avoid overplotting

e + geom_label(position = "nudge")
Nudge labels away from points

s + geom_bar(position = "stack")

o + scale_fill_gradient(low="red", high="yellow")

Stack elements on top of one another

Each position adjustment can be recast as a function


with manual width and height arguments
s + geom_bar(position = position_dodge(width = 1))

Themes
r + theme_bw()

150

100

50

fl

r + theme_gray()

150

100

50

Grey background
(default theme)

0
c

fl

150

r + theme_dark()

100

50

0
c

fl

dark for contrast

100

50

0
c

fl

150

100

50

0
c

fl

t + annotate(geom = "text", x = 8, y = 9, label = "A")


geom to place

manual values for geoms aesthetics

Legends
n + theme(legend.position = "bottom")

Place legend at "bottom", "top", "left", or "right"

n + guides(fill = "none")

Set legend type for each aesthetic: colorbar, legend,


or none (no legend)

n + scale_fill_discrete(name = "Title",
labels = c("A", "B", "C", "D", "E"))

Set legend title and labels with a scale function.

Zooming
r + theme_classic()
r + theme_light()
r + theme_linedraw()
r + theme_minimal()

150

White background
with grid lines

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Set labeller to adjust facet labels

Position adjustments determine how to arrange


geoms that would otherwise occupy the same space.
s + geom_bar(position = "dodge")

p <- e +Manual
geom_point(aes(shape
= fl, size = cyl))
shape values
p + scale_shape() + scale_size()
p + scale_shape_manual(values = c(3:7))

x and y axis limits adjust to individual facets


"free_x" - x axis limits adjust
"free_y" - y axis limits adjust

Labels

Color and fill scales (Discrete)

Shape and size scales

Set scales to let axis limits vary across facets

Position Adjustments
s <- ggplot(mpg, aes(fl, fill = drv))

o + scale_fill_gradient2(low="red", high="blue",
mid = "white", midpoint = 25)
o + scale_fill_gradientn(colours=topo.colors(6))
Also: rainbow(), heat.colors(), terrain.colors(),
cm.colors(), RColorBrewer::brewer.pal()

wrap facets into a rectangular layout

fl: c

projection, orientation, xlim, ylim


long
Map projections from the mapproj package
(mercator (default), azequalarea, lagrange, etc.)

Use with x or y aesthetics (x shown here)


scale_x_log10() - Plot x on log10 scale
scale_x_reverse() - Reverse direction of x axis
scale_x_sqrt() - Plot x on square root scale

t + facet_wrap(~ fl)

t + facet_grid(drv ~ fl, scales = "free")

r + coord_trans(ytrans = "sqrt")

scale_*_continuous() - map cont values to visual ones


scale_*_discrete() - map discrete values to visual ones
scale_*_identity() - use data values as visual ones
scale_*_manual(values = c()) - map discrete values to
manually chosen visual ones
scale_*_date(date_labels = "%m/%d"),
date_breaks = "2 weeks") - treat data values as dates.
scale_*_datetime() - treat data x values as date times.
Use same arguments as scale_x_date().
See ?strptime for label formats.

c(-1, 26)

facet into columns based on fl

t + facet_grid(year ~ .)

xlim, ylim
Flipped Cartesian coordinates

General Purpose scales

RStudio is a trademark of RStudio, Inc. CC BY RStudio info@rstudio.com 844-448-1212 rstudio.com

t + facet_grid(. ~ fl)

r + coord_flip()

breaks to use in
legend/axis

p + scale_radius(range = c(1,6)) Maps to radius of


p + scale_size_area(max_size = 6) circle, or area

t <- ggplot(mpg, aes(cty, hwy)) + geom_point()

ratio, xlim, ylim


Cartesian coordinates with fixed aspect
ratio between x and y units

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

Facets divide a plot into subplots based on the values


of one or more discrete variables.

r + coord_fixed(ratio = 1/2)

scale specific
arguments

count

count

..count..

count

r <- d + geom_bar()
r + coord_cartesian(xlim = c(0, 5))

Scales map data values to the visual values of an


aesthetic. To change a mapping, add a new scale.

count

fl cty cyl

Faceting

Coordinate Systems

count

A stat builds new variables to plot (e.g., count, prop).

Scales

lat

Stats - An alternative way to build a layer

Minimal themes

r + theme_void()
Empty theme

Without clipping (preferred)


t + coord_cartesian(
xlim = c(0, 100), ylim = c(10, 20))
With clipping (removes unseen data points)
t + xlim(0, 100) + ylim(10, 20)
t + scale_x_continuous(limits = c(0, 100)) +
scale_y_continuous(limits = c(0, 100))

Learn more at docs.ggplot2.org and www.ggplot2-exts.org ggplot2 2.1.0 Updated: 11/16

Potrebbero piacerti anche