Professional Documents
Culture Documents
Data Visualization
Graphical Primitives
with ggplot2
with ggplot2
+
F MA
2
1
0
4
1
4
0
3
geom_point()
he<-+ggplot(movies,
aes(year, rating))
h + geom_density2d()
hx,+x,
geom_bin2d(binwidth
= c(5,
0.5)) size, stroke
y, alpha,
shape,
y, alpha,
colour, color,
linetype, fill,
size
linetype, size, weight
b += "gaussian")
geom_path(lineend="butt",
a + geom_density(kernal
ax,+y,geom_area(stat
= "bin")
linejoin="round
, linemitre=1)
alpha, color, fill, linetype,
size, weight
size, weight
h +linetype,
geom_hex()
ehx,++y,geom_density2d()
geom_quantile()
alpha,
colour, fill size
f + geom_point()
x, y, alpha, color, group,
linetype, size
f x,
+ geom_jitter()
y, alpha, color, fill, shape, size
a+ geom_dotplot()
ax,+y,geom_density(kernal
= "gaussian")
alpha, color, fill
x,Continuous
y, alpha, color,
group, linetype, size, weight
Function
h + geom_hex() aes(date, unemploy))
g <- ggplot(economics,
b + geom_polygon(aes(group
= group))
f + geom_quantile()
f x,
+ geom_point()
y, alpha, color, linetype, size, weight
f + geom_rug(sides = "bl")
a + geom_rect(aes(xmin
=color,
long,
ymin
f alpha,
+ geom_quantile()
linetype,
size = lat,
a + geom_histogram(binwidth = 5)
geom
x=F
y=A
coordinate
system
plot
= "bl")
h + geom_hex()
Continuous Function
i <- ggplot(economics, aes(date, unemploy))
i + geom_area()
i + geom_line()
AB
linetype, size
aesthetic mappings
Function
x, Continuous
y, alpha, color,
linetype, size
One Variable
h + geom_density2d()
data
a + geom_freqpoly()
a+ geom_dotplot()
0
3
Continuous X, Continuous Y
Continuous
Bivariate Distribution= 2, width = 2)
erepresent
+ggplot(movies,
geom_jitter(height
Geomsxend
- Use=a geom
data
points,
use the
geoms
aesthetic properties to
variables
longto+represent
delta_long,
curvature
=hwy))
z))
h <aes(year, rating))
f <- ggplot(mpg,
aes(cty,
Continuous
4
3
with ggplot2
Cheat Sheet
Data Visualization
Basics
Continuous X, Continuous Y
e <- ggplot(mpg, aes(cty, hwy))
a + geom_blank()
Data Visualization
Basics
Two Variables
Cheat Sheet
- Use a geom to represent data points, use the geoms aesthetic properties to represent variables. Each function returns a layer.
data
geom
last_plot()
Returns the last plot
c + geom_freqpoly()
g <-x, ggplot(diamonds,
y, alpha, color, fill, linetype, size aes(cut, color))
g + geom_count()
c + geom_histogram(binwidth = 5)
Discrete
d <- ggplot(mpg, aes(fl))
d + geom_bar()
x, alpha, color, fill, linetype, size, weight
i + geom_step(direction = "hv")
Visualizing error
j + geom_crossbar(fatten = 2)
j + geom_errorbar()
j + geom_linerange()
j + geom_pointrange()
Maps
Three Variables
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_tile(aes(fill = z))
Scales
2
1
stat
geom
2
1
scale_
data
coordinate
x=x
system
y = ..count..
plot
layer mappings
i + stat_density2d(aes(fill = ..level..),
geom = "polygon", n = 100) variable
geom for layer
created by
transformation
aesthetic prepackaged
to adjust scale to use
title to use in
legend/axis
labels to use in
legend/axis
xlim, ylim
Flipped Cartesian coordinates
breaks to use in
legend/axis
r + coord_trans(ytrans = "sqrt")
xtrans, ytrans, limx, limy
Transformed cartesian coordinates. Set
xtrans and ytrans to the name
of a window function.
+ coord_map(projection = "ortho",
orientation=c(41, -74, 0))
60
Position Adjustments
Position adjustments determine how to arrange
geoms that would otherwise occupy the same space.
s + geom_bar(position = "fill")
Continuous
o <- c + geom_dotplot(
aes(fill = ..x..))
n + scale_fill_brewer(
palette = "Blues")
For palette choices:
library(RColorBrewer)
display.brewer.all()
o + scale_fill_gradient(
low = "red",
high = "yellow")
o + scale_fill_gradient2(
low = "red", high = "blue",
mid = "white", midpoint = 25)
n + scale_fill_grey(
start = 0.2, end = 0.8,
na.value = "red")
e + geom_point(position = "jitter")
A
B
12
18
24
p + scale_shape(
solid = FALSE)
13
19
25
14
20
15
21
10
11
16
17
22
23
0
+
* |
o %
O #
Themes
r + theme_bw()
150
100
50
fl
r + theme_gray()
150
100
50
Grey background
(default theme)
fl
Size scales
p + scale_size_area(
max_scale = 6)
t + facet_grid(year ~ .)
t + facet_grid(year ~ fl)
150
r + theme_dark()
100
50
0
c
fl
100
50
0
c
fl
150
100
50
0
c
fl
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
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
p + scale_radius(
range=c(1,6))
p + scale_size()
e + geom_label(position = "nudge")
p <- e + geom_point(aes(
shape = fl, size = cyl))
p + scale_shape_manual(
values = c(3:7))
Shape values shown in
chart on right
s + geom_bar(position = "stack")
o + scale_fill_gradientn(
colours = terrain.colors(6))
Also: rainbow(), heat.colors(),
topo.colors(), cm.colors(),
RColorBrewer::brewer.pal()
Shape scales
fl: c
n <- d + 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
lat
count
count
xlim, ylim
The default cartesian coordinate system
count
..count..
count
r <- d + geom_bar()
count
fl cty cyl
Faceting
Coordinate Systems
Minimal themes
r + theme_void()
Empty theme