tidyverse实践之绘制简单地图

简介

tidyverse是一个数据科学的综合包,里面包含了数据分析一系列流程涉及的包,今天主要利用它绘制地图。这并不是它的强项,绘制地图leaflet才是利器,本文只是tidyverse的一个小练习。 目前该包支持有限的几个国家,比如美国,新西兰、意大利等,当然世界地图是支持的。

绘图

library(tidyverse)
map_data("usa")%>%
  ggplot(aes(x=long, y=lat, group=group))+
  geom_polygon()

可以绘制美国的不同州,就行我国的省份一样

map_data("state")%>%
  ggplot(aes(x=long, y=lat, group=group))+
  geom_polygon(color="white")

另外我们可以只单独绘制几个state

map_data("state")%>%
  filter(region%in%c("california","nevada","oregon","washington"))%>%
  ggplot(aes(x=long, y=lat, group=group))+
  geom_polygon(color="red")

再绘制细一点的地图,相当于市、县等

map_data("county")%>%
  filter(region %in% c("california","nevada","oregon","washington"))%>%
  ggplot(aes(x=long, y=lat, group=group))+
  geom_polygon(color="white")

世界地图

很简单,只需将map_data()里的usa换成world就行

map_data("world")%>%ggplot(aes(x=long, y=lat, group=group))+
  geom_polygon(color="white")

在世界地图中标出中国

map_data("world")%>%
  filter(region=="China")%>%
  ggplot(aes(x=long, y=lat, group=group))+
  geom_polygon(fill="red")

美帝用心险恶,不承认台湾属于中国,自己添上去就是了

map_data("world")%>%
  filter(region==c("China","Taiwan"))%>%
  ggplot(aes(x=long, y=lat, group=group))+
  geom_polygon(fill="red")

SessionInfo

sessionInfo()
## R version 3.4.1 (2017-06-30)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 15063)
## 
## 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] bindrcpp_0.2         maps_3.2.0           dplyr_0.7.2         
##  [4] purrr_0.2.3          readr_1.1.1          tidyr_0.7.1         
##  [7] tibble_1.3.4         ggplot2_2.2.1        tidyverse_1.1.1     
## [10] RevoUtilsMath_10.0.0
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.12     cellranger_1.1.0 compiler_3.4.1   plyr_1.8.4      
##  [5] bindr_0.1        forcats_0.2.0    tools_3.4.1      digest_0.6.12   
##  [9] lubridate_1.6.0  jsonlite_1.5     evaluate_0.10.1  nlme_3.1-131    
## [13] gtable_0.2.0     lattice_0.20-35  pkgconfig_2.0.1  rlang_0.1.2     
## [17] psych_1.7.8      yaml_2.1.14      parallel_3.4.1   haven_1.1.0     
## [21] xml2_1.1.1       httr_1.3.1       stringr_1.2.0    knitr_1.17      
## [25] hms_0.3          RevoUtils_10.0.5 rprojroot_1.2    grid_3.4.1      
## [29] glue_1.1.1       R6_2.2.2         readxl_1.0.0     foreign_0.8-69  
## [33] rmarkdown_1.6    modelr_0.1.1     reshape2_1.4.2   magrittr_1.5    
## [37] backports_1.1.0  scales_0.5.0     htmltools_0.3.6  rvest_0.3.2     
## [41] assertthat_0.2.0 mnormt_1.5-5     colorspace_1.3-2 labeling_0.3    
## [45] stringi_1.1.5    lazyeval_0.2.0   munsell_0.4.3    broom_0.4.2
Researcher

I am a PhD student of Crop Genetics and Breeding at the Zhejiang University Crop Science Lab. My research interests covers a range of issues:Population Genetics Evolution and Ecotype Divergence Analysis of Oilseed Rape, Genome-wide Association Study (GWAS) of Agronomic Traits.

comments powered by Disqus