开始之前

我之前用leaflet这个R包是画过自己的出差路线的, 但是无奈我的hugo主题并不支持我弄的太花哨, 所以javascript的动态路线是没办法展现的, 但是本地还是可以展现的, 所以本地只能自己看咯. 想学leaflet动态地图的可以看我的博客文章

除此以外, 我还用sf这个R包画过.可以直接跳转下面的附录 - 如果关注的是geom_sf怎么画的, 可以看我的微信公众号文章

主题

背景就不再多说了, 今天之所以又用R画图,是因为在twitter上看到的教程. 拿来学习一下, 并改称自己的出差版本.

先构建画图数据

想用fontawesome的图表来代替点, 所以就加载了emojifont包, 话说这个包还是Y叔的包.先搜索下图表.

search_fontawesome("plane")
## [1] "fa-paper-plane"   "fa-paper-plane-o" "fa-plane"
search_fontawesome("train")
## [1] "fa-train"
plane_city <- c("哈尔滨", "郑州", "广州", "北京", "呼和浩特")
search_fontawesome("train")
## [1] "fa-train"
post$emoji = ifelse(post$post_city %in% plane_city, fontawesome("fa-plane"), fontawesome("fa-train"))

然后就是画图了.

invisible(saveGIF({
    
    
    for (i in seq) {
        
        p1 <- ggplot(china) + geom_sf(size = 0.5, color = "#f0f0f0", fill = "#2a2a2a", 
            show.legend = FALSE) + geom_text(data = post %>% filter(post_city == 
            i), aes(x = long, y = lat, label = emoji, size = t_day + 1), color = "white", 
            family = "fontawesome-webfont", size = 6) + theme_void() + labs(title = "The Spring Travl") + 
            labs(caption = "by 王家轩 \n") + theme(legend.background = element_rect(fill = "#2a2a2a"), 
            legend.text = element_text(color = "white"), plot.background = element_rect(fill = "#FFD300", 
                color = "#FFD300"), panel.background = element_rect(fill = "#FFD300", 
                color = "#FFD300"), legend.position = c(0.11, 0.32), legend.key = element_rect(fill = "#2a2a2a", 
                color = NA), legend.title = element_text(color = "white", size = 20, 
                hjust = 0), plot.title = element_text(hjust = 0.5, size = 30, colour = "#2A2A2A", 
                face = "bold"), plot.subtitle = element_text(hjust = 0.5, size = 15, 
                face = "bold", colour = "#73d055ff"), plot.caption = element_text(hjust = 0.9, 
                size = 15, face = "bold", colour = "black")) + scale_size_continuous(range = c(1, 
            12))
        
        p3 <- ggplot(data = NULL, aes(x = seq, y = 1)) + geom_line() + geom_point(aes(fill = (x = seq %in% 
            i)), shape = 21, size = 5) + theme_void() + theme(legend.position = "none") + 
            scale_fill_manual(values = c("#b2d1e0", "gold")) + geom_text(aes(x = i, 
            y = 1, label = i), vjust = -1, size = 9, color = "#2a2a2a")
        
        print(ggpubr::ggarrange(p1, p3, nrow = 2, ncol = 1, heights = c(1.4, 0.3)))
        
    }
    
}, movie.name = "nuclearExplosions.gif", interval = 1, ani.width = 1200, ani.height = 900))

效果如图: