# template elements
2018-04-11
Bobae Kang
(Bobae.Kang@illinois.gov)
Source: Wickham, H. (2010). “A Layered Grammar of Graphics.”
“The grammar of graphics takes us beyond a limited set of charts (words) to an almost unlimited world of graphical forms (statements).
-Wilkinson, L. (2005), p.1”
Wilkinson's idea is implemented in the Graphics Production Language (GPL) and has the following components:
Source: tidyverse.com
This article proposes an alternative parameterization of the [graphical] grammar, based around the idea of building up a graphic from multiple layers of data. The grammar differs from Wilkinson's in its arrangement of the components, the development of a hierarchy of defaults, and in that it is embedded inside another programming language.
-Wickham, H. (2010), p.4
Source: Wickham, H. (2010). “A Layered Grammar of Graphics.”
# data and aesthetics
ggplot(data, mapping = aes(x, y, ...))
data
is a data frame object (or its variant)mapping
defines aesthetic mappings of data
aes()
function x
and y
are columns in data
input to be mapped to the x-axis and y-axisaes
components
aes component |
Description | Input |
---|---|---|
colour |
Border color | Name ("red" ), rgb specification (#FF0000 ), or NA |
fill |
Fill color | Name ("red" ), rgb specification (#FF0000 ), or NA |
shape |
Shape of a point | An integer value 0 to 24, or NA |
linetype |
Linetype | An integer value 0 to 6 or a string |
size |
Size of line/point | A non-negative numeric value |
alpha |
Transparency | A numeric value 0 to 1 |
ggplot2
aesthetics
Source: Figure 1.1 in Wilke, C. (n.d).Data Visualiation.
shape
values
Source: Tidyverse. (n.d.). “Aesthetic specifications”. ggplot2.tidyverse.org.
linetype
values
Source: Tidyverse. (n.d.). “Aesthetic specifications”. ggplot2.tidyverse.org.
# adding one or more geometric objects
ggplot(data, aes(x, y, ...)) +
geom_*()
# with geom_specific `aes`
ggplot(data) +
geom_*(aes(x, y, ...))
geom
s for different graph types:geom
can take its own mapping
input
mapping
input from ggplot()
aes
specifications can be directly provided for each geom
Basic geom
s
geom | Description | Input |
---|---|---|
geom_histogram |
Histograms | Continous x |
geom_bar |
Bar plot with frequncies | Discrete x |
geom_col |
Bar plot with values | Discrete x and continuous y |
geom_point |
Points/scattorplots | Discrete/continuous x and y |
geom_jitter |
Jittered points | Discrete/continuous x and y |
geom_line |
Line plots | Discrete/continuous x and y |
geom_abline |
Reference line | intercept and slope value |
geom_hline , geom_vline |
Reference lines | xintercept or yintercept |
# geom histogram example
data <- ispcrime %>% filter(year == 2015, county != "Cook")
ggplot(data, aes(violentCrime)) +
geom_histogram()
# geom col example
data <- ispcrime %>% filter(county == "Cook") %>% gather("type", "count", murder:aggAssault)
ggplot(data, aes(type, count, fill = type)) +
geom_col(width = 0.8)
# geom point example
data <- ispcrime %>% filter(county != "Cook") %>% left_join(regions)
ggplot(data, aes(violentCrime, propertyCrime, color = region)) +
geom_point(aes(size = violentCrime + propertyCrime), alpha = .5)
# geom line example
data <- ispcrime %>% filter(county == "Cook")
ggplot(data, aes(year, violentCrime)) +
geom_line(color = "maroon", size = 1.5) +
geom_hline(yintercept = mean(data$violentCrime), linetype = "longdash")
Other geom
s
geom | Description | Input |
---|---|---|
geom_density |
Smoothed density estimates | Continous x |
geom_density2d |
Contours of a 2-d density estimates | Continous x |
geom_boxplot |
Box plots | Disc. x and cont. y |
geom_smooth |
Smoothed conditional means | |
geom_text , geom_label |
Text | |
geom_polygon |
Polygons |
geom
s.# adding labels
plot + labs(title, subtitle, caption, x, y, ...)
labs()
take a character vector of length 1
title
and subtitle
appear at the top-left.caption
appears at the bottom-rightx
and y
are for x-axis and y-axis namestheme()
alternatively …
plot +
xlab(label) +
ylab(label) +
ggtitle(label, subtitle = NULL)
labs()
can be added with a separate function.
xlab()
is for x-axis nameylab()
is for y-axis nameggtitle()
is for plot title and subtitle# a generic example with title, subtitle, and axes names
plot +
labs(
title = "This is plot title", subtitle = "This is plot subtitle",
x = "x-axis here", y = "y-axis here",
caption = "(and caption...)"
)
# a title with mathematical expressions
plot +
ggtitle(label = expression(paste("Another plot title with math expressions like ", pi, " and ", sigma^{2})))
Scale limits
plot +
xlim(...) +
ylim(...) +
lims(...)
xlim()
changes x-axis limitsylim()
changes y-axis limitslims()
is a general function to change limits...
in xlim()
and ylim()
are numeric values to set lower and upper limit for the corresponding axis...
in lims()
is a name-value pair, where the name is an aesthetic and the value is either a length-2 numeric, a character, a factor, or a datetime # limit x axis to 2000 at the top;
# this removes points with violentCrime > 2000
plot + xlim(NA, 2000)
Position scales (continuous)
scale_x_continuous(..., expand = waiver(), trans = "identity", position = "bottom")
scale_y_continuous(..., expand = waiver(), trans = "identity", position = "left")
# shortcuts for common transformations
scale_x_log10(...)
scale_y_log10(...)
scale_x_sqrt(...)
scale_y_sqrt(...)
scale_x_reverse(...)
scale_y_reverse(...)
Common scale_* arguments
Argument | Description |
---|---|
name |
a name of the scale, used as the axis label or the legend title |
breaks |
controls the breaks in the guide, which can be a character vector |
labels |
controls the lable for each break; its input must be the same length as breaks input |
limits |
a character vector specifying the data range for the scale |
Position scales (discrete)
scale_x_discrete(..., expand = waiver(), position = "bottom")
scale_y_discrete(..., expand = waiver(), position = "left")
Position scales (datetime)
scale_x_date(...)
scale_y_date(...)
scale_x_datetime(...)
scale_y_datetime(...)
scale_x_time(...)
scale_y_time(...)
# apply the log 10 scale to the y-axis
plot + scale_y_log10()
Custom scale “manuals”
scale_*_manual(name, breaks, labels, limits, ..., values)
colour
fill
size
shape
linetype
alpha
plot + scale_color_manual(
name = "",
breaks = c("Central", "Northern", "Southern"),
labels = c("Central region", "Northern region", "Southern region"),
values = c("#00ffff", "#ffff00", "#ff00ff")
)
Other custom scales
ggplot2
offers many more functions to customize scales.
See the full documentation on scales here.
guides(...)
guide_legend(...)
guide_colourbar() # equivalent to guide_colorbar()
guides
can be used to set (or remove) guides for each scaleguide_legend()
can be used to specify the legend components for each visual properties (e.g. colour
, size
, alpha
, etc.)guide_colourbar()
or guide_colorbar()
controls the continous color barguide_legned()
and guide_colourbar()
can be used as an input for each scale argument in guide()
plot + guides(
colour = guide_legend(title = "Region", title.position = "bottom"),
size = FALSE
)
plot + coord_cartesian()
coord_cartesian
coord_fixed
, coord_flip
, coord_map
and coord_trans
coord_polar
# default plot
plot
# with coord_flip()
plot + coord_flip()
# pie chart with coord_polar()
ggplot(ispcrime %>% filter(county == "Cook") %>% gather("type", "count", murder:aggAssault), aes("", count, fill = type)) +
geom_col(width = 1) +
coord_polar("y")
plot + facet_grid(facets, scales, ...)
plot + facet_wrap(facets, nrow, ncol, scales, ...)
facets
input takes a “formula” according to which the faceting is appliedfacet_grid vs facet_wrap
facet_grid()
and facet_wrap()
are mostly similar to each otherfacet_grid()
facets the plot with a variable in a single direction (horizontal or vertical)facet_wrap()
simply places the facets next to each other and wraps them accoridng to the provided number of columns and/or rows.facet formulas
Type | Formula | Description |
---|---|---|
Grid | facet_grid(. ~ x) |
Facet horizontally across x values |
Grid | facet_grid(y ~ .) |
Facet vertically across y values |
Grid | facet_grid(y ~ x) |
Facet 2-dimensionally |
Wrap | facet_wrap(~ x) |
Facet across x values |
Wrap | facet_wrap(~ x + y) |
Facet across x and y values |
# facet_grid horizontal
plot + facet_grid(. ~ region)
# facet_grid horizontal with free scales
plot + facet_grid(. ~ region, scales = "free")
# facet_grid vertical
plot + facet_grid(year ~ .)
# facet_grid two-dimensional
plot + facet_grid(year ~ region)
# facet wrap
plot + facet_wrap(~ year)
# facet wrap with specified nrow/ncol
plot + facet_wrap(~ year, ncol = 3)
# facet wrap with multiple variables
plot + facet_wrap(~ year + region, ncol = 3)
# themes
plot + theme_gray(base_size = 11, base_family = "")
ggplot2
offers several predefined themes
theme_gray()
(or theme_grey()
)base_size
controls the base font sizebase_family
controls the base font family (“serif”, “sans”, “mono”)ggthemes
pacakge offers additional predefined themesplot + theme_gray() # this is the default
plot + theme_bw()
plot + theme_linedraw()
plot + theme_light()
plot + theme_dark()
plot + theme_minimal()
plot + theme_classic()
plot + theme_void()
plot + ggthemes::theme_economist()
plot + ggthemes::theme_fivethirtyeight()
plot + ggthemes::theme_hc()
plot + ggthemes::theme_solarized()
plot + theme(...)
theme
has arguments to control and motify individual components of a plot theme:
ggplot2
extentions.
ggplot2
ggplot2
?”. r-statistics.co.Source: tenor.com
References
Source: Wikimedia.org