R que R

Data visualization: Interactive and animated maps with {ggplot2} and {plotly} (Example III)

Sat, Mar 28, 2020
R
#maps

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(echarts4r)
library(gganimate)
library(ggiraph)
library(widgetframe)
library(ggthemes)
library(plotly)
library(viridis)
library(DT)


Datasets


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, "\\(.*"))


Meteorites dataset



meteoritos <- readr::read_csv("https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2019/2019-06-11/meteorites.csv")

options(scipen = 999) 

meteoritos_Mundo <- meteoritos %>%
  filter(fall == "Fell") %>%
  filter(year > 1800) %>%
  drop_na() %>%
  arrange(mass)


Example 3



mapa_animado_3 <- gapminder_data %>%
 filter(year == 2007) %>%
  right_join(world, by= c(mapname = "region")) %>%
  ggplot(aes(long, lat, 
             group= group, 
             fill= lifeExp)) +
  geom_polygon(color = "white", 
               size = 0.01) +
  theme_void() +
  scale_fill_viridis(option = "B",
                     name= "Years") +
  labs(title="Life Expectancy",
       subtitle = "year: 2007")  +
  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_3 <- ggplotly(mapa_animado_3)

fig_3