Reproduce the examples shown in the Lab Manual PDF. Do not skip steps, this section confirms you worked through the lab.
Tip: copy and paste code from the manual, then run each chunk.
# Barplot of countries by continent
counts <- gapminder_df %>%
distinct(country, continent) %>%
count(continent, name = "count_of_countries")
counts
ggplot(counts, aes(x = continent, y = count_of_countries)) +
geom_col()
# Top 10 countries by life expectancy in 2007
top10_2007 <- gapminder_df %>%
filter(year == 2007) %>%
arrange(desc(lifeExp)) %>%
slice_head(n = 10)
ggplot(top10_2007, aes(x = country, y = lifeExp)) +
geom_col()
ggplot(top10_2007, aes(x = country, y = lifeExp)) +
geom_col() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
ggplot(top10_2007, aes(x = country, y = lifeExp, fill = country)) +
geom_col(show.legend = FALSE) +
labs(
title = "Top 10 countries by life expectancy, 2007",
x = "Country",
y = "Life expectancy"
) +
theme_classic() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# Plot of Life Expectancy Over Time (all countries)
ggplot(gapminder_df, aes(x = lifeExp)) +
geom_histogram(color = "white", bins = 40) +
theme_classic(base_size = 14) +
labs(x = "Life Expectancy", y = "Number of observations",
title = "Distribution of Life Expectancy (Gapminder data)")
# Plot of Life Expectancy Over Time (North America)
gapminder_df %>%
filter(country %in% c("United States", "Canada", "Mexico")) %>%
ggplot(aes(x = year, y = lifeExp, color = country)) +
geom_point() +
geom_line() +
labs(x = "Year", y = "Life Expectancy",
title = "Life Expectancy in North America (1952-2007)")
# Mode: table() on the small discrete vector from the manual
table(c(1, 1, 1, 1, 1, 1, 1, 2, 3, 4))
##
## 1 2 3 4
## 7 1 1 1
# Range of sample_numbers
set.seed(123)
sample_numbers <- rnorm(100, mean = 10, sd = 5)
hist(sample_numbers)
range(sample_numbers)
## [1] -1.545844 20.936665
# Histograms of the clustered vs. bimodal datasets (same mean and range, different shape)
clustered <- c(8, 9, 9, 10, 10, 10, 10, 11, 11, 12, 0, 20)
bimodal <- c(0, 0, 1, 2, 19, 20, 20, 18, 3, 1, 19, 17)
mean(clustered); range(clustered)
## [1] 10
## [1] 0 20
mean(bimodal); range(bimodal)
## [1] 10
## [1] 0 20
df_compare <- tibble(
value = c(clustered, bimodal),
group = rep(c("Clustered", "Bimodal"), each = 12)
)
ggplot(df_compare, aes(x = value)) +
geom_histogram(binwidth = 2, color = "white") +
facet_wrap(~ group) +
theme_classic(base_size = 13) +
labs(x = "Value", y = "Count")
# Mean life expectancy by continent (group_by + summarise)
gapminder_df %>%
group_by(continent) %>%
summarise(mean_lifeExp = mean(lifeExp)) %>%
knitr::kable(digits = 1)
| continent | mean_lifeExp |
|---|---|
| Africa | 48.9 |
| Americas | 64.7 |
| Asia | 60.1 |
| Europe | 71.9 |
| Oceania | 74.3 |
Now adapt what you learned to answer new questions.
# Task 1: Plot Life Expectancy by year for the five continents.
# Hint: use the "continent" factor and update the plot title accordingly.
ggplot(gapminder_df, aes(x = year, y = lifeExp, color = continent)) +
geom_point() +
geom_line() +
labs(
title = "Life Expectancy by Year for the Five Continents",
x = "Year",
y = "Life Expectancy"
)
# Task 2: Filter + group. Compute mean life expectancy by continent
# for 2007 ONLY, then rank continents from highest to lowest mean.
# Hint: filter() %>% group_by() %>% summarise() %>% arrange()
gapminder_df %>%
filter(year == 2007) %>%
group_by(continent) %>%
summarise(mean_lifeExp = mean(lifeExp)) %>%
arrange(desc(mean_lifeExp)) %>%
knitr::kable(digits = 1)
| continent | mean_lifeExp |
|---|---|
| Oceania | 80.7 |
| Europe | 77.6 |
| Americas | 73.6 |
| Asia | 70.7 |
| Africa | 54.8 |
# Task 3: Compute and print two tables of mean life expectancy by continent,
# one for 1952 and one for 2007.
lifeExp_1952 <- gapminder_df %>%
filter(year == 1952) %>%
group_by(continent) %>%
summarise(mean_lifeExp = mean(lifeExp))
knitr::kable(
lifeExp_1952,
digits = 1,
caption = "Mean Life Expectancy by Continent in 1952"
)
| continent | mean_lifeExp |
|---|---|
| Africa | 39.1 |
| Americas | 53.3 |
| Asia | 46.3 |
| Europe | 64.4 |
| Oceania | 69.3 |
# Mean life expectancy by continent in 2007
lifeExp_2007 <- gapminder_df %>%
filter(year == 2007) %>%
group_by(continent) %>%
summarise(mean_lifeExp = mean(lifeExp))
knitr::kable(
lifeExp_2007,
digits = 1,
caption = "Mean Life Expectancy by Continent in 2007"
)
| continent | mean_lifeExp |
|---|---|
| Africa | 54.8 |
| Americas | 73.6 |
| Asia | 70.7 |
| Europe | 77.6 |
| Oceania | 80.7 |
Write one sentence identifying which continent improved the most and which improved the least (Task 3):
Asia improved the most, from 46.3 to 70.7, and Europe improved the least, from 64.4 to 77.6.
Answer in full sentences. Each question is worth 2 points (8 points total).
What do the bars on a histogram represent, and how does changing the number of bins change what you see? The bars show how many observations there are of something over certain ranges. It divides the data in groups, and when the number of bins change to a larger number, you can see more detail because data isn’t lumped in a few all encompassing bins.
Imagine two histograms: one is narrow with a tall peak, the other is wide and flat. Which shows more variability, and why? The lab manual discusses that variation is how spread out the values are in a dataset. The narrow histogram would have less variability since most of its values would be grouped more closely together. A wide and flat one would have more variability since it is spread out over a bigger area.
The clustered and bimodal datasets earlier in this lab had the exact same mean and the exact same range. Explain why relying on those two numbers alone would have given you a misleading picture of the data, and what the histograms showed that the numbers didn’t. If you went by those numbers, you would have a misleading picture because the bimodel has two modes at different sides of the data set, so even though they have the same mean and range, the data is distributed quite differently.The histograms showed the pattern of the data, unlike the mean and range.
Explain one weakness of using the range as your only measure of spread. In class we talked about how it is heavily affected by extremes, so if there are outliers, it heavily accounts for those and might be a good indicator of how the data is spread out. Data might be mostly clumped in the middle, but if there is a high and low outlier, the range wouldn’t show that pattern.