The tidyverse is a collection of powerful R packages
for data science. They include:
readrtidyr, dplyrggplot2tibble,
purrr, stringr, forcatsYou can install them all using
(Remember, you only need to do this once!)
Pipes use the %>% operator to
take the output from a previous function call and “pipe” it through to
the next function.
The pipe operator, written as %>%, is a
longstanding feature of the magrittr package for R. It takes the output
of one function and passes it into another function as an argument. This
allows us to link a sequence of analysis steps
This can really help with readability of code when we use multiple nested functions!
We will use the gapminder data from Jenny Bryan to
demonstrate throughout this section. Load the package
gapminder (after installing) and the corresponding
data.
Let’s preview the data.
## # A tibble: 6 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
Let’s preview the data.
## # A tibble: 10 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
## 7 Afghanistan Asia 1982 39.9 12881816 978.
## 8 Afghanistan Asia 1987 40.8 13867957 852.
## 9 Afghanistan Asia 1992 41.7 16317921 649.
## 10 Afghanistan Asia 1997 41.8 22227415 635.
Let’s preview the data.
## # A tibble: 15 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
## 7 Afghanistan Asia 1982 39.9 12881816 978.
## 8 Afghanistan Asia 1987 40.8 13867957 852.
## 9 Afghanistan Asia 1992 41.7 16317921 649.
## 10 Afghanistan Asia 1997 41.8 22227415 635.
## 11 Afghanistan Asia 2002 42.1 25268405 727.
## 12 Afghanistan Asia 2007 43.8 31889923 975.
## 13 Albania Europe 1952 55.2 1282697 1601.
## 14 Albania Europe 1957 59.3 1476505 1942.
## 15 Albania Europe 1962 64.8 1728137 2313.
Let’s preview the data. Some information not showing up.
## [1] 779.4453 820.8530 853.1007 836.1971 739.9811 786.1134 978.0114
## [8] 852.3959 649.3414 635.3414 726.7341 974.5803 1601.0561 1942.2842
## [15] 2312.8890
## country continent year lifeExp
## Afghanistan: 12 Africa :624 Min. :1952 Min. :23.60
## Albania : 12 Americas:300 1st Qu.:1966 1st Qu.:48.20
## Algeria : 12 Asia :396 Median :1980 Median :60.71
## Angola : 12 Europe :360 Mean :1980 Mean :59.47
## Argentina : 12 Oceania : 24 3rd Qu.:1993 3rd Qu.:70.85
## Australia : 12 Max. :2007 Max. :82.60
## (Other) :1632
## pop gdpPercap
## Min. :6.001e+04 Min. : 241.2
## 1st Qu.:2.794e+06 1st Qu.: 1202.1
## Median :7.024e+06 Median : 3531.8
## Mean :2.960e+07 Mean : 7215.3
## 3rd Qu.:1.959e+07 3rd Qu.: 9325.5
## Max. :1.319e+09 Max. :113523.1
##
## tibble [1,704 × 6] (S3: tbl_df/tbl/data.frame)
## $ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
## $ year : int [1:1704] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
## $ lifeExp : num [1:1704] 28.8 30.3 32 34 36.1 ...
## $ pop : int [1:1704] 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
## $ gdpPercap: num [1:1704] 779 821 853 836 740 ...
factors are categorical data that use integer representation.
This can be efficient to store character vectors, because each
string is only entered once. Because of this, creating data frames (but
not tibbles!) in R often default to set strings as factors. However, it
can also get annoying! Factors limit the categories, or
levels, of the vector, which is often not appropriate. I
usually turn this off by setting the parameter
stringsAsFactors = FALSE when I use
data.frame(), unless I specifically want my data encoded as
categorical.
Below are the mean, median, standard deviation and IQR of the variable “pop”.
## [1] 29601212
## [1] 7023596
## [1] 106157897
## [1] 16791558
Both of these are acceptable. However, when we start adding more and more functions, pipes can make your code much easier and more natural to read.
## [1] 29601212
With pipes, we can use
## [1] 29601212
What if we want the mean log population within our dataset? Using base R, we might type:
## [1] 15.76611
With pipes, we can use
## [1] 15.76611
Very commonly, we will want to filter our data to restrict what we plot or analyze.
For example, what if we are only interested in data from China since 1970?
## [1] FALSE FALSE FALSE FALSE FALSE FALSE
Very commonly, we will want to filter our data to restrict what we plot or analyze.
For example, what if we are only interested in data from all countries and its corresponding years?
## # A tibble: 1,704 × 2
## country year
## <fct> <int>
## 1 Afghanistan 1952
## 2 Afghanistan 1957
## 3 Afghanistan 1962
## 4 Afghanistan 1967
## 5 Afghanistan 1972
## 6 Afghanistan 1977
## 7 Afghanistan 1982
## 8 Afghanistan 1987
## 9 Afghanistan 1992
## 10 Afghanistan 1997
## # … with 1,694 more rows
Very commonly, we will want to filter our data to restrict what we plot or analyze.
For example, what if we are only interested in data from China since 1970?
china_1980 <- gapminder %>%
select(country, year) %>%
filter(country == "China" & year >= 1970)
china_1980## # A tibble: 8 × 2
## country year
## <fct> <int>
## 1 China 1972
## 2 China 1977
## 3 China 1982
## 4 China 1987
## 5 China 1992
## 6 China 1997
## 7 China 2002
## 8 China 2007
ggplot2 is a fantastic way to make
complex publication quality images without much pre-processing of your
data.
Plots are built sequentially using layers, so it’s easy to edit and fine-tune the plots you generate.
When using ggplot2, it is essential that
your data are tidy! If they are not, the functions probably will not
look like you expect.
Let’s work through how to build a plot layer by layer.
ggplot2 refers to the name of the package itself. When using the package we use the function ggplot() to generate the plots, and so references to using the function will be referred to as ggplot() and the package as a whole as ggplot2.
Cheatsheet - ggplot2 Cheatsheet
Produce scatter plots, boxplots, and time series plots using ggplot.
Modify the aesthetics of an existing ggplot plot (including axis labels and color).
Build complex and customized plots from data in a data frame.
To build a ggplot, we will use the following basic template that can be used for different types of plots:
Format – ggplot(data = , mapping =
aes(
First, let’s initialize a plot. Use the data
parameter to tell ggplot what data frame to use. It should
be tidy data, in either a data.frame or
tibble!
Use the ggplot() function and bind the plot to a specific data frame using the data argument.
Add an aesthetic using the function aes() within the
initial ggplot() call.
Define an aesthetic mapping (using the aesthetic (aes) function), by selecting the variables to be plotted and specifying how to present them in the graph, e.g., as x/y positions or characteristics such as size, shape, color, etc.
Controls our axes variables as well as graphical parameters such as color, size, shape.
Now ggplot knows what to plot, but it doesn’t know
how to plot it yet. Let’s add some points with
geom_point().
Add ‘geoms’ – graphical representations of the data in the plot (points, lines, bars). ggplot2 offers many different geoms; we will use some common ones today, including:
geom_point() for scatter plots, dot plots, etc.
geom_boxplot() for, well, boxplots!
geom_line() for trend lines, time series, etc.
This is a new layer! We add layers using the +
operator.
ggplot knows what to plot, but it doesn’t know how
to plot it yet. Let’s add some points with
geom_point().gapminder_plot <- ggplot(data = gapminder,
aes(x = year, y = lifeExp))
gapminder_plot +
geom_point()Building plots with ggplot2 is typically an iterative process. We start by defining the dataset we’ll use, lay out the axes, and choose a geom.
You can start modifying this plot to extract more information from it. For instance, sometimes we can add transparency (alpha) to avoid overplotting.
## Plant Type Treatment conc uptake
## 1 Qn1 Quebec nonchilled 95 16.0
## 2 Qn1 Quebec nonchilled 175 30.4
## 3 Qn1 Quebec nonchilled 250 34.8
## 4 Qn1 Quebec nonchilled 350 37.2
## 5 Qn1 Quebec nonchilled 500 35.3
## 6 Qn1 Quebec nonchilled 675 39.2
## Plant Type Treatment conc uptake
## Qn1 : 7 Quebec :42 nonchilled:42 Min. : 95 Min. : 7.70
## Qn2 : 7 Mississippi:42 chilled :42 1st Qu.: 175 1st Qu.:17.90
## Qn3 : 7 Median : 350 Median :28.30
## Qc1 : 7 Mean : 435 Mean :27.21
## Qc3 : 7 3rd Qu.: 675 3rd Qu.:37.12
## Qc2 : 7 Max. :1000 Max. :45.50
## (Other):42
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
ggplot(data = CO2,
aes(x = conc, y = uptake, color = Treatment)) +
geom_point() +
geom_smooth(method = lm, se = F)## `geom_smooth()` using formula = 'y ~ x'
ggplot(data = CO2,
aes(x = conc, y = uptake, color = Treatment)) +
geom_point() +
geom_smooth(method = lm, se = T)## `geom_smooth()` using formula = 'y ~ x'
ggplot has a special technique called faceting that allows the user to split one plot into multiple plots based on a factor included in the dataset.
We will use it to make a plot for each treatment:
ggplot(data = CO2,
aes(x = conc, y = uptake, color = Treatment)) +
geom_point() +
geom_smooth(method = lm, se = F) +
facet_wrap(facets = vars(Type))## `geom_smooth()` using formula = 'y ~ x'
ggplot(data = CO2,
aes(x = conc, y = uptake, color = Treatment)) +
geom_point() +
geom_smooth(method = lm, se = F) +
facet_wrap(~Type)## `geom_smooth()` using formula = 'y ~ x'
ggplot(data = CO2,
aes(x = conc, y = uptake, color = Treatment)) +
geom_point() +
geom_smooth(method = lm, se = F) +
facet_grid(rows = vars(Type), cols = vars(Treatment))## `geom_smooth()` using formula = 'y ~ x'
ggplot(data = CO2,
aes(x = conc, y = uptake, color = Treatment)) +
geom_point() +
geom_smooth(method = lm, se = F) +
facet_grid(cols = vars(Type))## `geom_smooth()` using formula = 'y ~ x'
Usually plots with white background look more readable when printed. Every single component of a ggplot graph can be customized using the generic theme() function, as we will see below.
For example, we can change our previous graph to have a simpler white background using the theme_bw() function:
ggplot(data = CO2,
aes(x = conc, y = uptake, color = Treatment)) +
geom_point() +
geom_smooth(method = lm, se = T) +
facet_wrap(~Type) +
labs(title = "Concerntration of CO2") +
theme_bw()## `geom_smooth()` using formula = 'y ~ x'
ggplot(data = CO2,
aes(x = conc, y = uptake, color = Treatment)) +
geom_point() +
geom_smooth(method = lm, se = T) +
facet_wrap(~Type) +
labs(title = "Concerntration of CO2") +
theme_bw() +
theme(text=element_text(size = 20))## `geom_smooth()` using formula = 'y ~ x'
We want to explore differences across continents, so let’s color by continent
aes() because we want to color by something in
our dataaes() will automatically add a
labelgeom_line(), or it
will override the aes()ggplot(data = gapminder,
aes(x = year, y = lifeExp, group = country, color = continent)) +
geom_line(size = 0.4) ## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
This is cluttered and hard to read. Let’s try separating by continents using facets!
facet_wrap~ and then the variable name you wantggplot(data = gapminder,
aes(x = year, y = lifeExp, group = country, color = continent)) +
geom_line(size = 0.4) +
geom_smooth(aes(group = continent),
se = FALSE, size = 1.5,
color = "black",
method = "loess") +
facet_wrap(~ continent) ## `geom_smooth()` using formula = 'y ~ x'
Looking better! Now let’s fine tune. First we’ll formalize the
labels on our plot using labs().
xlab(),
ylab(), ggmain(), …ggplot(data = gapminder,
aes(x = year, y = lifeExp,
group = country,
color = continent)) +
geom_line(size = 0.5) +
geom_smooth(aes(group = continent),
se = FALSE, size = 1.5,
color = "black",
method = "loess") +
facet_wrap(~ continent) +
labs(title = "Life expectancy over time by continent",
x = "Year", y = "Life Expectancy",
legend = "Continent")ggplot(data = gapminder,
aes(x = year, y = lifeExp,
group = country,
color = continent)) +
geom_line(size = 0.5) +
geom_smooth(aes(group = continent),
se = FALSE, size = 1.5,
color = "black",
method = "loess") +
facet_wrap(~ continent) +
labs(title = "Life expectancy over time by continent",
x = "Year", y = "Life Expectancy",
legend = "Continent") ggplot(data = gapminder,
aes(x = year, y = lifeExp,
group = country,
color = continent)) +
geom_line(size = 0.5) +
geom_smooth(aes(group = continent),
se = FALSE, size = 1.5, color = "black", method = "loess") +
facet_wrap(~ continent) +
labs(title = "Life expectancy over time by continent",
x = "Year", y = "Life Expectancy") +
theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 14),
legend.position = "none") Great, that looks good! Now we can store it as an object.
lifeExp_plot <- ggplot(data = gapminder,
aes(x = year, y = lifeExp,
group = country,
color = continent)) +
geom_line(size = 0.5) +
geom_smooth(aes(group = continent),
se = FALSE, size = 1.5, color = "black", method = "loess") +
facet_wrap(~ continent) +
labs(title = "Life expectancy over time by continent",
x = "Year", y = "Life Expectancy") +
theme_bw(base_size = 16) +
theme(plot.title = element_text(hjust = 0.5, face = "bold"),
legend.position = "none",
axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),
panel.spacing.x = unit(0.75, "cm")) ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(data = gapminder,
aes(x = lifeExp)) +
geom_histogram(binwidth = 1,
color = "black",
fill = "lightblue") ggplot(data = gapminder,
aes(x = lifeExp)) +
geom_histogram(binwidth = 1,
color = "black",
fill = "lightblue") +
theme_bw(base_size = 20) +
labs(x = "Life Expectancy",
y = "Count")ggplot(data = gapminder,
aes(x = continent, y = lifeExp)) +
geom_boxplot(fill = "lightblue") +
labs(title = "Life expectancy by Continent",
x = "",
y = "") +
theme(plot.title =
element_text(hjust = 0.5)) +
ylim(c(20, 85)) ggplot(data = gapminder,
aes(x = continent, y = lifeExp)) +
geom_boxplot(fill = "lightblue") +
labs(title = "Life expectancy by Continent",
x = "",
y = "") +
theme(plot.title =
element_text(hjust = 0.5)) +
ylim(c(0, 85)) *Don’ts
*Do’s
xlim(), ylim()aes(), edit within
theme() or guides()geom_point(), geom_line(),
geom_histogram(), geom_bar(),
geom_boxplot(), geom_text()facet_grid(), facet_wrap() for
facetinglabs() for labelstheme_bw() to make things look nicercolor for color,
alpha for opacity, lwd/size for
thickness, shape for shape, fill for interior
color, …geom_histogram()ggplot(gapminder, aes(x = lifeExp)) +
geom_histogram() +
labs(x = "Life Expectancy",
y = "Count",
title = "Distribution of Life Expectancy") +
theme_bw(base_size = 16) +
theme(plot.title =
element_text(hjust = 0.5)) geom_boxplot()ggplot(gapminder, aes(y = lifeExp)) +
geom_boxplot() +
labs(x = "",
y = "Life Expectancy",
title = "Distribution of Life Expectancy") +
theme_bw(base_size = 16) +
theme(plot.title =
element_text(hjust = 0.5),
axis.ticks.x = element_blank(),
axis.text.x = element_blank()) +
ylim(c(0,90))geom_bar()ggplot(gapminder, aes(x = continent)) +
geom_bar() +
labs(x = "Continent",
y = "Count",
title = "Distribution of Continent") +
theme_bw(base_size = 16) +
theme(plot.title =
element_text(hjust = 0.5)) geom_boxplot()Here, we have multiple observations for each category.
ggplot(gapminder, aes(x = continent, y = lifeExp)) +
geom_boxplot() +
labs(x = "Continent",
y = "Life Expectancy",
title = "Life Expectancy by Continent") +
theme_bw(base_size = 16) +
theme(plot.title =
element_text(hjust = 0.5)) geom_bar() (with argument
stat = "identity")
Here we have only one observation per category.
mean_lifeExp <- gapminder %>%
group_by(continent) %>%
summarise(mean_lifeExp = mean(lifeExp))
ggplot(mean_lifeExp, aes(x = continent, y = mean_lifeExp)) +
geom_bar(stat = "identity") +
labs(x = "Continent",
y = "Mean Life Expectancy",
title = "Mean Life Expectancy by Continent") +
theme_bw(base_size = 16) +
theme(plot.title =
element_text(hjust = 0.5)) geom_point()ggplot(gapminder, aes(y = lifeExp, x = log(gdpPercap))) +
geom_point() +
labs(y = "Life expectancy",
x = "Log GDP per capita",
title = "Life Expectancy by Log GDP per capita") +
ylim(c(0,90)) +
theme_bw(base_size = 16) +
theme(plot.title =
element_text(hjust = 0.5)) geom_bar() setting x and fill
within aes()gapminder <- gapminder %>%
mutate(lifeExpCat = ifelse(lifeExp > 60, TRUE, FALSE))
ggplot(gapminder, aes(x = continent, fill = lifeExpCat)) +
geom_bar() +
xlab("Continent") +
ylab("Count") +
labs(fill = "Life Exp. > 60") +
ggtitle("Distribution of Continent and Life Expectancy") +
theme_bw(base_size = 16) +
theme(plot.title =
element_text(hjust = 0.5)) Let’s preview the data.
## # A tibble: 6 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
select() to choose certain columns.## # A tibble: 1,704 × 2
## lifeExp gdpPercap
## <dbl> <dbl>
## 1 28.8 779.
## 2 30.3 821.
## 3 32.0 853.
## 4 34.0 836.
## 5 36.1 740.
## 6 38.4 786.
## 7 39.9 978.
## 8 40.8 852.
## 9 41.7 649.
## 10 41.8 635.
## # … with 1,694 more rows
Use select() to choose certain columns. We can also use
select() to remove certain columns by putting a
- symbol in front.
## # A tibble: 1,704 × 4
## country continent year pop
## <fct> <fct> <int> <int>
## 1 Afghanistan Asia 1952 8425333
## 2 Afghanistan Asia 1957 9240934
## 3 Afghanistan Asia 1962 10267083
## 4 Afghanistan Asia 1967 11537966
## 5 Afghanistan Asia 1972 13079460
## 6 Afghanistan Asia 1977 14880372
## 7 Afghanistan Asia 1982 12881816
## 8 Afghanistan Asia 1987 13867957
## 9 Afghanistan Asia 1992 16317921
## 10 Afghanistan Asia 1997 22227415
## # … with 1,694 more rows
select() has a number of helper functions to select
columns (see ?select). Notably:
starts_with() for prefixesends_with() for suffixescontains() for strings## [1] "country" "continent" "year" "lifeExp" "pop" "gdpPercap"
## # A tibble: 6 × 2
## country continent
## <fct> <fct>
## 1 Afghanistan Asia
## 2 Afghanistan Asia
## 3 Afghanistan Asia
## 4 Afghanistan Asia
## 5 Afghanistan Asia
## 6 Afghanistan Asia
select() has a number of helper functions to select
columns (see ?select). Notably:
starts_with() for prefixesends_with() for suffixescontains() for strings## [1] "country" "continent" "year" "lifeExp" "pop" "gdpPercap"
## # A tibble: 6 × 3
## lifeExp pop gdpPercap
## <dbl> <int> <dbl>
## 1 28.8 8425333 779.
## 2 30.3 9240934 821.
## 3 32.0 10267083 853.
## 4 34.0 11537966 836.
## 5 36.1 13079460 740.
## 6 38.4 14880372 786.
select() has a number of helper functions to select
columns (see ?select). Notably:
starts_with() for prefixesends_with() for suffixescontains() for strings## [1] "country" "continent" "year" "lifeExp" "pop" "gdpPercap"
## # A tibble: 6 × 1
## gdpPercap
## <dbl>
## 1 779.
## 2 821.
## 3 853.
## 4 836.
## 5 740.
## 6 786.
select() has a number of helper functions to select
columns (see ?select). Notably:
starts_with() for prefixesends_with() for suffixescontains() for stringscategory_01, category_02,
category_03, …)Create and transform variables from existing variables using
mutate().
mutate(NEW_VAR_NAME = ...).
## # A tibble: 6 × 7
## country continent year lifeExp pop gdpPercap gdp
## <fct> <fct> <int> <dbl> <int> <dbl> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779. 6567086330.
## 2 Afghanistan Asia 1957 30.3 9240934 821. 7585448670.
## 3 Afghanistan Asia 1962 32.0 10267083 853. 8758855797.
## 4 Afghanistan Asia 1967 34.0 11537966 836. 9648014150.
## 5 Afghanistan Asia 1972 36.1 13079460 740. 9678553274.
## 6 Afghanistan Asia 1977 38.4 14880372 786. 11697659231.
Create and transform variables from existing variables using
mutate().
mutate(NEW_VAR_NAME = ...).
Note that we can use existing columns, including those we created
within the same call to mutate().
## # A tibble: 6 × 8
## country continent year lifeExp pop gdpPercap gdp logGDP
## <fct> <fct> <int> <dbl> <int> <dbl> <dbl> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779. 6567086330. 22.6
## 2 Afghanistan Asia 1957 30.3 9240934 821. 7585448670. 22.7
## 3 Afghanistan Asia 1962 32.0 10267083 853. 8758855797. 22.9
## 4 Afghanistan Asia 1967 34.0 11537966 836. 9648014150. 23.0
## 5 Afghanistan Asia 1972 36.1 13079460 740. 9678553274. 23.0
## 6 Afghanistan Asia 1977 38.4 14880372 786. 11697659231. 23.2
group_by()
You can use group_by() to define a grouping of the
rows of your data based on the columns.
Notice that it doesn’t change our data, except for that we can now see that it has 5 groups.
## # A tibble: 4 × 6
## # Groups: continent [1]
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
group_by()## # A tibble: 4 × 6
## # Groups: continent [1]
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Zimbabwe Africa 1992 60.4 10704340 693.
## 2 Zimbabwe Africa 1997 46.8 11404948 792.
## 3 Zimbabwe Africa 2002 40.0 11926563 672.
## 4 Zimbabwe Africa 2007 43.5 12311143 470.
group_by()
You can use group_by() to define a grouping of the
rows of your data based on the columns.
We can remove the groups with ungroup().
## # A tibble: 6 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
summarize()
summarize() (or summarise(), if you
prefer) will apply functions to rows of a data frame. If a data frame is
grouped, the function will be applied by group.
gapminder %>%
group_by(continent) %>%
summarize(mean_lifeExp = mean(lifeExp, na.rm = TRUE),
max_gdp = max(gdpPercap))## # A tibble: 5 × 3
## continent mean_lifeExp max_gdp
## <fct> <dbl> <dbl>
## 1 Africa 48.9 21951.
## 2 Americas 64.7 42952.
## 3 Asia 60.1 113523.
## 4 Europe 71.9 49357.
## 5 Oceania 74.3 34435.
summarize()gapminder %>%
group_by(continent) %>%
summarize(summary_lifeExp = mean(lifeExp, na.rm = TRUE), median(lifeExp, na.rm = TRUE), sd(lifeExp, na.rm = TRUE))## # A tibble: 5 × 4
## continent summary_lifeExp `median(lifeExp, na.rm = TRUE)` sd(lifeExp, na.rm …¹
## <fct> <dbl> <dbl> <dbl>
## 1 Africa 48.9 47.8 9.15
## 2 Americas 64.7 67.0 9.35
## 3 Asia 60.1 61.8 11.9
## 4 Europe 71.9 72.2 5.43
## 5 Oceania 74.3 73.7 3.80
## # … with abbreviated variable name ¹`sd(lifeExp, na.rm = TRUE)`
arrange()
We can order the rows of the data using arrange().
Usually, you do this after some call to
summarize().
gapminder %>%
group_by(continent) %>%
summarize(mean_lifeExp = mean(lifeExp, na.rm = TRUE),
max_gdp = max(gdpPercap)) %>%
arrange(mean_lifeExp)## # A tibble: 5 × 3
## continent mean_lifeExp max_gdp
## <fct> <dbl> <dbl>
## 1 Africa 48.9 21951.
## 2 Asia 60.1 113523.
## 3 Americas 64.7 42952.
## 4 Europe 71.9 49357.
## 5 Oceania 74.3 34435.
arrange()
We can order the rows of the data using arrange().
Usually, you do this after some call to summarize(). We can
use desc() to put in decreasing order.
gapminder %>%
group_by(continent) %>%
summarize(mean_lifeExp = mean(lifeExp, na.rm = TRUE),
max_gdp = max(gdpPercap)) %>%
arrange(desc(mean_lifeExp))## # A tibble: 5 × 3
## continent mean_lifeExp max_gdp
## <fct> <dbl> <dbl>
## 1 Oceania 74.3 34435.
## 2 Europe 71.9 49357.
## 3 Americas 64.7 42952.
## 4 Asia 60.1 113523.
## 5 Africa 48.9 21951.
pull() to extract a single column as a
vector. Notice the difference between## # A tibble: 4 × 1
## lifeExp
## <dbl>
## 1 28.8
## 2 30.3
## 3 32.0
## 4 34.0
## [1] 28.801 30.332 31.997 34.020 36.088 38.438 39.854 40.822 41.674 41.763
pull() to extract a single column as a
vector. Notice the difference between## # A tibble: 4 × 1
## continent
## <fct>
## 1 Asia
## 2 Asia
## 3 Asia
## 4 Asia
## # A tibble: 20 × 1
## continent
## <fct>
## 1 Asia
## 2 Asia
## 3 Asia
## 4 Asia
## 5 Asia
## 6 Asia
## 7 Asia
## 8 Asia
## 9 Asia
## 10 Asia
## 11 Asia
## 12 Asia
## 13 Europe
## 14 Europe
## 15 Europe
## 16 Europe
## 17 Europe
## 18 Europe
## 19 Europe
## 20 Europe
rename(data, NEW_VAR_NAME = OLD_VAR_NAME)
Recall, want to change the variable lifeExp to life_exp
## # A tibble: 6 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
rename(data, NEW_VAR_NAME = OLD_VAR_NAME)
Recall, want to change the variable lifeExp to life_exp
## # A tibble: 1,704 × 6
## country continent year life_exp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
## 7 Afghanistan Asia 1982 39.9 12881816 978.
## 8 Afghanistan Asia 1987 40.8 13867957 852.
## 9 Afghanistan Asia 1992 41.7 16317921 649.
## 10 Afghanistan Asia 1997 41.8 22227415 635.
## # … with 1,694 more rows