At the end of this course, you will know …
aes
geom
s to make scatterplots, boxplots, histograms, density plots, and barplotsstat
functionsfacet
sThree datasets from Kaggle
read.csv()
to read in each of the three datasets from the webhead()
, summary()
, or str()
to examine each datasetWHR
dataset has a row for each countryggplot()
function with data
argument+
to add each new piece of the plotting code.geom
plots the same data in a different waygeom_point()
with geom_line()
geom
s if we wantgeom_smooth()
) overlaid on the scatterplotgeom
s are drawn in the order they are added to the plotPutting each piece on its own line makes the code easier to read!
geom_smooth()
plots a locally weighted regression with standard error as a shaded areamethod = lm
se = FALSE
aes
argument modifies what data are used to plotcolor
aesthetic to the point plot to color each country’s point by continentgeom
s, we can add an aes
argument to a single geom
aes(color = Continent)
inside geom_point()
geom
s outside aes()
, it will modify their appearance without mapping back to the datasize = 2
), and black trendline (color = 'black'
)The default for
geom_point
issize = 1
theme_bw()
changes the default theme to a black-and-white theme.ggplot()
to an object, in this case happy_gdp_plot
theme_set()
theme()
to add specific theme arguments to a plotaes
mapping in your plot
scale_color_viridis_d()
is a color scheme that can be distinguished by colorblind people.
geom_boxplot()
to see distribution of happiness by continentfill
), line color (color
), line thickness (size = 1.5
where the default is 1)outlier.size = 2
) and 70% transparent (outlier.alpha = 0.7
)cereal
datasetsugars
)x
aestheticy
value computed internally by ggplot()
expand
argumentexpansion(add = c(0, 1))
indicates 0 units of padding at the low end of the axis, and 1 unit at the high endggtitle()
labs()
function to change axis label(s) without having to specify the entire scaleFunctions beginning
stat_*()
will compute some summary statistic or function based on the data and plot it.
adjust
) of the density smoothing algorithmadjust
(default is 1) gives you a smoother curvegeom
to be used for stat_density()
'polygon'
but we can change it to 'line'
ggplot(data = cereal, aes(x = calories)) +
stat_density(adjust = 2, geom = 'line', linewidth = 1.2, color = 'forestgreen') +
scale_y_continuous(expand = expansion(mult = c(0, 0.02))) +
scale_x_continuous(expand = expansion(mult = c(0, 0)), name = 'calories per serving') +
ggtitle('Distribution of calories per serving', 'for 77 brands of breakfast cereal')
olympics
track and field medals dataset (four countries)geom_bar()
function used to compute counts within each categoryfacet_wrap()
function with a one-sided formula ~ Country
to make a separate plot for each countryscales = 'free_y'
scales = 'free_x'
, or scales = 'free'
to let both x and y limits vary)facet_grid()
with a two-sided formulaMedal
into a factor and specify order of factor levelsggplot(olympics_best, aes(x = Medal, fill = Medal)) +
geom_bar(stat = 'count') +
facet_grid(Country ~ Gender, scales = 'free_y') +
ggtitle('Track and field medal tallies for selected countries, 1896-2014', subtitle = 'data source: The Guardian') +
scale_y_continuous(expand = expansion(add = c(0, 2))) +
scale_fill_manual(values = c(Bronze = 'chocolate4', Silver = 'gray60', Gold = 'goldenrod')) +
theme(legend.position = 'none',
strip.background = element_blank(),
panel.grid = element_blank())
ggsave()
writes your plot to a file.height
and width
We can use other file types such as PDF (vector graphics, so you don’t supply a resolution)
If you don’t specify a plot object, ggsave
saves the last ggplot you plotted in the plotting window to the file
library(ggpubr)
ggboxplot(data = WHR, x = 'Continent', y = 'Happiness.Score', color = 'Continent', add = 'jitter',
palette = unname(palette.colors(4)),
ylab = 'Happiness Score', ylim = c(2.5, 8.5)) +
geom_bracket(xmin = 'Europe', xmax = 'Africa', label = '*', y.position = 8.3, label.size = 8) +
geom_bracket(xmin = 'Americas', xmax = 'Africa', label = '*', y.position = 7.7, label.size = 8)
What did we just learn? Let’s revisit the learning objectives!
aes
geom
s to make scatterplots, boxplots, histograms, density plots, and barplotsstat
functionsfacet
sNow get out there and visualize some data!