pacman::p_load(readxl, gifski, gapminder,
plotly, gganimate, tidyverse)Hands-on_Ex03_2
col <- c("Country", "Continent")
globalPop <- read_xls("data/GlobalPopulation.xls",
sheet="Data") %>%
mutate_at(col, as.factor) %>%
mutate(Year = as.integer(Year))globalPop# A tibble: 6,204 × 6
Country Year Young Old Population Continent
<fct> <int> <dbl> <dbl> <dbl> <fct>
1 Afghanistan 1996 83.6 4.5 21560. Asia
2 Afghanistan 1998 84.1 4.5 22913. Asia
3 Afghanistan 2000 84.6 4.5 23898. Asia
4 Afghanistan 2002 85.1 4.5 25268. Asia
5 Afghanistan 2004 84.5 4.5 28514. Asia
6 Afghanistan 2006 84.3 4.6 31057 Asia
7 Afghanistan 2008 84.1 4.6 32738. Asia
8 Afghanistan 2010 83.7 4.6 34505. Asia
9 Afghanistan 2012 82.9 4.6 36416. Asia
10 Afghanistan 2014 82.1 4.7 38327. Asia
# ℹ 6,194 more rows
ggplot(globalPop, aes(x = Old, y = Young,
size = Population,
colour = Country)) +
geom_point(alpha = 0.7,
show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(title = 'Year: {frame_time}',
x = '% Aged',
y = '% Young') +
transition_time(Year) +
ease_aes('linear')
gg <-ggplot(globalPop, aes(x = Old, y = Young,
size = Population,
colour = Country)) +
geom_point(aes(size = Population,
frame = Year),
alpha = 0.7,
show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(title = 'Year: {frame_time}',
x = '% Aged',
y = '% Young')Warning in geom_point(aes(size = Population, frame = Year), alpha = 0.7, :
Ignoring unknown aesthetics: frame
ggplotly(gg)Warning in p$x$data[firstFrame] <- p$x$frames[[1]]$data: number of items to
replace is not a multiple of replacement length