-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-gapminder-solution.R
50 lines (40 loc) · 1.59 KB
/
01-gapminder-solution.R
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# load required packages
library(tibble)
library(ggplot2)
library(gapminder)
# view header of the dataset
gapminder
# view help file for the dataset
?gapminder
# generate a histogram of life expectancy
ggplot(data = gapminder, mapping = aes(x = lifeExp)) +
geom_histogram()
# generate separate histograms of life expectancy for each continent
# Hint: think about how to split your plots to show different subsets of data
ggplot(data = gapminder, mapping = aes(x = lifeExp)) +
geom_histogram() +
facet_wrap(facets = vars(continent))
# compare the distribution of life expectancy, by continent by generating a boxplot
ggplot(data = gapminder, mapping = aes(x = continent, y = lifeExp)) +
geom_boxplot()
## redraw the plot, but this time use a violin plot
ggplot(data = gapminder, mapping = aes(x = continent, y = lifeExp)) +
geom_violin()
# generate a scatterplot of the relationship between per capita GDP and life expectancy
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point()
## add a smoothing line to the scatterplot
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point() +
geom_smooth()
## use color to identify whether this relationship differs by continent
ggplot(data = gapminder,
mapping = aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point() +
geom_smooth()
## bonus: identify the outlying countries on the right-side of the graph
## by labeling each observation with the country name
ggplot(data = gapminder,
mapping = aes(x = gdpPercap, y = lifeExp, label = country)) +
geom_smooth() +
geom_text()