利用ggmap绘制简单地图

mark

简介

ggmap允许用户从Google地图下载并绘制地图。并且ggmap完美契合ggplot2,因此经ggmap绘制的图形可以作为ggplot2的图层,进而在此基础上不断添加图形元素形成十分复杂的图形。ggplot2+ggmap是地理可视化的利器。

安装

安装方法有两种:

# From CRAN
install.packages("ggmap")
# From Github,必须先安装devtools
devtools::install_github("dkahle/ggmap")

绘制

先画个杭州的地图,由于调用的是Google地图,可能需要翻墙,同时速度有点慢(访问的是Google地图数据库,由于网络限制数据抓取可能不完整)。

library(tidyverse)
library(ggmap)
map.hangzhou <- get_map("Hangzhou")
ggmap(map.hangzhou)

mark

这里我们只使用了两个函数,分别是get_map()从Google中检索并下载地图,之后使用函数ggmap()绘制地图。同时ggmap支持管道操作(%>%)。

get_map("Hangzhou")%>%ggmap()

mark

现在我们绘制中国地图

get_map("China")%>%ggmap()

mark

可以看出画出来的图形十分不美观,加上zoom参数之后好看一点。

get_map("China", zoom=4)%>%ggmap()

mark

我们再来看一下天安门

get_map("Tiananmen",zoom=18)%>%ggmap()

mark

应该足够清晰了 再来看看浙江大学的情形,这里默认显示的是玉泉校区

get_map("ZheJiang University Zijingang Campus", zoom = 17)%>%ggmap()

mark

接下来我们将浙江大学在杭州的几个校区标识出来,分别是紫金港、玉泉、华家池、西溪、之江

# 首先创建含有校区名字的数据集
df.zjucampus_locations <- tibble(location=c("Zhejiang university Zijingang Campus,Hangzhou,China","Zhejiang university Yuquan Campus,Hangzhou,China","Zhejiang university Zhijiang Campus,Hangzhou,China","Zhejiang university Huajiachi Campus,Hangzhou,China","Zhejiang university Xixi Campus,Hangzhou,China"))
# 获取经纬度
geo.zjucampus_locations <- geocode(df.zjucampus_locations$location)
# 合并数据集
df.zjucampus_locations <- cbind(df.zjucampus_locations,geo.zjucampus_locations)
# 绘制图形,在地图上将几个校区标识出来
get_map("Hangzhou", zoom = 11)%>%ggmap()+
  geom_point(data=df.zjucampus_locations, aes(x=lon, y=lat), color="red", size=5)+
  geom_text(data = df.zjucampus_locations,aes(lon, lat, label=c("Zhejiang university Zijingang Campus","Zhejiang university Yuquan Campus","Zhejiang university Zhijiang Campus","Zhejiang university Huajiachi Campus","Zhejiang university Xixi Campus")), size=3, color="blue")

mark

由于网络限制就不调整了,这次能出图已经不容易了。可以看到玉泉校区与华家池校区竟然重叠在一起,通过它们的经纬度数据也发现相同,可能Google地图数据库对于国内来说还不是很好用,但是没办法调用百度地图只能很好的获取国内地图数据,一遇到国外的就歇菜了!

SessionInfo

sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Chinese (Simplified)_China.936  LC_CTYPE=Chinese (Simplified)_China.936   
[3] LC_MONETARY=Chinese (Simplified)_China.936 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] ggmap_2.6.1        forcats_0.2.0      stringr_1.2.0      dplyr_0.7.4        purrr_0.2.4       
 [6] readr_1.1.1        tidyr_0.7.2        tibble_1.4.1       ggplot2_2.2.1.9000 tidyverse_1.2.1   

loaded via a namespace (and not attached):
 [1] reshape2_1.4.3    haven_1.1.0       lattice_0.20-35   colorspace_1.3-2  yaml_2.1.16      
 [6] rlang_0.1.6       pillar_1.0.1      foreign_0.8-69    glue_1.2.0        sp_1.2-5         
[11] modelr_0.1.1      readxl_1.0.0      bindrcpp_0.2      jpeg_0.1-8        bindr_0.1        
[16] plyr_1.8.4        munsell_0.4.3     gtable_0.2.0      cellranger_1.1.0  rvest_0.3.2      
[21] RgoogleMaps_1.4.1 mapproj_1.2-5     psych_1.7.8       knitr_1.18        parallel_3.4.3   
[26] proto_1.0.0       broom_0.4.3       Rcpp_0.12.14      geosphere_1.5-7   scales_0.5.0.9000
[31] jsonlite_1.5      mnormt_1.5-5      rjson_0.2.15      hms_0.4.0         png_0.1-7        
[36] stringi_1.1.6     grid_3.4.3        cli_1.0.0         tools_3.4.3       maps_3.2.0       
[41] magrittr_1.5      lazyeval_0.2.1    crayon_1.3.4      pkgconfig_2.0.1   xml2_1.1.1       
[46] lubridate_1.7.1   assertthat_0.2.0  httr_1.3.1        rstudioapi_0.7    R6_2.2.2         
[51] nlme_3.1-131      compiler_3.4.3   
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