使用gganimate在R中创建动画地理地图

使用gganimate在R中创建动画地理地图,r,ggplot2,gganimate,R,Ggplot2,Gganimate,我可以在R中生成一些外观不错的choropleth贴图,例如,请参见以下内容 library(tidyverse) library(rnaturalearth) library(rnaturalearthdata) set.seed(1234) ww <- ne_countries(scale = "medium", returnclass = "sf") ll <- ww$name %>% length val <- sample(c("a","b","c",

我可以在R中生成一些外观不错的choropleth贴图,例如,请参见以下内容

library(tidyverse)
library(rnaturalearth) 
library(rnaturalearthdata)

set.seed(1234)

ww <- ne_countries(scale = "medium", returnclass = "sf")

ll <- ww$name %>% length

val <- sample(c("a","b","c","d"), ll, replace=T)
bb <- ne_download(type = "wgs84_bounding_box", category = "physical",
              returnclass = "sf")

ww <- ww %>% mutate(value=val)


gpl1 <- ggplot(data = ww) +
geom_sf(aes(fill=value),  col = "black", lwd = 0.3 )+
xlab(NULL) + ylab(NULL) +
ggtitle("World Export of Merchandise")+
geom_sf(data = bb, col = "grey", fill = "transparent") +
theme(plot.background = element_rect(fill = "white"),
      panel.background = element_rect(fill = 'white'),
      panel.grid.major = element_line(colour = "grey"),
      legend.position="top",
      plot.title = element_text(lineheight=.8, size=24, face="bold",
                                vjust=1),
      legend.text = element_text(vjust=.4,lineheight=1,size = 14),
      legend.title = element_text(vjust=1,lineheight=1, size=14,
                                  face="bold" ))+
coord_sf(crs = "+proj=eqearth +wktext") 

ggsave("test_world1.pdf", gpl1, width=6*1.618,height=5)
库(tidyverse)
图书馆(rnaturalearth)
图书馆(RNATuralLearthData)
种子集(1234)

ww我不是地理空间数据或gganimate方面的专家,但我通过以下操作获得了类似于您问题的答案。我们将以类似于您开始示例的方式开始,但我们也将加载gganimate包

library(tidyverse)
library(rnaturalearth) 
library(rnaturalearthdata)
library(gganimate) # also needs transformr

## Do all previous stuff
set.seed(1234)

ww <- ne_countries(scale = "medium", returnclass = "sf")

ll <- ww$name %>% length

val <- sample(c("a","b","c","d"), ll, replace=T)
bb <- ne_download(type = "wgs84_bounding_box", category = "physical",
                  returnclass = "sf")
ww <- ww %>% mutate(value=val)
然后我们制作动画:

ani <- animate(gpl1)

ani我既不是地理空间数据专家,也不是gganimate专家,但我通过以下操作获得了类似于您问题答案的东西。我们将以类似于您开始示例的方式开始,但我们也将加载gganimate包

library(tidyverse)
library(rnaturalearth) 
library(rnaturalearthdata)
library(gganimate) # also needs transformr

## Do all previous stuff
set.seed(1234)

ww <- ne_countries(scale = "medium", returnclass = "sf")

ll <- ww$name %>% length

val <- sample(c("a","b","c","d"), ll, replace=T)
bb <- ne_download(type = "wgs84_bounding_box", category = "physical",
                  returnclass = "sf")
ww <- ww %>% mutate(value=val)
然后我们制作动画:

ani <- animate(gpl1)

谢谢,但我想我找到了更简单的

library(tidyverse)

library(rnaturalearth) 
library(rnaturalearthdata)

library(gganimate)

set.seed(1234)

ww_ini <- ne_countries(scale = "medium", returnclass = "sf")

ll <- ww_ini$name %>% length

val <- sample(c("a","b","c","d"), ll, replace=T)



bb <- ne_download(type = "wgs84_bounding_box", category = "physical",
              returnclass = "sf")

ww <- ww_ini %>%
mutate(value=val)


gpl1 <- ggplot(data = ww) +
geom_sf(aes(fill=value),  col = "black", lwd = 0.3 )+
xlab(NULL) + ylab(NULL) +
ggtitle("World Export of Merchandise")+
 geom_sf(data = bb, col = "grey", fill = "transparent") +
theme(plot.background = element_rect(fill = "white"),
      panel.background = element_rect(fill = 'white'),
      panel.grid.major = element_line(colour = "grey"),
      legend.position="top",
      plot.title = element_text(lineheight=.8, size=24, face="bold",
                                vjust=1),
      legend.text = element_text(vjust=.4,lineheight=1,size = 14),
      legend.title = element_text(vjust=1,lineheight=1, size=14,
                                  face="bold" ))+
coord_sf(crs = "+proj=eqearth +wktext") 

ggsave("test_world1.pdf", gpl1, width=6*1.618,height=5)



values_years <- tibble(name=rep(ww$name,4),
                   year=c(rep(1,ll), rep(2,ll), rep(3, ll), rep(4, ll)),
                   value=sample(c("a","b","c","d"),4* ll, replace=T))


 ww_ext <- left_join(ww_ini, values_years, by="name")



gpl2 <- ggplot(data = ww_ext) +
geom_sf(aes(fill=value),  col = "black", lwd = 0.3 )+
xlab(NULL) + ylab(NULL) +
ggtitle("World Export of Merchandise")+
 geom_sf(data = bb, col = "grey", fill = "transparent") +
theme(plot.background = element_rect(fill = "white"),
      panel.background = element_rect(fill = 'white'),
      panel.grid.major = element_line(colour = "grey"),
      legend.position="top",
      plot.title = element_text(lineheight=.8, size=24, face="bold",
                                vjust=1),
      legend.text = element_text(vjust=.4,lineheight=1,size = 14),
      legend.title = element_text(vjust=1,lineheight=1, size=14,
                                  face="bold" ))+
 coord_sf(crs = "+proj=eqearth +wktext") +

transition_manual(year )

anim <- animate(gpl2)
库(tidyverse)
图书馆(rnaturalearth)
图书馆(RNATuralLearthData)
库(gganimate)
种子集(1234)

谢谢,但我想我找到了更简单的方法

library(tidyverse)

library(rnaturalearth) 
library(rnaturalearthdata)

library(gganimate)

set.seed(1234)

ww_ini <- ne_countries(scale = "medium", returnclass = "sf")

ll <- ww_ini$name %>% length

val <- sample(c("a","b","c","d"), ll, replace=T)



bb <- ne_download(type = "wgs84_bounding_box", category = "physical",
              returnclass = "sf")

ww <- ww_ini %>%
mutate(value=val)


gpl1 <- ggplot(data = ww) +
geom_sf(aes(fill=value),  col = "black", lwd = 0.3 )+
xlab(NULL) + ylab(NULL) +
ggtitle("World Export of Merchandise")+
 geom_sf(data = bb, col = "grey", fill = "transparent") +
theme(plot.background = element_rect(fill = "white"),
      panel.background = element_rect(fill = 'white'),
      panel.grid.major = element_line(colour = "grey"),
      legend.position="top",
      plot.title = element_text(lineheight=.8, size=24, face="bold",
                                vjust=1),
      legend.text = element_text(vjust=.4,lineheight=1,size = 14),
      legend.title = element_text(vjust=1,lineheight=1, size=14,
                                  face="bold" ))+
coord_sf(crs = "+proj=eqearth +wktext") 

ggsave("test_world1.pdf", gpl1, width=6*1.618,height=5)



values_years <- tibble(name=rep(ww$name,4),
                   year=c(rep(1,ll), rep(2,ll), rep(3, ll), rep(4, ll)),
                   value=sample(c("a","b","c","d"),4* ll, replace=T))


 ww_ext <- left_join(ww_ini, values_years, by="name")



gpl2 <- ggplot(data = ww_ext) +
geom_sf(aes(fill=value),  col = "black", lwd = 0.3 )+
xlab(NULL) + ylab(NULL) +
ggtitle("World Export of Merchandise")+
 geom_sf(data = bb, col = "grey", fill = "transparent") +
theme(plot.background = element_rect(fill = "white"),
      panel.background = element_rect(fill = 'white'),
      panel.grid.major = element_line(colour = "grey"),
      legend.position="top",
      plot.title = element_text(lineheight=.8, size=24, face="bold",
                                vjust=1),
      legend.text = element_text(vjust=.4,lineheight=1,size = 14),
      legend.title = element_text(vjust=1,lineheight=1, size=14,
                                  face="bold" ))+
 coord_sf(crs = "+proj=eqearth +wktext") +

transition_manual(year )

anim <- animate(gpl2)
库(tidyverse)
图书馆(rnaturalearth)
图书馆(RNATuralLearthData)
库(gganimate)
种子集(1234)
伊尼世界酒店