Re
In this post we will present a few examples of interactive and animated maps using both {ggplot2} and {plotly} .
This post is a continuation of the previous one ( interactive and animated maps ) .
Libraries / packages
This post is just a continuation of the previous post on animated and interactive maps. We will show four examples of maps created using {ggplot2} and {plotly}.
library(tidyverse)
library(gapminder)
library(gganimate)
library(widgetframe)
library(ggthemes)
library(plotly)
library(viridis)
gapminder dataset:
world <- map_data("world") %>%
filter(region != "Antarctica")
gapminder_codes <- gapminder::country_codes
gapminder <-gapminder::gapminder_unfiltered
gapminder <- gapminder %>%
inner_join(gapminder_codes, by= "country") %>%
mutate(code = iso_alpha)
gapminder_data <- gapminder %>%
inner_join(maps::iso3166 %>%
select(a3, mapname), by= c(code = "a3")) %>%
mutate(mapname = str_remove(mapname, "\\(.*"))
Example 4.
mapa_animado_4 <- gapminder_data %>%
filter(year %in% c(1992, 2007)) %>%
right_join(world, by= c(mapname = "region")) %>%
ggplot(aes(long, lat,
group= group,
fill= lifeExp)) +
geom_polygon(aes(frame = year),
color = "white",
size = 0.01) +
theme_void() +
scale_fill_viridis(option = "B",
name= "Years") +
labs(title="Life Expectancy") +
theme(
plot.title = element_text(size = 12, hjust = 0.5),
plot.subtitle = element_text(size = 10, hjust = 0.5),
plot.caption = element_text(size = 8, hjust = 1)) +
coord_fixed(ratio = 1.3)
fig_4 <- ggplotly(mapa_animado_4) %>% animation_slider(currentvalue = list(prefix = "Year ", font = list(color="orange")))
fig_4
If you can not see the plot click here