我正在使用以下代码来重现附带的gif。
library(wbstats)
library(dplyr)
library(tidyr)
library(purrr)
library(ggplot2)
library(sf)
library(rnaturalearth)
library(tmap)
wb_data_create = function(indicator, new_name, year, ...){
df = wb(indicator = indicator,
startdate = year, enddate = year, ...) %>%
select(iso_a2 = iso2c, value, year = date) %>%
mutate(indicator = new_name) %>%
spread(indicator, value) %>%
as_data_frame()
df
}
data_life_exp = seq(1966, 2016, by = 10) %>%
map_dfr(wb_data_create,
indicator = "SP.DYN.LE00.IN",
new_name = "life_exp",
country = "countries_only")
data_life_exp_avg = data_life_exp %>%
group_by(year) %>%
summarize(mean_life_exp = mean(life_exp, na.rm = TRUE))
ggplot()+
geom_line(data = data_life_exp,
aes(year, life_exp, group = iso_a2),
color = "gray40") +
geom_line(data = data_life_exp_avg,
aes(year, mean_life_exp, group = 1),
color = "red", size = 2) +
labs(x = "Año", y = "Esperanza de vida") +
theme_minimal()
world = ne_countries(returnclass = "sf") %>%
select(iso_a2, name_long, continent)
data_life_exp_wide = data_life_exp %>%
spread(year, life_exp)
world_temporal = world %>%
left_join(data_life_exp_wide, by = c("iso_a2")) %>%
gather(year, life_exp, '1966':'2016')
world_temporal_eu = filter(world_temporal,
continent == "Europe") %>%
na.omit()
my_ani3 = tm_shape(world_temporal_eu) +
tm_polygons("life_exp", title = "Esperanza de vida",
palette = "viridis") +
tm_facets(by = "name_long", along = "year", nrow = 3)
tmap_animation(my_ani3, filename = "life_exp_eu_animation.gif",
width = 1600, height = 1000, delay = 60)
但是,我不知道如何增加包含国家/地区的框的大小,因为某些国家/地区的名称更长,并且框恰好适合该国家/地区的大小。 另外,我不知道如何将图例放置在任何其他位置,这些位置可能会基于调整框后剩余的间隙或插槽。如果可能的话,最后一个问题是如何在图表中将“ to”一词更改为西班牙语(我必须用英语写出国家名称)。感谢大家的支持。