Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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,
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
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,
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
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
aesthetic mappings
data
geom
last_plot()
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
Continuous Function
i <- ggplot(economics, aes(date, unemploy))
i + geom_area()
i + geom_step(direction = "hv")
Visualizing error
f + geom_col()
e + geom_pointrange()
y, alpha, color, fill, group, linetype, size
e +x,geom_linerange()
g + geom_count()
j + geom_errorbar()
j + geom_linerange()
j + geom_pointrange()
Maps
Three Variables
c2 + geom_qq(aes(sample = hwy))
j + geom_crossbar(fatten = 2)
e + geom_linerange()
e + geom_errorbar()
h + geom_hex()
Visualizing error= 2)
e + hjust,
geom_crossbar(fatten
lineheight, size, vjust
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..))
size, stroke
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
h + geom_hex()
eh ++geom_density2d()
geom_quantile()
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
Continuous X, Continuous Y
e <- ggplot(mpg, aes(cty, hwy))
Data Visualization
Two Variables
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.
l + geom_contour(aes(z = z))
x, y, z, alpha, colour, group, linetype, size,
weight
l + geom_tile(aes(fill = z))
2
1
stat
geom
1
0
data
coordinate
x=x
system
y = ..count..
plot
stat function
geom mappings
i + stat_density2d(aes(fill = ..level..),
geom = "polygon") variable created by stat
1D distributions
xlim, ylim
The default cartesian coordinate system
Comparisons
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..
0:1
scale_
aesthetic prepackaged
to adjust scale to use
title to use in
legend/axis
labels to use in
legend/axis
t + facet_grid(year ~ fl)
+ coord_quickmap()
+ coord_map(projection = "ortho",
orientation=c(41, -74, 0))
60
fl: e
fl: p
fl: r
s + geom_bar(position = "fill")
e + geom_point(position = "jitter")
e + geom_label(position = "nudge")
Nudge labels away from points
s + geom_bar(position = "stack")
o + scale_fill_gradient(low="red", high="yellow")
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
100
50
0
c
fl
150
100
50
0
c
fl
Legends
n + theme(legend.position = "bottom")
n + guides(fill = "none")
n + scale_fill_discrete(name = "Title",
labels = c("A", "B", "C", "D", "E"))
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
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))
Labels
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()
fl: c
t + facet_wrap(~ fl)
r + coord_trans(ytrans = "sqrt")
c(-1, 26)
t + facet_grid(year ~ .)
xlim, ylim
Flipped Cartesian coordinates
t + facet_grid(. ~ fl)
r + coord_flip()
breaks to use in
legend/axis
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
count
count
..count..
count
r <- d + geom_bar()
r + coord_cartesian(xlim = c(0, 5))
count
fl cty cyl
Faceting
Coordinate Systems
count
Scales
lat
Minimal themes
r + theme_void()
Empty theme