Begin by loading the required packages. If you don’t have these installed (or don’t know whether you have them installed), you can install them by executing the following code in your console:
install.packages("tidyverse")
install.packages("scales")
install.packages("tsibble")
Now run this code chunk to load the packages:
suppressPackageStartupMessages(library(tidyverse))
suppressPackageStartupMessages(library(gapminder))
suppressPackageStartupMessages(library(scales))
suppressPackageStartupMessages(library(tsibble))
Consider the following plot. Don’t concern yourself with the code at this point.
gapminder %>%
filter(year == 2007) %>%
mutate(continent = fct_infreq(continent)) %>%
ggplot(aes(continent)) +
geom_bar() +
theme_bw()
Fill in the seven grammar components for this plot.
| Grammar Component | Specification |
|---|---|
| data | gapminder |
| aesthetic mapping | x: continent, y: count |
| geometric object | bar |
| scale | linear |
| statistical transform | count |
| coordinate system | cartesian (rectangular) |
| facetting | no |
ggplot2 Syntax (Your Turn)The following is a tsibble (a special type of tibble containing time series data, which we’ll see more of later), stored in the variable mauna, of CO\(_2\) concentrations collected monthly at the Mauna Loa station.
Execute this code to store the data in mauna:
# enclose in () to assign to variable AND print to console
(mauna <- tsibble::as_tsibble(co2) %>%
rename(month = index, conc = value))
## # A tsibble: 468 x 2 [1M]
## month conc
## <mth> <dbl>
## 1 1959 Jan 315.
## 2 1959 Feb 316.
## 3 1959 Mar 316.
## 4 1959 Apr 318.
## 5 1959 May 318.
## 6 1959 Jun 318
## 7 1959 Jul 316.
## 8 1959 Aug 315.
## 9 1959 Sep 314.
## 10 1959 Oct 313.
## # … with 458 more rows
Produce a line chart showing the concentration over time. Specifically, the plot should have the following grammar components:
| Grammar Component | Specification |
|---|---|
| data | mauna |
| aesthetic mapping | x: month, y: conc |
| geometric object | lines |
| scale | linear |
| statistical transform | none |
| coordinate system | rectangular |
| facetting | none |
Fill in the blanks to obtain the plot:
# use fig.width and fig.height to configure plot size
ggplot(mauna, aes(month, conc)) +
geom_line()
It turns out that you’re allowed to specify the aesthetic mappings in a geom layer instead of, or in addition to, in the ggplot() function, with the following rules:
geom layer apply only to that layer.ggplot() function and the geom layer, the geom layer takes precedence.The following code mistakenly puts the month variable on the y-axis. Fill in the FILL_THIS_IN so that you still obtain the same result as above.
# overwrite the value of y in geom_line
ggplot(mauna, aes(y = month)) +
geom_line(aes(month,conc)) + ylab("conc")
You can store the output of the plot in a variable, too. Store the plot from 2(a) in the variable named p, then add a layer to p that adds green points to the plot.
# p + geom_point(colour = "green")
(p <- ggplot(mauna, aes(month, conc)) +
geom_line())
p + geom_point(colour="green")
What’s wrong with the following code? Fix it.
ggplot(gapminder) +
geom_point(aes(x = gdpPercap, y = lifeExp), alpha = 0.1)