使用gganimate绘制含双geom_points的德国地图动态可视化时出现空白动画的问题求助
gganimate绘制含双geom_points的德国地图动态可视化时出现空白动画的问题求助
我最近尝试用gganimate制作一个包含两种geom_point的德国地图动态可视化,但最终只得到了空白的动画,实在搞不清哪里出问题了😭。
我想要实现的效果是:
- 绘制德国地图作为底图
- 用一组点根据
variable_of_interest的值(大于0.3标红,否则标蓝)展示不同位置随时间的变化 - 另一组点用季节对应的emoji符号(比如雪花代表冬季)来显示当前季节
以下是我写的可复现代码,麻烦各位帮忙排查下问题:
# Load necessary packages pacman::p_load(ggplot2, tidyverse, maps, mapdata, gganimate, av, emojifont) # Production of example data { set.seed(123) index = rep(c(1:10), each = 100) ; year = rep(c(1951:1960), each = 10, times = 10); month = rep(c(1:10), each = 1, times = 100) variable_of_interest = runif(1000, min = 0, max = 0.4); season = rep(x = c("Winter", "Winter", "Spring", "Spring", "Spring", "Summer", "Summer", "Summer", "Autmn", "Autmn"), times = 100) lat = rep(c(seq(from = 48, to = 53, length = 10)), each = 100); lon = rep(c(seq(from = 7, to = 14, length = 10)), each = 100) Variable_of_Interest = as.data.frame(cbind(index, year, month, variable_of_interest, season, lat, lon)) Variable_of_Interest$month_of_year = paste0(Variable_of_Interest$year, "-", Variable_of_Interest$month) rownames(Variable_of_Interest) = NULL } # Transform columns from character to numeric Variable_of_Interest = mutate_at(Variable_of_Interest, c(1,2,3,4,6,7), as.numeric) # Preperation for the animation season_symbols = c("Spring" = emoji("cherry_blossom"), "Summer" = emoji("tropical_drink"), "Autmn" = emoji("fallen_leaf"), "Winter" = emoji("snowflake")) # Map of values of the variable of interest Variable_of_Interest_Map = ggplot(Variable_of_Interest) + geom_path(data = map_data("world","Germany"), aes(x = long, y = lat, group = group)) + coord_fixed(xlim = c(6, 15), ylim = c(47, 55)) + geom_point(aes(x=lon, y=lat, group = month_of_year, shape = season), size = 10) + scale_shape_manual(values = season_symbols, breaks = c("Spring", "Summer", "Autmn", "Winter")) + geom_point(aes(x=lon, y=lat, group = month_of_year), colour = ifelse(test = Variable_of_Interest$variable_of_interest > 0.3, I("red"), I("blue"))) + # scale_color_gradient(low="blue", high="yellow") + xlab("Longitude (degree)") + ylab("Latitude (degree)") + theme_bw() + transition_manual(frames = month_of_year) + theme(legend.position = "bottom", legend.title = element_text(size=12, face="bold"), panel.background = element_blank(), legend.background = element_blank()) + labs(title = '{unique(Variable_of_Interest$month_of_year)[as.integer(frame)]}', color = paste0("Variable of Interest"), shape = "season") anim_save("Variable-of-Interest.mp4", animate( Variable_of_Interest_Map, nframes = 1000, renderer = ffmpeg_renderer() ))
现在运行这段代码后生成的动画完全是空白的,没有任何地图或点显示,有没有大佬能帮我找出问题所在呀?
备注:内容来源于stack exchange,提问作者RibbitRibbit
相关产品推荐
相关产品推荐

