SlideShare a Scribd company logo
Geoms - Use a geom function to represent data points, use the geom’s aesthetic properties to represent variables. Each function returns a layer.
Three Variables
l + geom_contour(aes(z = z))
x, y, z, alpha, colour, group, linetype, size,
weight
seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2))
l <- ggplot(seals, aes(long, lat))
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
Two Variables
Discrete X, Discrete Y
g <- ggplot(diamonds, aes(cut, color))
g + geom_count()
x, y, alpha, color, fill, shape, size, stroke
Discrete X, Continuous Y
f <- ggplot(mpg, aes(class, hwy))
f + geom_col()
x, y, alpha, color, fill, group, linetype, size
f + geom_boxplot()
x, y, lower, middle, upper, ymax, ymin, alpha,
color, fill, group, linetype, shape, size, weight
f + geom_dotplot(binaxis = "y",
stackdir = "center")
x, y, alpha, color, fill, group
f + geom_violin(scale = "area")
x, y, alpha, color, fill, group, linetype, size,
weight
Continuous X, Continuous Y
e <- ggplot(mpg, aes(cty, hwy))
e + geom_label(aes(label = cty), nudge_x = 1,
nudge_y = 1, check_overlap = TRUE)
x, y, label, alpha, angle, color, family, fontface,
hjust, lineheight, size, vjust
e + geom_jitter(height = 2, width = 2)
x, y, alpha, color, fill, shape, size
e + geom_point()
x, y, alpha, color, fill, shape, size, stroke
e + geom_quantile()
x, y, alpha, color, group, linetype, size, weight
e + geom_rug(sides = "bl")
x, y, alpha, color, linetype, size
e + geom_smooth(method = lm)
x, y, alpha, color, fill, group, linetype, size, weight
e + geom_text(aes(label = cty), nudge_x = 1,
nudge_y = 1, check_overlap = TRUE)
x, y, label, alpha, angle, color, family, fontface,
hjust, lineheight, size, vjust
AB
C
A
B
C
Continuous Function
i <- ggplot(economics, aes(date, unemploy))
i + geom_area()
x, y, alpha, color, fill, linetype, size
i + geom_line()
x, y, alpha, color, group, linetype, size
i + geom_step(direction = "hv")
x, y, alpha, color, group, linetype, size
Continuous Bivariate Distribution
h <- ggplot(diamonds, aes(carat, price))
j + geom_crossbar(fatten = 2)
x, y, ymax, ymin, alpha, color, fill, group,
linetype, size
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
Visualizing error
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))
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
Maps
h + geom_bin2d(binwidth = c(0.25, 500))
x, y, alpha, color, fill, linetype, size, weight
h + geom_density2d()
x, y, alpha, colour, group, linetype, size
h + geom_hex()
x, y, alpha, colour, fill, size
Data Visualization
with ggplot2
Cheat Sheet
RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • info@rstudio.com • 844-448-1212 • rstudio.com Learn more at docs.ggplot2.org and www.ggplot2-exts.org • ggplot2 2.1.0 • Updated: 11/16
ggplot(data = mpg, aes(x = cty, y = hwy))
Begins a plot that you finish by adding layers to.
Add one geom function per layer.
Basics
Complete the template below to build a graph.
ggplot2 is based on the grammar of graphics, the
idea that you can build every graph from the same
components: a data set, a coordinate system, and
geoms—visual marks that represent data points.
To display values, map variables in the data to visual
properties of the geom (aesthetics) like size, color,
and x and y locations.
Graphical Primitives
Data Visualization
with ggplot2
Cheat Sheet
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 0.9.3.1 • Updated: 3/15
Geoms - Use a geom to represent data points, use the geom’s aesthetic properties to represent variables
Basics
One Variable
a + geom_area(stat = "bin")
x, y, alpha, color, fill, linetype, size
b + geom_area(aes(y = ..density..), stat = "bin")
a + geom_density(kernal = "gaussian")
x, y, alpha, color, fill, linetype, size, weight
b + geom_density(aes(y = ..county..))
a+ geom_dotplot()
x, y, alpha, color, fill
a + geom_freqpoly()
x, y, alpha, color, linetype, size
b + geom_freqpoly(aes(y = ..density..))
a + geom_histogram(binwidth = 5)
x, y, alpha, color, fill, linetype, size, weight
b + geom_histogram(aes(y = ..density..))
Discrete
a <- ggplot(mpg, aes(fl))
b + geom_bar()
x, alpha, color, fill, linetype, size, weight
Continuous
a <- ggplot(mpg, aes(hwy))
Two Variables
Discrete X, Discrete Y
h <- ggplot(diamonds, aes(cut, color))
h + geom_jitter()
x, y, alpha, color, fill, shape, size
Discrete X, Continuous Y
g <- ggplot(mpg, aes(class, hwy))
g + geom_bar(stat = "identity")
x, y, alpha, color, fill, linetype, size, weight
g + geom_boxplot()
lower, middle, upper, x, ymax, ymin, alpha,
color, fill, linetype, shape, size, weight
g + geom_dotplot(binaxis = "y",
stackdir = "center")
x, y, alpha, color, fill
g + geom_violin(scale = "area")
x, y, alpha, color, fill, linetype, size, weight
Continuous X, Continuous Y
f <- ggplot(mpg, aes(cty, hwy))
f + geom_blank()
f + geom_jitter()
x, y, alpha, color, fill, shape, size
f + geom_point()
x, y, alpha, color, fill, shape, size
f + geom_quantile()
x, y, alpha, color, linetype, size, weight
f + geom_rug(sides = "bl")
alpha, color, linetype, size
f + geom_smooth(model = lm)
x, y, alpha, color, fill, linetype, size, weight
f + geom_text(aes(label = cty))
x, y, label, alpha, angle, color, family, fontface,
hjust, lineheight, size, vjust
Three Variables
i + geom_contour(aes(z = z))
x, y, z, alpha, colour, linetype, size, weight
seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2))
i <- ggplot(seals, aes(long, lat))
g <- ggplot(economics, aes(date, unemploy))
Continuous Function
g + geom_area()
x, y, alpha, color, fill, linetype, size
g + geom_line()
x, y, alpha, color, linetype, size
g + geom_step(direction = "hv")
x, y, alpha, color, linetype, size
Continuous Bivariate Distribution
h <- ggplot(movies, aes(year, rating))
h + geom_bin2d(binwidth = c(5, 0.5))
xmax, xmin, ymax, ymin, alpha, color, fill,
linetype, size, weight
h + geom_density2d()
x, y, alpha, colour, linetype, size
h + geom_hex()
x, y, alpha, colour, fill size
d + geom_segment(aes(
xend = long + delta_long,
yend = lat + delta_lat))
x, xend, y, yend, alpha, color, linetype, size
d + geom_rect(aes(xmin = long, ymin = lat,
xmax= long + delta_long,
ymax = lat + delta_lat))
xmax, xmin, ymax, ymin, alpha, color, fill,
linetype, size
c + geom_polygon(aes(group = group))
x, y, alpha, color, fill, linetype, size
d<- ggplot(seals, aes(x = long, y = lat))
i + geom_raster(aes(fill = z), hjust=0.5,
vjust=0.5, interpolate=FALSE)
x, y, alpha, fill
i + geom_tile(aes(fill = z))
x, y, alpha, color, fill, linetype, size
e + geom_crossbar(fatten = 2)
x, y, ymax, ymin, alpha, color, fill, linetype,
size
e + geom_errorbar()
x, ymax, ymin, alpha, color, linetype, size,
width (also geom_errorbarh())
e + geom_linerange()
x, ymin, ymax, alpha, color, linetype, size
e + geom_pointrange()
x, y, ymin, ymax, alpha, color, fill, linetype,
shape, size
Visualizing error
df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2)
e <- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se))
g + geom_path(lineend="butt",
linejoin="round’, linemitre=1)
x, y, alpha, color, linetype, size
g + geom_ribbon(aes(ymin=unemploy - 900,
ymax=unemploy + 900))
x, ymax, ymin, alpha, color, fill, linetype, size
g <- ggplot(economics, aes(date, unemploy))
c <- ggplot(map, aes(long, lat))
data <- data.frame(murder = USArrests$Murder,
state = tolower(rownames(USArrests)))
map <- map_data("state")
e <- ggplot(data, aes(fill = murder))
e + geom_map(aes(map_id = state), map = map) +
expand_limits(x = map$long, y = map$lat)
map_id, alpha, color, fill, linetype, size
Maps
F M A
= 1
2
3
0
0 1 2 3 4
4
1
2
3
0
0 1 2 3 4
4
+
data geom coordinate
system
plot
+
F M A
= 1
2
3
0
0 1 2 3 4
4
1
2
3
0
0 1 2 3 4
4
data geom coordinate
system
plot
x = F
y = A
color = F
size = A
1
2
3
0
0 1 2 3 4
4
plot
+
F M A
=1
2
3
0
0 1 2 3 4
4
data geom coordinate
systemx = F
y = A
x = F
y = A
Graphical Primitives
Data Visualization
with ggplot2
Cheat Sheet
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 0.9.3.1 • Updated: 3/15
Geoms - Use a geom to represent data points, use the geom’s aesthetic properties to represent variables
Basics
One Variable
a + geom_area(stat = "bin")
x, y, alpha, color, fill, linetype, size
b + geom_area(aes(y = ..density..), stat = "bin")
a + geom_density(kernal = "gaussian")
x, y, alpha, color, fill, linetype, size, weight
b + geom_density(aes(y = ..county..))
a+ geom_dotplot()
x, y, alpha, color, fill
a + geom_freqpoly()
x, y, alpha, color, linetype, size
b + geom_freqpoly(aes(y = ..density..))
a + geom_histogram(binwidth = 5)
x, y, alpha, color, fill, linetype, size, weight
b + geom_histogram(aes(y = ..density..))
Discrete
a <- ggplot(mpg, aes(fl))
b + geom_bar()
x, alpha, color, fill, linetype, size, weight
Continuous
a <- ggplot(mpg, aes(hwy))
Two Variables
Discrete X, Discrete Y
h <- ggplot(diamonds, aes(cut, color))
h + geom_jitter()
x, y, alpha, color, fill, shape, size
Discrete X, Continuous Y
g <- ggplot(mpg, aes(class, hwy))
g + geom_bar(stat = "identity")
x, y, alpha, color, fill, linetype, size, weight
g + geom_boxplot()
lower, middle, upper, x, ymax, ymin, alpha,
color, fill, linetype, shape, size, weight
g + geom_dotplot(binaxis = "y",
stackdir = "center")
x, y, alpha, color, fill
g + geom_violin(scale = "area")
x, y, alpha, color, fill, linetype, size, weight
Continuous X, Continuous Y
f <- ggplot(mpg, aes(cty, hwy))
f + geom_blank()
f + geom_jitter()
x, y, alpha, color, fill, shape, size
f + geom_point()
x, y, alpha, color, fill, shape, size
f + geom_quantile()
x, y, alpha, color, linetype, size, weight
f + geom_rug(sides = "bl")
alpha, color, linetype, size
f + geom_smooth(model = lm)
x, y, alpha, color, fill, linetype, size, weight
f + geom_text(aes(label = cty))
x, y, label, alpha, angle, color, family, fontface,
hjust, lineheight, size, vjust
Three Variables
i + geom_contour(aes(z = z))
x, y, z, alpha, colour, linetype, size, weight
seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2))
i <- ggplot(seals, aes(long, lat))
g <- ggplot(economics, aes(date, unemploy))
Continuous Function
g + geom_area()
x, y, alpha, color, fill, linetype, size
g + geom_line()
x, y, alpha, color, linetype, size
g + geom_step(direction = "hv")
x, y, alpha, color, linetype, size
Continuous Bivariate Distribution
h <- ggplot(movies, aes(year, rating))
h + geom_bin2d(binwidth = c(5, 0.5))
xmax, xmin, ymax, ymin, alpha, color, fill,
linetype, size, weight
h + geom_density2d()
x, y, alpha, colour, linetype, size
h + geom_hex()
x, y, alpha, colour, fill size
d + geom_segment(aes(
xend = long + delta_long,
yend = lat + delta_lat))
x, xend, y, yend, alpha, color, linetype, size
d + geom_rect(aes(xmin = long, ymin = lat,
xmax= long + delta_long,
ymax = lat + delta_lat))
xmax, xmin, ymax, ymin, alpha, color, fill,
linetype, size
c + geom_polygon(aes(group = group))
x, y, alpha, color, fill, linetype, size
d<- ggplot(seals, aes(x = long, y = lat))
i + geom_raster(aes(fill = z), hjust=0.5,
vjust=0.5, interpolate=FALSE)
x, y, alpha, fill
i + geom_tile(aes(fill = z))
x, y, alpha, color, fill, linetype, size
e + geom_crossbar(fatten = 2)
x, y, ymax, ymin, alpha, color, fill, linetype,
size
e + geom_errorbar()
x, ymax, ymin, alpha, color, linetype, size,
width (also geom_errorbarh())
e + geom_linerange()
x, ymin, ymax, alpha, color, linetype, size
e + geom_pointrange()
x, y, ymin, ymax, alpha, color, fill, linetype,
shape, size
Visualizing error
df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2)
e <- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se))
g + geom_path(lineend="butt",
linejoin="round’, linemitre=1)
x, y, alpha, color, linetype, size
g + geom_ribbon(aes(ymin=unemploy - 900,
ymax=unemploy + 900))
x, ymax, ymin, alpha, color, fill, linetype, size
g <- ggplot(economics, aes(date, unemploy))
c <- ggplot(map, aes(long, lat))
data <- data.frame(murder = USArrests$Murder,
state = tolower(rownames(USArrests)))
map <- map_data("state")
e <- ggplot(data, aes(fill = murder))
e + geom_map(aes(map_id = state), map = map) +
expand_limits(x = map$long, y = map$lat)
map_id, alpha, color, fill, linetype, size
Maps
F M A
= 1
2
3
0
0 1 2 3 4
4
1
2
3
0
0 1 2 3 4
4
+
data geom coordinate
system
plot
+
F M A
= 1
2
3
0
0 1 2 3 4
4
1
2
3
0
0 1 2 3 4
4
data geom coordinate
system
plot
x = F
y = A
color = F
size = A
1
2
3
0
0 1 2 3 4
4
plot
+
F M A
=1
2
3
0
0 1 2 3 4
4
data geom coordinate
systemx = F
y = A
x = F
y = A
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.
qplot(x = cty, y = hwy, data = mpg, geom = "point")
Creates a complete plot with given data, geom, and
mappings. Supplies many useful defaults.
aesthetic mappings data geom
last_plot()
Returns the last plot
ggplot(data = <DATA >) +
<GEOM_FUNCTION> (
mapping = aes(<MAPPINGS> ),
stat = <STAT> ,
position = <POSITION>
) +
<COORDINATE_FUNCTION> +
<FACET_FUNCTION> +
<SCALE_FUNCTION> +
<THEME_FUNCTION><THEME_FUNCTION>
<SCALE_FUNCTION>
<FACET_FUNCTION>
<COORDINATE_FUNCTION>
<POSITION>
<STAT>
<MAPPINGS>
<GEOM_FUNCTION>
<DATA> Required
Not
required,
sensible
defaults
supplied
Graphical Primitives
a <- ggplot(economics, aes(date, unemploy))
b <- ggplot(seals, aes(x = long, y = lat))
a + geom_blank()
(Useful for expanding limits)
b + geom_curve(aes(yend = lat + 1,
xend=long+1,curvature=z)) - x, xend, y, yend,
alpha, angle, color, curvature, linetype, size
a + geom_path(lineend="butt",
linejoin="round’, linemitre=1)
x, y, alpha, color, group, linetype, size
a + geom_polygon(aes(group = group))
x, y, alpha, color, fill, group, linetype, size
b + geom_rect(aes(xmin = long, ymin=lat,
xmax= long + 1, ymax = lat + 1)) - xmax, xmin,
ymax, ymin, alpha, color, fill, linetype, size
a + geom_ribbon(aes(ymin=unemploy - 900,
ymax=unemploy + 900)) - x, ymax, ymin
alpha, color, fill, group, linetype, size
Line Segments
common aesthetics: x, y, alpha, color, linetype, size
b + geom_abline(aes(intercept=0, slope=1))
b + geom_hline(aes(yintercept = lat))
b + geom_vline(aes(xintercept = long))
b + geom_segment(aes(yend=lat+1, xend=long+1))
b + geom_spoke(aes(angle = 1:1155, radius = 1))
One Variable
c + geom_area(stat = "bin")
x, y, alpha, color, fill, linetype, size
c + geom_density(kernel = "gaussian")
x, y, alpha, color, fill, group, linetype, size, weight
c + geom_dotplot()
x, y, alpha, color, fill
c + geom_freqpoly()
x, y, alpha, color, group, linetype, size
c + geom_histogram(binwidth = 5)
x, y, alpha, color, fill, linetype, size, weight
c2 + geom_qq(aes(sample = hwy))
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
c <- ggplot(mpg, aes(hwy)); c2 <- ggplot(mpg)
RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • info@rstudio.com • 844-448-1212 • rstudio.com
Coordinate Systems
60
long
lat
π + coord_quickmap()
π + coord_map(projection = "ortho",
orientation=c(41, -74, 0))
projection, orientation, xlim, ylim
Map projections from the mapproj package
(mercator (default), azequalarea, lagrange, etc.)
r + coord_cartesian(xlim = c(0, 5))
xlim, ylim
The default cartesian coordinate system
r + coord_fixed(ratio = 1/2)
ratio, xlim, ylim
Cartesian coordinates with fixed aspect
ratio between x and y units
r + coord_flip()
xlim, ylim
Flipped Cartesian coordinates
r + coord_polar(theta = "x", direction=1 )
theta, start, direction
Polar coordinates
r + coord_trans(ytrans = "sqrt")
xtrans, ytrans, limx, limy
Transformed cartesian coordinates. Set
xtrans and ytrans to the name
of a window function.
r <- d + geom_bar()
Position Adjustments
s + geom_bar(position = "dodge")
Arrange elements side by side
s + geom_bar(position = "fill")
Stack elements on top of one another,
normalize height
e + geom_point(position = "jitter")
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")
Stack elements on top of one another
s <- ggplot(mpg, aes(fl, fill = drv))
Position adjustments determine how to arrange
geoms that would otherwise occupy the same space.
Each position adjustment can be recast as a function
with manual width and height arguments
s + geom_bar(position = position_dodge(width = 1))
A
B
Themes
r + theme_classic()
r + theme_light()
r + theme_linedraw()
r + theme_minimal()
Minimal themes
r + theme_void()
Empty theme
0
50
100
150
c d e p r
fl
count
0
50
100
150
c d e p r
fl
count
0
50
100
150
c d e p r
fl
count
0
50
100
150
c d e p r
fl
count
r + theme_bw()
White background
with grid lines
r + theme_gray()
Grey background
(default theme)
r + theme_dark()
dark for contrast0
50
100
150
c d e p r
fl
count
Zooming
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))
Without clipping (preferred)
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.
Faceting
t <- ggplot(mpg, aes(cty, hwy)) + geom_point()
Facets divide a plot into subplots based on the values
of one or more discrete variables.
t + facet_grid(. ~ fl)
facet into columns based on fl
t + facet_grid(year ~ .)
facet into rows based on year
t + facet_grid(year ~ fl)
facet into both rows and columns
t + facet_wrap(~ fl)
wrap facets into a rectangular layout
Set scales to let axis limits vary across facets
t + facet_grid(drv ~ fl, scales = "free")
x and y axis limits adjust to individual facets
• "free_x" - x axis limits adjust
• "free_y" - y axis limits adjust
Set labeller to adjust facet labels
t + facet_grid(. ~ fl, labeller = label_both)
t + facet_grid(fl ~ ., labeller = label_bquote(alpha ^ .(fl)))
t + facet_grid(. ~ fl, labeller = label_parsed)
fl: c fl: d fl: e fl: p fl: r
c d e p r
↵c
↵d ↵e
↵p
↵r
Learn more at docs.ggplot2.org and www.ggplot2-exts.org • ggplot2 2.1.0 • Updated: 11/16
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)
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..
f + stat_boxplot(coef = 1.5)
x, y | ..lower.., ..middle.., ..upper.., ..width.. , ..ymin.., ..ymax..
f + stat_ydensity(kernel = "gaussian", scale = "area")
x, y | ..density.., ..scaled.., ..count.., ..n.., ..violinwidth.., ..width..
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..
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()
Stats - An alternative way to build a layer
+
x ..count..
= 1
2
3
0
0 1 2 3 4
4
1
2
3
0
0 1 2 3 4
4
data geom coordinate
system
plot
x = x
y = ..count..
fl cty cyl
stat
A stat builds new variables to plot (e.g., count, prop).
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.
i + stat_density2d(aes(fill = ..level..),
geom = "polygon")
stat function geom mappings
variable created by stat
geom to use
1D distributions
2D distributions
3 Variables
Comparisons
Functions
General Purpose
Labels
t + labs( x = "New x axis label", y = "New y axis label",
title ="Add a title above the plot",
subtitle = "Add a subtitle below title",
caption = "Add a caption below plot",
<aes> = "New <aes> legend title")
Use scale
functions
to update
legend labels
<AES> <AES>
t + annotate(geom = "text", x = 8, y = 9, label = "A")
geom to place manual values for geom’s aesthetics
o + scale_fill_distiller(palette = "Blues")
o + scale_fill_gradient(low="red", high="yellow")
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()
Scales
Scales map data values to the visual values of an
aesthetic. To change a mapping, add a new scale.
(n <- d + geom_bar(aes(fill = fl)))
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"))
scale_
aesthetic
to adjust
prepackaged
scale to use
scale specific
arguments
range of values to
include in mapping
title to use in
legend/axis
labels to use in
legend/axis
breaks to use in
legend/axis
General Purpose scales
Use with most aesthetics
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.
X and Y location scales
Color and fill scales (Discrete)
Shape and size scales
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
n <- d + geom_bar(aes(fill = fl))
n + scale_fill_brewer(palette = "Blues")
For palette choices: RColorBrewer::display.brewer.all()
n + scale_fill_grey(start = 0.2, end = 0.8, na.value
= "red")
p + scale_shape() + scale_size()
p + scale_shape_manual(values = c(3:7))
Color and fill scales (Continuous)
o <- c + geom_dotplot(aes(fill = ..x..))
p <- e + geom_point(aes(shape = fl, size = cyl))
p + scale_radius(range = c(1,6))
p + scale_size_area(max_size = 6)
Maps to radius of
circle, or area
c(-1, 26)
0:1
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
Manual shape values

More Related Content

What's hot

Grid computing standards
Grid computing standardsGrid computing standards
Grid computing standards
Pooja Dixit
 
TYBSC IT PGIS Unit I Chapter II Geographic Information and Spacial Database
TYBSC IT PGIS Unit I Chapter II Geographic Information and Spacial DatabaseTYBSC IT PGIS Unit I Chapter II Geographic Information and Spacial Database
TYBSC IT PGIS Unit I Chapter II Geographic Information and Spacial Database
Arti Parab Academics
 
Operating Systems Chapter 6 silberschatz
Operating Systems Chapter 6 silberschatzOperating Systems Chapter 6 silberschatz
Operating Systems Chapter 6 silberschatz
GiulianoRanauro
 
Relational model
Relational modelRelational model
Relational model
Sabana Maharjan
 
TYBSC IT PGIS Unit I Chapter I- Introduction to Geographic Information Systems
TYBSC IT PGIS Unit I  Chapter I- Introduction to Geographic Information SystemsTYBSC IT PGIS Unit I  Chapter I- Introduction to Geographic Information Systems
TYBSC IT PGIS Unit I Chapter I- Introduction to Geographic Information Systems
Arti Parab Academics
 
Getting Started with R
Getting Started with RGetting Started with R
Getting Started with R
Sankhya_Analytics
 
QGIS Tutorial 2
QGIS Tutorial 2QGIS Tutorial 2
QGIS Tutorial 2
niloyghosh1984
 
The CAP Theorem
The CAP Theorem The CAP Theorem
The CAP Theorem
Aleksandar Bradic
 
Unit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptxUnit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptx
Malla Reddy University
 
Column oriented database
Column oriented databaseColumn oriented database
Column oriented databaseKanike Krishna
 
Symbology and Classifying data in ARC GIS
Symbology and Classifying data in ARC GISSymbology and Classifying data in ARC GIS
Symbology and Classifying data in ARC GIS
KU Leuven
 
Polyglot Persistence with MongoDB and Neo4j
Polyglot Persistence with MongoDB and Neo4jPolyglot Persistence with MongoDB and Neo4j
Polyglot Persistence with MongoDB and Neo4j
Corie Pollock
 
Mobile dbms
Mobile dbmsMobile dbms
Mobile dbmsTech_MX
 
Transaction management
Transaction managementTransaction management
Transaction managementrenuka_a
 
16. Concurrency Control in DBMS
16. Concurrency Control in DBMS16. Concurrency Control in DBMS
16. Concurrency Control in DBMSkoolkampus
 
14. Query Optimization in DBMS
14. Query Optimization in DBMS14. Query Optimization in DBMS
14. Query Optimization in DBMSkoolkampus
 
Spatial Databases
Spatial DatabasesSpatial Databases
Spatial Databases
Pratibha Chaudhary
 
TYBSC IT PGIS Unit II Chapter I Data Management and Processing Systems
TYBSC IT PGIS Unit II Chapter I Data Management and Processing SystemsTYBSC IT PGIS Unit II Chapter I Data Management and Processing Systems
TYBSC IT PGIS Unit II Chapter I Data Management and Processing Systems
Arti Parab Academics
 
QGIS training class 3
QGIS training class 3QGIS training class 3
QGIS training class 3
Hiroaki Sengoku
 

What's hot (20)

Grid computing standards
Grid computing standardsGrid computing standards
Grid computing standards
 
TYBSC IT PGIS Unit I Chapter II Geographic Information and Spacial Database
TYBSC IT PGIS Unit I Chapter II Geographic Information and Spacial DatabaseTYBSC IT PGIS Unit I Chapter II Geographic Information and Spacial Database
TYBSC IT PGIS Unit I Chapter II Geographic Information and Spacial Database
 
Operating Systems Chapter 6 silberschatz
Operating Systems Chapter 6 silberschatzOperating Systems Chapter 6 silberschatz
Operating Systems Chapter 6 silberschatz
 
Relational model
Relational modelRelational model
Relational model
 
TYBSC IT PGIS Unit I Chapter I- Introduction to Geographic Information Systems
TYBSC IT PGIS Unit I  Chapter I- Introduction to Geographic Information SystemsTYBSC IT PGIS Unit I  Chapter I- Introduction to Geographic Information Systems
TYBSC IT PGIS Unit I Chapter I- Introduction to Geographic Information Systems
 
Getting Started with R
Getting Started with RGetting Started with R
Getting Started with R
 
Database System Architectures
Database System ArchitecturesDatabase System Architectures
Database System Architectures
 
QGIS Tutorial 2
QGIS Tutorial 2QGIS Tutorial 2
QGIS Tutorial 2
 
The CAP Theorem
The CAP Theorem The CAP Theorem
The CAP Theorem
 
Unit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptxUnit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptx
 
Column oriented database
Column oriented databaseColumn oriented database
Column oriented database
 
Symbology and Classifying data in ARC GIS
Symbology and Classifying data in ARC GISSymbology and Classifying data in ARC GIS
Symbology and Classifying data in ARC GIS
 
Polyglot Persistence with MongoDB and Neo4j
Polyglot Persistence with MongoDB and Neo4jPolyglot Persistence with MongoDB and Neo4j
Polyglot Persistence with MongoDB and Neo4j
 
Mobile dbms
Mobile dbmsMobile dbms
Mobile dbms
 
Transaction management
Transaction managementTransaction management
Transaction management
 
16. Concurrency Control in DBMS
16. Concurrency Control in DBMS16. Concurrency Control in DBMS
16. Concurrency Control in DBMS
 
14. Query Optimization in DBMS
14. Query Optimization in DBMS14. Query Optimization in DBMS
14. Query Optimization in DBMS
 
Spatial Databases
Spatial DatabasesSpatial Databases
Spatial Databases
 
TYBSC IT PGIS Unit II Chapter I Data Management and Processing Systems
TYBSC IT PGIS Unit II Chapter I Data Management and Processing SystemsTYBSC IT PGIS Unit II Chapter I Data Management and Processing Systems
TYBSC IT PGIS Unit II Chapter I Data Management and Processing Systems
 
QGIS training class 3
QGIS training class 3QGIS training class 3
QGIS training class 3
 

Similar to Ggplot2 cheatsheet-2.1

Ggplot
GgplotGgplot
data-visualization.pdf
data-visualization.pdfdata-visualization.pdf
data-visualization.pdf
Juan José Rivas
 
VISIALIZACION DE DATA.pdf
VISIALIZACION DE DATA.pdfVISIALIZACION DE DATA.pdf
VISIALIZACION DE DATA.pdf
Ivan Bautista Fuentes
 
Data Visualization with ggplot2.pdf
Data Visualization with ggplot2.pdfData Visualization with ggplot2.pdf
Data Visualization with ggplot2.pdf
CarlosTrujillo199971
 
Data visualization-2.1
Data visualization-2.1Data visualization-2.1
Data visualization-2.1
RenukaRajmohan
 
RBootcamp Day 4
RBootcamp Day 4RBootcamp Day 4
RBootcamp Day 4
Olga Scrivner
 
Grammar of Graphics - Darya Vanichkina
Grammar of Graphics - Darya VanichkinaGrammar of Graphics - Darya Vanichkina
Grammar of Graphics - Darya Vanichkina
Darya Vanichkina
 
ggplot2: An Extensible Platform for Publication-quality Graphics
ggplot2: An Extensible Platform for Publication-quality Graphicsggplot2: An Extensible Platform for Publication-quality Graphics
ggplot2: An Extensible Platform for Publication-quality Graphics
Claus Wilke
 
r for data science 2. grammar of graphics (ggplot2) clean -ref
r for data science 2. grammar of graphics (ggplot2)  clean -refr for data science 2. grammar of graphics (ggplot2)  clean -ref
r for data science 2. grammar of graphics (ggplot2) clean -ref
Min-hyung Kim
 
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavVyacheslav Arbuzov
 
Tech talk ggplot2
Tech talk   ggplot2Tech talk   ggplot2
Tech talk ggplot2
jalle6
 
Stata cheat sheet: Data visualization
Stata cheat sheet: Data visualizationStata cheat sheet: Data visualization
Stata cheat sheet: Data visualization
Laura Hughes
 
Geo Spatial Plot using R
Geo Spatial Plot using R Geo Spatial Plot using R
Geo Spatial Plot using R
Rupak Roy
 
Foliumcheatsheet
FoliumcheatsheetFoliumcheatsheet
Foliumcheatsheet
Nishant Upadhyay
 
Stata cheat sheet: data visualization
Stata cheat sheet: data visualizationStata cheat sheet: data visualization
Stata cheat sheet: data visualization
Tim Essam
 
8. R Graphics with R
8. R Graphics with R8. R Graphics with R
8. R Graphics with R
FAO
 
Data visualization using the grammar of graphics
Data visualization using the grammar of graphicsData visualization using the grammar of graphics
Data visualization using the grammar of graphics
Rupak Roy
 
Lectures r-graphics
Lectures r-graphicsLectures r-graphics
Lectures r-graphics
etyca
 
R graphics
R graphicsR graphics
R graphics
DHIVYADEVAKI
 

Similar to Ggplot2 cheatsheet-2.1 (20)

Ggplot
GgplotGgplot
Ggplot
 
data-visualization.pdf
data-visualization.pdfdata-visualization.pdf
data-visualization.pdf
 
VISIALIZACION DE DATA.pdf
VISIALIZACION DE DATA.pdfVISIALIZACION DE DATA.pdf
VISIALIZACION DE DATA.pdf
 
Data Visualization with ggplot2.pdf
Data Visualization with ggplot2.pdfData Visualization with ggplot2.pdf
Data Visualization with ggplot2.pdf
 
Data visualization-2.1
Data visualization-2.1Data visualization-2.1
Data visualization-2.1
 
RBootcamp Day 4
RBootcamp Day 4RBootcamp Day 4
RBootcamp Day 4
 
Grammar of Graphics - Darya Vanichkina
Grammar of Graphics - Darya VanichkinaGrammar of Graphics - Darya Vanichkina
Grammar of Graphics - Darya Vanichkina
 
ggplot2: An Extensible Platform for Publication-quality Graphics
ggplot2: An Extensible Platform for Publication-quality Graphicsggplot2: An Extensible Platform for Publication-quality Graphics
ggplot2: An Extensible Platform for Publication-quality Graphics
 
r for data science 2. grammar of graphics (ggplot2) clean -ref
r for data science 2. grammar of graphics (ggplot2)  clean -refr for data science 2. grammar of graphics (ggplot2)  clean -ref
r for data science 2. grammar of graphics (ggplot2) clean -ref
 
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov VyacheslavSeminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
Seminar PSU 09.04.2013 - 10.04.2013 MiFIT, Arbuzov Vyacheslav
 
Tech talk ggplot2
Tech talk   ggplot2Tech talk   ggplot2
Tech talk ggplot2
 
Stata cheat sheet: Data visualization
Stata cheat sheet: Data visualizationStata cheat sheet: Data visualization
Stata cheat sheet: Data visualization
 
Geo Spatial Plot using R
Geo Spatial Plot using R Geo Spatial Plot using R
Geo Spatial Plot using R
 
Foliumcheatsheet
FoliumcheatsheetFoliumcheatsheet
Foliumcheatsheet
 
Stata cheat sheet: data visualization
Stata cheat sheet: data visualizationStata cheat sheet: data visualization
Stata cheat sheet: data visualization
 
Joclad 2010 d
Joclad 2010 dJoclad 2010 d
Joclad 2010 d
 
8. R Graphics with R
8. R Graphics with R8. R Graphics with R
8. R Graphics with R
 
Data visualization using the grammar of graphics
Data visualization using the grammar of graphicsData visualization using the grammar of graphics
Data visualization using the grammar of graphics
 
Lectures r-graphics
Lectures r-graphicsLectures r-graphics
Lectures r-graphics
 
R graphics
R graphicsR graphics
R graphics
 

More from Dieudonne Nahigombeye

Rstudio ide-cheatsheet
Rstudio ide-cheatsheetRstudio ide-cheatsheet
Rstudio ide-cheatsheet
Dieudonne Nahigombeye
 
Rmarkdown cheatsheet-2.0
Rmarkdown cheatsheet-2.0Rmarkdown cheatsheet-2.0
Rmarkdown cheatsheet-2.0
Dieudonne Nahigombeye
 
Reg ex cheatsheet
Reg ex cheatsheetReg ex cheatsheet
Reg ex cheatsheet
Dieudonne Nahigombeye
 
How big-is-your-graph
How big-is-your-graphHow big-is-your-graph
How big-is-your-graph
Dieudonne Nahigombeye
 
Eurostat cheatsheet
Eurostat cheatsheetEurostat cheatsheet
Eurostat cheatsheet
Dieudonne Nahigombeye
 
Devtools cheatsheet
Devtools cheatsheetDevtools cheatsheet
Devtools cheatsheet
Dieudonne Nahigombeye
 
Data transformation-cheatsheet
Data transformation-cheatsheetData transformation-cheatsheet
Data transformation-cheatsheet
Dieudonne Nahigombeye
 
Data import-cheatsheet
Data import-cheatsheetData import-cheatsheet
Data import-cheatsheet
Dieudonne Nahigombeye
 
Base r
Base rBase r
Advanced r
Advanced rAdvanced r

More from Dieudonne Nahigombeye (11)

Sparklyr
SparklyrSparklyr
Sparklyr
 
Rstudio ide-cheatsheet
Rstudio ide-cheatsheetRstudio ide-cheatsheet
Rstudio ide-cheatsheet
 
Rmarkdown cheatsheet-2.0
Rmarkdown cheatsheet-2.0Rmarkdown cheatsheet-2.0
Rmarkdown cheatsheet-2.0
 
Reg ex cheatsheet
Reg ex cheatsheetReg ex cheatsheet
Reg ex cheatsheet
 
How big-is-your-graph
How big-is-your-graphHow big-is-your-graph
How big-is-your-graph
 
Eurostat cheatsheet
Eurostat cheatsheetEurostat cheatsheet
Eurostat cheatsheet
 
Devtools cheatsheet
Devtools cheatsheetDevtools cheatsheet
Devtools cheatsheet
 
Data transformation-cheatsheet
Data transformation-cheatsheetData transformation-cheatsheet
Data transformation-cheatsheet
 
Data import-cheatsheet
Data import-cheatsheetData import-cheatsheet
Data import-cheatsheet
 
Base r
Base rBase r
Base r
 
Advanced r
Advanced rAdvanced r
Advanced r
 

Recently uploaded

一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
yhkoc
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
axoqas
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
enxupq
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
ocavb
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Boston Institute of Analytics
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
ArpitMalhotra16
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
axoqas
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
ewymefz
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
benishzehra469
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
Subhajit Sahu
 
Business update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMIBusiness update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMI
AlejandraGmez176757
 
Jpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization SampleJpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization Sample
James Polillo
 
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
Tiktokethiodaily
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
ewymefz
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
John Andrews
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Subhajit Sahu
 

Recently uploaded (20)

一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
 
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
哪里卖(usq毕业证书)南昆士兰大学毕业证研究生文凭证书托福证书原版一模一样
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
 
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
做(mqu毕业证书)麦考瑞大学毕业证硕士文凭证书学费发票原版一模一样
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
 
Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
 
Business update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMIBusiness update Q1 2024 Lar España Real Estate SOCIMI
Business update Q1 2024 Lar España Real Estate SOCIMI
 
Jpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization SampleJpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization Sample
 
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
 

Ggplot2 cheatsheet-2.1

  • 1. Geoms - Use a geom function to represent data points, use the geom’s aesthetic properties to represent variables. Each function returns a layer. Three Variables l + geom_contour(aes(z = z)) x, y, z, alpha, colour, group, linetype, size, weight seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2)) l <- ggplot(seals, aes(long, lat)) 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 Two Variables Discrete X, Discrete Y g <- ggplot(diamonds, aes(cut, color)) g + geom_count() x, y, alpha, color, fill, shape, size, stroke Discrete X, Continuous Y f <- ggplot(mpg, aes(class, hwy)) f + geom_col() x, y, alpha, color, fill, group, linetype, size f + geom_boxplot() x, y, lower, middle, upper, ymax, ymin, alpha, color, fill, group, linetype, shape, size, weight f + geom_dotplot(binaxis = "y", stackdir = "center") x, y, alpha, color, fill, group f + geom_violin(scale = "area") x, y, alpha, color, fill, group, linetype, size, weight Continuous X, Continuous Y e <- ggplot(mpg, aes(cty, hwy)) e + geom_label(aes(label = cty), nudge_x = 1, nudge_y = 1, check_overlap = TRUE) x, y, label, alpha, angle, color, family, fontface, hjust, lineheight, size, vjust e + geom_jitter(height = 2, width = 2) x, y, alpha, color, fill, shape, size e + geom_point() x, y, alpha, color, fill, shape, size, stroke e + geom_quantile() x, y, alpha, color, group, linetype, size, weight e + geom_rug(sides = "bl") x, y, alpha, color, linetype, size e + geom_smooth(method = lm) x, y, alpha, color, fill, group, linetype, size, weight e + geom_text(aes(label = cty), nudge_x = 1, nudge_y = 1, check_overlap = TRUE) x, y, label, alpha, angle, color, family, fontface, hjust, lineheight, size, vjust AB C A B C Continuous Function i <- ggplot(economics, aes(date, unemploy)) i + geom_area() x, y, alpha, color, fill, linetype, size i + geom_line() x, y, alpha, color, group, linetype, size i + geom_step(direction = "hv") x, y, alpha, color, group, linetype, size Continuous Bivariate Distribution h <- ggplot(diamonds, aes(carat, price)) j + geom_crossbar(fatten = 2) x, y, ymax, ymin, alpha, color, fill, group, linetype, size 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 Visualizing error 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)) 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 Maps h + geom_bin2d(binwidth = c(0.25, 500)) x, y, alpha, color, fill, linetype, size, weight h + geom_density2d() x, y, alpha, colour, group, linetype, size h + geom_hex() x, y, alpha, colour, fill, size Data Visualization with ggplot2 Cheat Sheet RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • info@rstudio.com • 844-448-1212 • rstudio.com Learn more at docs.ggplot2.org and www.ggplot2-exts.org • ggplot2 2.1.0 • Updated: 11/16 ggplot(data = mpg, aes(x = cty, y = hwy)) Begins a plot that you finish by adding layers to. Add one geom function per layer. Basics Complete the template below to build a graph. ggplot2 is based on the grammar of graphics, the idea that you can build every graph from the same components: a data set, a coordinate system, and geoms—visual marks that represent data points. To display values, map variables in the data to visual properties of the geom (aesthetics) like size, color, and x and y locations. Graphical Primitives Data Visualization with ggplot2 Cheat Sheet 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 0.9.3.1 • Updated: 3/15 Geoms - Use a geom to represent data points, use the geom’s aesthetic properties to represent variables Basics One Variable a + geom_area(stat = "bin") x, y, alpha, color, fill, linetype, size b + geom_area(aes(y = ..density..), stat = "bin") a + geom_density(kernal = "gaussian") x, y, alpha, color, fill, linetype, size, weight b + geom_density(aes(y = ..county..)) a+ geom_dotplot() x, y, alpha, color, fill a + geom_freqpoly() x, y, alpha, color, linetype, size b + geom_freqpoly(aes(y = ..density..)) a + geom_histogram(binwidth = 5) x, y, alpha, color, fill, linetype, size, weight b + geom_histogram(aes(y = ..density..)) Discrete a <- ggplot(mpg, aes(fl)) b + geom_bar() x, alpha, color, fill, linetype, size, weight Continuous a <- ggplot(mpg, aes(hwy)) Two Variables Discrete X, Discrete Y h <- ggplot(diamonds, aes(cut, color)) h + geom_jitter() x, y, alpha, color, fill, shape, size Discrete X, Continuous Y g <- ggplot(mpg, aes(class, hwy)) g + geom_bar(stat = "identity") x, y, alpha, color, fill, linetype, size, weight g + geom_boxplot() lower, middle, upper, x, ymax, ymin, alpha, color, fill, linetype, shape, size, weight g + geom_dotplot(binaxis = "y", stackdir = "center") x, y, alpha, color, fill g + geom_violin(scale = "area") x, y, alpha, color, fill, linetype, size, weight Continuous X, Continuous Y f <- ggplot(mpg, aes(cty, hwy)) f + geom_blank() f + geom_jitter() x, y, alpha, color, fill, shape, size f + geom_point() x, y, alpha, color, fill, shape, size f + geom_quantile() x, y, alpha, color, linetype, size, weight f + geom_rug(sides = "bl") alpha, color, linetype, size f + geom_smooth(model = lm) x, y, alpha, color, fill, linetype, size, weight f + geom_text(aes(label = cty)) x, y, label, alpha, angle, color, family, fontface, hjust, lineheight, size, vjust Three Variables i + geom_contour(aes(z = z)) x, y, z, alpha, colour, linetype, size, weight seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2)) i <- ggplot(seals, aes(long, lat)) g <- ggplot(economics, aes(date, unemploy)) Continuous Function g + geom_area() x, y, alpha, color, fill, linetype, size g + geom_line() x, y, alpha, color, linetype, size g + geom_step(direction = "hv") x, y, alpha, color, linetype, size Continuous Bivariate Distribution h <- ggplot(movies, aes(year, rating)) h + geom_bin2d(binwidth = c(5, 0.5)) xmax, xmin, ymax, ymin, alpha, color, fill, linetype, size, weight h + geom_density2d() x, y, alpha, colour, linetype, size h + geom_hex() x, y, alpha, colour, fill size d + geom_segment(aes( xend = long + delta_long, yend = lat + delta_lat)) x, xend, y, yend, alpha, color, linetype, size d + geom_rect(aes(xmin = long, ymin = lat, xmax= long + delta_long, ymax = lat + delta_lat)) xmax, xmin, ymax, ymin, alpha, color, fill, linetype, size c + geom_polygon(aes(group = group)) x, y, alpha, color, fill, linetype, size d<- ggplot(seals, aes(x = long, y = lat)) i + geom_raster(aes(fill = z), hjust=0.5, vjust=0.5, interpolate=FALSE) x, y, alpha, fill i + geom_tile(aes(fill = z)) x, y, alpha, color, fill, linetype, size e + geom_crossbar(fatten = 2) x, y, ymax, ymin, alpha, color, fill, linetype, size e + geom_errorbar() x, ymax, ymin, alpha, color, linetype, size, width (also geom_errorbarh()) e + geom_linerange() x, ymin, ymax, alpha, color, linetype, size e + geom_pointrange() x, y, ymin, ymax, alpha, color, fill, linetype, shape, size Visualizing error df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2) e <- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se)) g + geom_path(lineend="butt", linejoin="round’, linemitre=1) x, y, alpha, color, linetype, size g + geom_ribbon(aes(ymin=unemploy - 900, ymax=unemploy + 900)) x, ymax, ymin, alpha, color, fill, linetype, size g <- ggplot(economics, aes(date, unemploy)) c <- ggplot(map, aes(long, lat)) data <- data.frame(murder = USArrests$Murder, state = tolower(rownames(USArrests))) map <- map_data("state") e <- ggplot(data, aes(fill = murder)) e + geom_map(aes(map_id = state), map = map) + expand_limits(x = map$long, y = map$lat) map_id, alpha, color, fill, linetype, size Maps F M A = 1 2 3 0 0 1 2 3 4 4 1 2 3 0 0 1 2 3 4 4 + data geom coordinate system plot + F M A = 1 2 3 0 0 1 2 3 4 4 1 2 3 0 0 1 2 3 4 4 data geom coordinate system plot x = F y = A color = F size = A 1 2 3 0 0 1 2 3 4 4 plot + F M A =1 2 3 0 0 1 2 3 4 4 data geom coordinate systemx = F y = A x = F y = A Graphical Primitives Data Visualization with ggplot2 Cheat Sheet 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 0.9.3.1 • Updated: 3/15 Geoms - Use a geom to represent data points, use the geom’s aesthetic properties to represent variables Basics One Variable a + geom_area(stat = "bin") x, y, alpha, color, fill, linetype, size b + geom_area(aes(y = ..density..), stat = "bin") a + geom_density(kernal = "gaussian") x, y, alpha, color, fill, linetype, size, weight b + geom_density(aes(y = ..county..)) a+ geom_dotplot() x, y, alpha, color, fill a + geom_freqpoly() x, y, alpha, color, linetype, size b + geom_freqpoly(aes(y = ..density..)) a + geom_histogram(binwidth = 5) x, y, alpha, color, fill, linetype, size, weight b + geom_histogram(aes(y = ..density..)) Discrete a <- ggplot(mpg, aes(fl)) b + geom_bar() x, alpha, color, fill, linetype, size, weight Continuous a <- ggplot(mpg, aes(hwy)) Two Variables Discrete X, Discrete Y h <- ggplot(diamonds, aes(cut, color)) h + geom_jitter() x, y, alpha, color, fill, shape, size Discrete X, Continuous Y g <- ggplot(mpg, aes(class, hwy)) g + geom_bar(stat = "identity") x, y, alpha, color, fill, linetype, size, weight g + geom_boxplot() lower, middle, upper, x, ymax, ymin, alpha, color, fill, linetype, shape, size, weight g + geom_dotplot(binaxis = "y", stackdir = "center") x, y, alpha, color, fill g + geom_violin(scale = "area") x, y, alpha, color, fill, linetype, size, weight Continuous X, Continuous Y f <- ggplot(mpg, aes(cty, hwy)) f + geom_blank() f + geom_jitter() x, y, alpha, color, fill, shape, size f + geom_point() x, y, alpha, color, fill, shape, size f + geom_quantile() x, y, alpha, color, linetype, size, weight f + geom_rug(sides = "bl") alpha, color, linetype, size f + geom_smooth(model = lm) x, y, alpha, color, fill, linetype, size, weight f + geom_text(aes(label = cty)) x, y, label, alpha, angle, color, family, fontface, hjust, lineheight, size, vjust Three Variables i + geom_contour(aes(z = z)) x, y, z, alpha, colour, linetype, size, weight seals$z <- with(seals, sqrt(delta_long^2 + delta_lat^2)) i <- ggplot(seals, aes(long, lat)) g <- ggplot(economics, aes(date, unemploy)) Continuous Function g + geom_area() x, y, alpha, color, fill, linetype, size g + geom_line() x, y, alpha, color, linetype, size g + geom_step(direction = "hv") x, y, alpha, color, linetype, size Continuous Bivariate Distribution h <- ggplot(movies, aes(year, rating)) h + geom_bin2d(binwidth = c(5, 0.5)) xmax, xmin, ymax, ymin, alpha, color, fill, linetype, size, weight h + geom_density2d() x, y, alpha, colour, linetype, size h + geom_hex() x, y, alpha, colour, fill size d + geom_segment(aes( xend = long + delta_long, yend = lat + delta_lat)) x, xend, y, yend, alpha, color, linetype, size d + geom_rect(aes(xmin = long, ymin = lat, xmax= long + delta_long, ymax = lat + delta_lat)) xmax, xmin, ymax, ymin, alpha, color, fill, linetype, size c + geom_polygon(aes(group = group)) x, y, alpha, color, fill, linetype, size d<- ggplot(seals, aes(x = long, y = lat)) i + geom_raster(aes(fill = z), hjust=0.5, vjust=0.5, interpolate=FALSE) x, y, alpha, fill i + geom_tile(aes(fill = z)) x, y, alpha, color, fill, linetype, size e + geom_crossbar(fatten = 2) x, y, ymax, ymin, alpha, color, fill, linetype, size e + geom_errorbar() x, ymax, ymin, alpha, color, linetype, size, width (also geom_errorbarh()) e + geom_linerange() x, ymin, ymax, alpha, color, linetype, size e + geom_pointrange() x, y, ymin, ymax, alpha, color, fill, linetype, shape, size Visualizing error df <- data.frame(grp = c("A", "B"), fit = 4:5, se = 1:2) e <- ggplot(df, aes(grp, fit, ymin = fit-se, ymax = fit+se)) g + geom_path(lineend="butt", linejoin="round’, linemitre=1) x, y, alpha, color, linetype, size g + geom_ribbon(aes(ymin=unemploy - 900, ymax=unemploy + 900)) x, ymax, ymin, alpha, color, fill, linetype, size g <- ggplot(economics, aes(date, unemploy)) c <- ggplot(map, aes(long, lat)) data <- data.frame(murder = USArrests$Murder, state = tolower(rownames(USArrests))) map <- map_data("state") e <- ggplot(data, aes(fill = murder)) e + geom_map(aes(map_id = state), map = map) + expand_limits(x = map$long, y = map$lat) map_id, alpha, color, fill, linetype, size Maps F M A = 1 2 3 0 0 1 2 3 4 4 1 2 3 0 0 1 2 3 4 4 + data geom coordinate system plot + F M A = 1 2 3 0 0 1 2 3 4 4 1 2 3 0 0 1 2 3 4 4 data geom coordinate system plot x = F y = A color = F size = A 1 2 3 0 0 1 2 3 4 4 plot + F M A =1 2 3 0 0 1 2 3 4 4 data geom coordinate systemx = F y = A x = F y = A 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. qplot(x = cty, y = hwy, data = mpg, geom = "point") Creates a complete plot with given data, geom, and mappings. Supplies many useful defaults. aesthetic mappings data geom last_plot() Returns the last plot ggplot(data = <DATA >) + <GEOM_FUNCTION> ( mapping = aes(<MAPPINGS> ), stat = <STAT> , position = <POSITION> ) + <COORDINATE_FUNCTION> + <FACET_FUNCTION> + <SCALE_FUNCTION> + <THEME_FUNCTION><THEME_FUNCTION> <SCALE_FUNCTION> <FACET_FUNCTION> <COORDINATE_FUNCTION> <POSITION> <STAT> <MAPPINGS> <GEOM_FUNCTION> <DATA> Required Not required, sensible defaults supplied Graphical Primitives a <- ggplot(economics, aes(date, unemploy)) b <- ggplot(seals, aes(x = long, y = lat)) a + geom_blank() (Useful for expanding limits) b + geom_curve(aes(yend = lat + 1, xend=long+1,curvature=z)) - x, xend, y, yend, alpha, angle, color, curvature, linetype, size a + geom_path(lineend="butt", linejoin="round’, linemitre=1) x, y, alpha, color, group, linetype, size a + geom_polygon(aes(group = group)) x, y, alpha, color, fill, group, linetype, size b + geom_rect(aes(xmin = long, ymin=lat, xmax= long + 1, ymax = lat + 1)) - xmax, xmin, ymax, ymin, alpha, color, fill, linetype, size a + geom_ribbon(aes(ymin=unemploy - 900, ymax=unemploy + 900)) - x, ymax, ymin alpha, color, fill, group, linetype, size Line Segments common aesthetics: x, y, alpha, color, linetype, size b + geom_abline(aes(intercept=0, slope=1)) b + geom_hline(aes(yintercept = lat)) b + geom_vline(aes(xintercept = long)) b + geom_segment(aes(yend=lat+1, xend=long+1)) b + geom_spoke(aes(angle = 1:1155, radius = 1)) One Variable c + geom_area(stat = "bin") x, y, alpha, color, fill, linetype, size c + geom_density(kernel = "gaussian") x, y, alpha, color, fill, group, linetype, size, weight c + geom_dotplot() x, y, alpha, color, fill c + geom_freqpoly() x, y, alpha, color, group, linetype, size c + geom_histogram(binwidth = 5) x, y, alpha, color, fill, linetype, size, weight c2 + geom_qq(aes(sample = hwy)) 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 c <- ggplot(mpg, aes(hwy)); c2 <- ggplot(mpg)
  • 2. RStudio® is a trademark of RStudio, Inc. • CC BY RStudio • info@rstudio.com • 844-448-1212 • rstudio.com Coordinate Systems 60 long lat π + coord_quickmap() π + coord_map(projection = "ortho", orientation=c(41, -74, 0)) projection, orientation, xlim, ylim Map projections from the mapproj package (mercator (default), azequalarea, lagrange, etc.) r + coord_cartesian(xlim = c(0, 5)) xlim, ylim The default cartesian coordinate system r + coord_fixed(ratio = 1/2) ratio, xlim, ylim Cartesian coordinates with fixed aspect ratio between x and y units r + coord_flip() xlim, ylim Flipped Cartesian coordinates r + coord_polar(theta = "x", direction=1 ) theta, start, direction Polar coordinates r + coord_trans(ytrans = "sqrt") xtrans, ytrans, limx, limy Transformed cartesian coordinates. Set xtrans and ytrans to the name of a window function. r <- d + geom_bar() Position Adjustments s + geom_bar(position = "dodge") Arrange elements side by side s + geom_bar(position = "fill") Stack elements on top of one another, normalize height e + geom_point(position = "jitter") 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") Stack elements on top of one another s <- ggplot(mpg, aes(fl, fill = drv)) Position adjustments determine how to arrange geoms that would otherwise occupy the same space. Each position adjustment can be recast as a function with manual width and height arguments s + geom_bar(position = position_dodge(width = 1)) A B Themes r + theme_classic() r + theme_light() r + theme_linedraw() r + theme_minimal() Minimal themes r + theme_void() Empty theme 0 50 100 150 c d e p r fl count 0 50 100 150 c d e p r fl count 0 50 100 150 c d e p r fl count 0 50 100 150 c d e p r fl count r + theme_bw() White background with grid lines r + theme_gray() Grey background (default theme) r + theme_dark() dark for contrast0 50 100 150 c d e p r fl count Zooming 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)) Without clipping (preferred) 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. Faceting t <- ggplot(mpg, aes(cty, hwy)) + geom_point() Facets divide a plot into subplots based on the values of one or more discrete variables. t + facet_grid(. ~ fl) facet into columns based on fl t + facet_grid(year ~ .) facet into rows based on year t + facet_grid(year ~ fl) facet into both rows and columns t + facet_wrap(~ fl) wrap facets into a rectangular layout Set scales to let axis limits vary across facets t + facet_grid(drv ~ fl, scales = "free") x and y axis limits adjust to individual facets • "free_x" - x axis limits adjust • "free_y" - y axis limits adjust Set labeller to adjust facet labels t + facet_grid(. ~ fl, labeller = label_both) t + facet_grid(fl ~ ., labeller = label_bquote(alpha ^ .(fl))) t + facet_grid(. ~ fl, labeller = label_parsed) fl: c fl: d fl: e fl: p fl: r c d e p r ↵c ↵d ↵e ↵p ↵r Learn more at docs.ggplot2.org and www.ggplot2-exts.org • ggplot2 2.1.0 • Updated: 11/16 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) 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.. f + stat_boxplot(coef = 1.5) x, y | ..lower.., ..middle.., ..upper.., ..width.. , ..ymin.., ..ymax.. f + stat_ydensity(kernel = "gaussian", scale = "area") x, y | ..density.., ..scaled.., ..count.., ..n.., ..violinwidth.., ..width.. 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.. 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() Stats - An alternative way to build a layer + x ..count.. = 1 2 3 0 0 1 2 3 4 4 1 2 3 0 0 1 2 3 4 4 data geom coordinate system plot x = x y = ..count.. fl cty cyl stat A stat builds new variables to plot (e.g., count, prop). 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. i + stat_density2d(aes(fill = ..level..), geom = "polygon") stat function geom mappings variable created by stat geom to use 1D distributions 2D distributions 3 Variables Comparisons Functions General Purpose Labels t + labs( x = "New x axis label", y = "New y axis label", title ="Add a title above the plot", subtitle = "Add a subtitle below title", caption = "Add a caption below plot", <aes> = "New <aes> legend title") Use scale functions to update legend labels <AES> <AES> t + annotate(geom = "text", x = 8, y = 9, label = "A") geom to place manual values for geom’s aesthetics o + scale_fill_distiller(palette = "Blues") o + scale_fill_gradient(low="red", high="yellow") 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() Scales Scales map data values to the visual values of an aesthetic. To change a mapping, add a new scale. (n <- d + geom_bar(aes(fill = fl))) 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")) scale_ aesthetic to adjust prepackaged scale to use scale specific arguments range of values to include in mapping title to use in legend/axis labels to use in legend/axis breaks to use in legend/axis General Purpose scales Use with most aesthetics 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. X and Y location scales Color and fill scales (Discrete) Shape and size scales 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 n <- d + geom_bar(aes(fill = fl)) n + scale_fill_brewer(palette = "Blues") For palette choices: RColorBrewer::display.brewer.all() n + scale_fill_grey(start = 0.2, end = 0.8, na.value = "red") p + scale_shape() + scale_size() p + scale_shape_manual(values = c(3:7)) Color and fill scales (Continuous) o <- c + geom_dotplot(aes(fill = ..x..)) p <- e + geom_point(aes(shape = fl, size = cyl)) p + scale_radius(range = c(1,6)) p + scale_size_area(max_size = 6) Maps to radius of circle, or area c(-1, 26) 0:1 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 Manual shape values