利用gganimate可视化R-Ladies发展情况
简介这篇博客主要基于d4tagirl(https://d4tagirl.com/2017/05/how-to-plot-animated-maps-with-gganimate),稍微有所修改。
数据加载library(pacman)p_load(tidyverse, gganomate, maps, ggthemes)rladies <- read_csv(url(“https://raw.githubusercontent.com/d4tagirl/R-Ladies-growth-maps/master/rladies.csv”))%>%select(-1)head(rladies)
## # A tibble: 6 x 7## screen_name location created_at followers age_days lon lat##
可视化主要是根据地理位置信息映射到地图上fig.height=18}ggplot()+borders(“world”, color=”gray85″, fill=”grey80″)+geom_point(data = rladies, aes(lon, lat, size=followers), color=”purple”, alpha=0.5)+scale_size_continuous(range = c(8, 24), breaks = c(250, 500, 750, 1000))+labs(size=”Followers”, title=” The development of R-Ladies’ Twitter accounts”,x=NULL,y=NULL)+theme(text = element_text(family = “Times New Roman”, color = “#EEEEEE”),plot.title = element_text(size=40,color = “#f9ba00″),plot.subtitle = element_text(size=14),axis.ticks = element_blank(),axis.text = element_blank(),panel.grid = element_blank(),panel.background = element_rect(fill=”#333333”),plot.background = element_rect(fill = “#333333”),legend.position = c(0.18,0.36),legend.background = element_blank(),legend.key = element_blank(),legend.text = element_text(size = 28),legend.title = element_text(size=28, color = “#f9ba00”))+annotate(geom = “text”,label=”Made by Logos ytlogos.github.io\nOriginally from d4tagirl https://d4tagirl.com”,x=70, y=-55, size=10, family=”Helvetica Black”, color=”#f9ba00″, hjust=”left”)
动画展示为了利用gganimate进行动态展示,需要构建一个映射变量:时间。同时为了使得可视化开始和结尾呈现的是空白,又构建了两个空白图层 ghost_points_ini <- tibble(created_at=as.Date(“2011-09-01”), followers=0, lon=0, lat=0)ghost_points_fin <- tibble(created_at=seq(as.Date(“2017-05-16”), as.Date(“2017-05-30″),by=”days”), followers=0, lon=0,lat=0)
添加frame映射map <- ggplot()+borders(“world”, color=”gray85″, fill=”grey80″)+geom_point(data = rladies, aes(lon, lat, size=followers, frame=created_at, cumulative=TRUE), color=”purple”, alpha=0.5)+scale_size_continuous(range = c(4, 16), breaks = c(250, 500, 750, 1000))+geom_point(data = ghost_points_ini, aes(lon, lat, size=followers, frame=created_at, cumulative=TRUE), alpha=0)+geom_point(data = ghost_points_fin, aes(lon, lat, size=followers, frame=created_at, cumulative=TRUE), alpha=0)+labs(size=”Followers”, title=”The development of R-Ladies’ Twitter accounts”,x=NULL,y=NULL)+theme(text = element_text(family = “Times New Roman”, color = “#EEEEEE”),plot.title = element_text(size=28, color = “#f9ba00″),plot.subtitle = element_text(size=14),axis.ticks = element_blank(),axis.text = element_blank(),panel.grid = element_blank(),panel.background = element_rect(fill=”#333333”),plot.background = element_rect(fill = “#333333”),legend.position = c(0.18,0.36),legend.background = element_blank(),legend.key = element_blank(),legend.text = element_text(size = 18),legend.title = element_text(size=24, color = “#f9ba00”))+annotate(geom = “text”,label=”Made by Logos ytlogos.github.io\nOriginally from d4tagirl https://d4tagirl.com”,x=70, y=-55, size=6, family=”Helvetica Black”, color=”#f9ba00″, hjust=”left”)animation::ani.options(interval=0.15, ani.width=1500, ani.height=800, units=”in”)gganimate::gganimate(map, filename = “d4tagirlmap.gif”) SessionInfosessionInfo()## R version 3.4.3 (2017-11-30)## Platform: x86_64-w64-mingw32/x64 (64-bit)## Running under: Windows 10 x64 (build 16299)#### Matrix products: default#### locale:## [1] LC_COLLATE=Chinese (Simplified)_China.936## [2] LC_CTYPE=Chinese (Simplified)_China.936## [3] LC_MONETARY=Chinese (Simplified)_China.936## [4] LC_NUMERIC=C## [5] LC_TIME=Chinese (Simplified)_China.936#### attached base packages:## [1] stats graphics grDevices utils datasets methods base#### other attached packages:## [1] ggthemes_3.4.0 maps_3.2.0 BiocInstaller_1.28.0## [4] forcats_0.2.0 stringr_1.2.0 dplyr_0.7.4## [7] purrr_0.2.4 readr_1.1.1 tidyr_0.8.0## [10] tibble_1.4.2 ggplot2_2.2.1.9000 tidyverse_1.2.1## [13] pacman_0.4.6#### loaded via a namespace (and not attached):## [1] reshape2_1.4.3 haven_1.1.1 lattice_0.20-35## [4] colorspace_1.3-2 htmltools_0.3.6 yaml_2.1.16## [7] utf8_1.1.3 rlang_0.1.6 pillar_1.1.0## [10] foreign_0.8-69 glue_1.2.0 modelr_0.1.1## [13] readxl_1.0.0 bindrcpp_0.2 bindr_0.1## [16] plyr_1.8.4 munsell_0.4.3 gtable_0.2.0## [19] cellranger_1.1.0 rvest_0.3.2 psych_1.7.8## [22] evaluate_0.10.1 labeling_0.3 knitr_1.19## [25] parallel_3.4.3 broom_0.4.3 Rcpp_0.12.15## [28] scales_0.5.0.9000 backports_1.1.2 jsonlite_1.5## [31] mnormt_1.5-5 hms_0.4.1 digest_0.6.15## [34] stringi_1.1.6 grid_3.4.3 rprojroot_1.3-2## [37] cli_1.0.0 tools_3.4.3 magrittr_1.5## [40] lazyeval_0.2.1 crayon_1.3.4 pkgconfig_2.0.1## [43] xml2_1.2.0 lubridate_1.7.1 assertthat_0.2.0## [46] rmarkdown_1.8 httr_1.3.1 rstudioapi_0.7## [49] R6_2.2.2 nlme_3.1-131 compiler_3.4.3
欢迎加入本站公开兴趣群商业智能与数据分析群兴趣范围包括各种让数据产生价值的办法,实际应用案例分享与讨论,分析工具,ETL工具,数据仓库,数据挖掘工具,报表系统等全方位知识QQ群:81035754