# load libraries
library(tidyverse)
In this complement mini-assignment, you are going to use the following data set.
processed-20220221-owid-life-expectancy-vs-gdp-per-capita.csv
data set. (Max Roser & Ritchie, 2013)D1 <- read_csv("processed-20220221-owid-life-expectancy-vs-gdp-per-capita.csv")
plotly
. Modify the R code below where you choose four countries other than what is written. The code below makes an interactive time-series plots for four countries showing the gdp_per_capita
variable in time. Use the scale_color_colorblind()
function to change the colors.SOLUTIONS: Answers may vary.
# load packages
library(ggthemes)
library(plotly)
##### start: Write you chosen countries here
chosen_countries <- c("China","Mexico","India","Germany")
##### end: Write you chosen countries here
# subset the data
D1_sub <- D1 %>%
# Use the %in% command to filter-in only the chosen countries
filter(country %in% chosen_countries) %>%
drop_na()
# create a time-series plot
p1 <- ggplot(data = D1_sub, aes(x = year,
y = gdp_per_capita,
color = country)) +
geom_point() +
geom_line() +
#### start: Write plot modifications here
scale_color_colorblind() +
#### end: Write plot modifications here
labs(x = "year",
y = "gdp per capita",
title = "Yearly GDP per Capita")
# render the ggplot using plotly - interactive
ggplotly(p1)
Basic Animated Figure using gganimate
.
# load gganimate package
# you need to install gganimate, gifski, transformr and png packages & restart R
library(gganimate)
# animate the time-series
p1_anim <- p1 +
geom_point(aes(group = seq_along(year))) +
transition_reveal(year)
animate(p1_anim,
#fps = 20, duration = 15, # duration and frame rate
#height = 4, width = 8, units = "in", res = 150 # size and resolution
)
# save the animation
anim_save("solutions-time_series_gganimate.gif")
plotly
. Filter the data set to only include year 2018, do not include the “Antarctica” continent, and use the gdp_per_capita
and life_expectancy
variables to create an interactive scatter plot, use the facet_wrap()
to separate the figure by continent.SOLUTIONS: Answers may vary.
D1_2018 <- D1 %>%
# filter only 2018
filter(year == 2018) %>%
# exclude Antarctica in the continent variable
filter(continent != "Antarctica") %>%
# remove any rows with missing values
drop_na()
p2 <- ggplot(data = D1_2018, aes(x = gdp_per_capita, y = life_expectancy)) +
geom_point(aes(label=country)) +
facet_wrap(~continent) + # separate the plot by continent
labs(x = "gdp per capita",
y = "life expectancy",
title = "2018 - GDP per Capita vs Life Expectancy")
## Warning: Ignoring unknown aesthetics: label
ggplotly(p2)
Multidimensional Animated Figure using gganimate
. Create an animated scatter plot using the gdp_per_capita
and life_expectancy
variables while using the facet_wrap()
to separate the figure by continent. Do not include the “Antarctica” continent. Note that you need to use the year
variable to do the transitions. Save the resulting gif file.
D1_sub_sub <- D1 %>%
mutate(year = as.integer(year)) %>%
filter(continent != "Antarctica") %>%
drop_na()
p3 <- ggplot(data = D1_sub_sub,
aes(x = gdp_per_capita, y = life_expectancy)) +
geom_point() +
facet_wrap(~continent) +
labs(x = "gdp per capita",
y = "life expectancy",
subtitle = "Data Source: Our World in Data")
# animate the time-series
p3_anim <- p3 +
transition_reveal(year) +
labs(title = "GDP per Capita vs Life Expectancy")
animate(p3_anim)
# save the animation
anim_save("solutions-separated_plots_gganimate.gif")
Submit the resulting html and the gif files using the Google form.