gapminder from package gapminder# Select Data Needed
## Filter, Select, and Arrange Data
life1952 <- gapminder %>%
filter(year == 1952) %>%
arrange(desc(lifeExp))
## Select Rows
ten1952 <- rbind(top_n(life1952, 5, lifeExp), top_n(life1952, -5, lifeExp))
print(ten1952)
## # A tibble: 10 x 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Norway Europe 1952 72.7 3327728 10095.
## 2 Iceland Europe 1952 72.5 147962 7268.
## 3 Netherlands Europe 1952 72.1 10381988 8942.
## 4 Sweden Europe 1952 71.9 7124673 8528.
## 5 Denmark Europe 1952 70.8 4334000 9692.
## 6 Mozambique Africa 1952 31.3 6446316 469.
## 7 Sierra Leone Africa 1952 30.3 2143249 880.
## 8 Angola Africa 1952 30.0 4232095 3521.
## 9 Gambia Africa 1952 30 284320 485.
## 10 Afghanistan Asia 1952 28.8 8425333 779.
# Plot
ten1952$country <- factor(ten1952$country, levels = ten1952$country)
ggplot(ten1952, aes(x = country, y = lifeExp)) +
geom_col() +
labs(title = "Median Life Expectancy in 1952", subtitle = "Top 5 and Bottom 5 Countries", y = "MLE in Years") +
theme(plot.title = element_text(hjust = 0.5, size = 30, color = "blue"),
plot.subtitle = element_text(hjust = 0.5, size = 20, color = "sky blue"),
axis.title.x = element_blank(),
axis.text.x = element_text(size = 8, angle = 30))
# Select Data Needed
## Filter, Select, and Arrange Data
life2007 <- gapminder %>%
filter(year == 2007) %>%
arrange(desc(lifeExp))
## Select Rows
ten2007 <- rbind(top_n(life2007, 5, lifeExp), top_n(life2007, -5, lifeExp))
print(ten2007)
## # A tibble: 10 x 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Japan Asia 2007 82.6 127467972 31656.
## 2 Hong Kong, China Asia 2007 82.2 6980412 39725.
## 3 Iceland Europe 2007 81.8 301931 36181.
## 4 Switzerland Europe 2007 81.7 7554661 37506.
## 5 Australia Oceania 2007 81.2 20434176 34435.
## 6 Lesotho Africa 2007 42.6 2012649 1569.
## 7 Sierra Leone Africa 2007 42.6 6144562 863.
## 8 Zambia Africa 2007 42.4 11746035 1271.
## 9 Mozambique Africa 2007 42.1 19951656 824.
## 10 Swaziland Africa 2007 39.6 1133066 4513.
# Plot
ten2007$country <- factor(ten2007$country, levels = ten2007$country)
ggplot(ten2007, aes(x = country, y = lifeExp)) +
geom_col() +
labs(title = "Median Life Expectancy in 2007", subtitle = "Top 5 and Bottom 5 Countries", y = "MLE in Years") +
theme(plot.title = element_text(hjust = 0.5, size = 30, color = "blue"),
plot.subtitle = element_text(hjust = 0.5, size = 20, color = "sky blue"),
axis.title.x = element_blank(),
axis.text.x = element_text(size = 8, angle = 30))
# Top 5 Countries based on 2007 population
top5Name <- gapminder %>%
filter(year == 2007) %>%
arrange(desc(pop)) %>%
top_n(5, pop)
top5Name$country
## [1] China India United States Indonesia Brazil
## 142 Levels: Afghanistan Albania Algeria Angola Argentina Australia ... Zimbabwe
# Summary Table
top5 <- gapminder %>%
filter(country == top5Name$country[1] |
country == top5Name$country[2] |
country == top5Name$country[3] |
country == top5Name$country[4] |
country == top5Name$country[5])
top5$country <- factor(top5$country, levels = as.vector(top5Name$country))
datatable(top5)
# Plot
ggplot(top5, aes(x = year, y = lifeExp, color = country)) +
geom_line() +
labs(title = "Median Life Expectancy \n for the Largest Five Countries", y = "MLE in Years") +
theme(plot.title = element_text(hjust = 0.5, size = 20, color = "blue"),
axis.title.x = element_blank())
Let’s say the median life expectancy of a continent is the median value of MLE in that continent for that specific year.
# Create new column for continent median
continentMLE <- gapminder %>%
group_by(continent, year) %>%
summarize(contMLE = median(lifeExp, na.rm = TRUE))
datatable(continentMLE)
# Plot
ggplot(continentMLE, aes(x = year, y = contMLE, color = continent)) +
geom_line() +
labs(title = "Median Life Expectancy \n for Each Continent", y = "MLE in Years") +
theme(plot.title = element_text(hjust = 0.5, size = 20, color = "blue"),
axis.title.x = element_blank())