My df is (output2):
output2 <- data.frame(Country=c(Turkey, Italy, Spain, Turkey, Italy, Spain),
Year=c(1990, 1990,1990,1991,1991,1991),
v2x_regime=c(0,1,1,1,0,0),
SupDem=c(0.9554,0.8685, 0.7849,0.9382,0.2839,0.39383))
library("rnaturalearth")
library("rnaturalearthdata")
world <- ne_countries(scale = "medium", returnclass = "sf")
class(world)
plotdata <- dplyr::left_join(world, filter(output2, Year == 2020), by = c("admin" = "Country"))
ggplot(data = plotdata)
geom_sf(aes(fill = SupDem))
scale_fill_viridis_c(option = "plasma")
ggtitle("Democratic Support - 2020")
This only maps the value for 2020. I wanna see how it changes from the beginning of the data set to the end of it. For example from 1990 to 2020.
CodePudding user response:
One option would be to use gganimate
which using transition_states(Year)
allows to animate your maps over time.
Note: As your example includes only data for two years I used some fake random data which includes 6 years. Moreover, for the reprex I first added a background world map and included only the data for the three countries for the animated part of the map.
library("rnaturalearth")
library("rnaturalearthdata")
library(gganimate)
#> Loading required package: ggplot2
world <- ne_countries(scale = "medium", returnclass = "sf")
world <- dplyr::select(world, admin, geometry)
plotdata <- dplyr::inner_join(world, output2, by = c("admin" = "Country"))
ggplot()
geom_sf(data = world, size = .1)
geom_sf(aes(fill = SupDem), data = plotdata)
scale_fill_viridis_c(option = "plasma")
ggtitle("Democratic Support - {closest_state}")
transition_states(Year)
DATA
output2 <- data.frame(
Country = rep(c("Turkey", "Italy", "Spain"), each = 7),
Year = rep(seq(1990, 2020, by = 5), 3),
SupDem = sample(c(0.9554, 0.8685, 0.7849, 0.9382, 0.2839, 0.39383), 7 * 3, replace = TRUE)
)