Black007的个人博客分享 http://blog.sciencenet.cn/u/Black007

博文

[转载]用 googleVis 包画地图

已有 3417 次阅读 2019-9-8 15:55 |个人分类:数据分析|系统分类:科研笔记|文章来源:转载

因为赖江山老师翻译的《数量生态学——R语言的应用》真的是看不懂,最近开始看起了英文版的 Numerical Ecology with R,这一本是2018年再版的,比上一版确实实用了太多。所以打算将一些重要的笔记记录在此。

大家可以自己去下载他的数据,或者发邮件(2674102796@qq.com)找我要。

加载包,并读取数据。

library(RgoogleMaps)
library(googleVis)
load("D:/R/statistics/NumericalEcologywR-2ed_code_data/NEwR-2ed_code_data/NEwR2-Data/Doubs.RData")

这个时候就可以直接用R包GoogleVis来处理数据。

nom <- latlong$Site
latlong2 <- paste(latlong$LatitudeN, latlong$LongitudeE, sep = ":")
df <- data.frame(latlong2, nom, stringsAsFactors = FALSE)

mymap1 <- gvisMap(df, 
  locationvar = "latlong2", 
  tipvar = "nom", 
  options = list(showTip = TRUE)
)

plot(mymap1)

gvisMap函数画出来的地图
gvisMap函数画出来的图虽然好看,但是不仅有打码,还不能保存,这在写论文的时候就非常不适用了。所以,在书的源代码中还有另外一种画图的方法。

# This method provides a static image that is viewed in R.
# Map background is fetched from the Internet. The map limits
# are defined by the latitude and longitude of the site
# coordinates.
# Several types of maps are available. Here we use "terrain".
MapBackground(lat = latlong$LatitudeN, 
  lon = latlong$LongitudeE, 
  destfile =  "bckg", 
  maptype = "terrain"
)

# Map is loaded into an R object
doubs.map <- ReadMapTile(destfile = "bckg")

# Site latitude-longitude coordinates are added
PlotOnStaticMap(doubs.map, 
  lat = latlong$LatitudeN, 
  lon = latlong$LongitudeE, 
  cex = 0.7, 
  col = "black", 
  pch = 19
)

# Plot of labels
TextOnStaticMap(doubs.map, 
  latlong$LatitudeN - 0.0008 * abs(latlong$LatitudeN),
  latlong$LongitudeE, 
  labels = latlong$Site, 
  add = TRUE, 
  cex = 0.5
)

  # The size of the points and labels is adjusted by the argument
  # cex. The location of the labels has been offset from the
  # points by subtraction of small fractions of the latitude.


这种画地图的方法,则更方便保存。也是更加实用的一种画地图方法。怎么样是不是很简单。



https://blog.sciencenet.cn/blog-3412381-1197146.html

上一篇:谈下鬼伞的自溶
下一篇:[转载]聚类分析树之间的对比
收藏 IP: 210.72.88.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-17 05:43

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部