使用rworldmap库绘制多个地图 [英] plotting multiple maps using rworldmap library

查看:49
本文介绍了使用rworldmap库绘制多个地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用rworldmap绘制多个地图,其中每列是单独的月份,行是年份.我知道这可以使用facet_grid在ggmap中完成.如何使用rworldmap做到这一点?

I want to plot multiple maps using rworldmap, where each column is a separate month and the rows are years. I know this can be done in ggmap using facet_grid. How can I do this using rworldmap?

例如, mydata 文件包含纬度经度的列每个点的年份.到目前为止,我的代码:

For example, the mydata file contains columns for the latitude, longitude, month and year of each point. My code so far:

library(rworldmap)
newmap <- getMap(resolution = "high")
plot(newmap, xlim = c(110, 155), ylim = c(-35, -20), asp = 1)
p1 <- read.csv("mydata.csv")
points(p1$lon, p1$lat, col = "red", cex = .5)

推荐答案

要使用rworldmap绘制多个地图,您可以使用layout和几个循环使用下面的代码创建类似的绘图.

To plot multiple maps using rworldmap, you could use layout and a couple of loops to create a plot like this using the code below.

我知道这些天循环并不酷,但我还是这么认为.可能将所有这些都放到apply类型函数中,但是在绘制时循环的速度很少成为问题.

I know loops aren't cool these days but I still think that way. Probably possible to put all this into an apply type function, but the speed of the loops is rarely an issue when plotting.

(另请参阅常见问题解答中的第19项多面板图 http://cran.r-project.org/web/包/rworldmap/vignettes/rworldmapFAQ.pdf )

(also see item 19 multi-panel plots in the FAQ http://cran.r-project.org/web/packages/rworldmap/vignettes/rworldmapFAQ.pdf )

library(rworldmap)
newmap <- getMap(resolution = "coarse") #'low' or even 'coarse' resolution map may be sufficient

#example data for 2 years 6 months each
month <- c(1:6,1:6)
year <- c(rep(2012,6),rep(2013,6))
lon <- c(120:131)
lat <- c(-35:-24)
p1 <- data.frame(month=month,year=year,lon=lon,lat=lat)

months <- unique(p1$month)
years <- unique(p1$year)

oldPar <- par(mar=c(2, 0, 0, 2)) #margins top,bottom,left,right

#use layout to create multiple panels including space at top for a title
nPanels <- layout( cbind(c(0,1:6),c(0,7:12))
                   , heights=c(lcm(1),rep(1,6))
                   , respect=F )


for( yrNum in 1:length(years) )
{
  yr <- years[yrNum]
  for( moNum in 1:length(months) )
  {
    mo <- months[moNum]

    cat(yr,mo,"\n")

    plot(newmap, xlim = c(110, 155), ylim = c(-35, -20), asp = 1)
    mtext( paste(yr,"month",mo), cex=0.7) #add titile to subplot

    pMoYr <- p1[ p1$year==yr & p1$month==mo, ]

    points(pMoYr$lon, pMoYr$lat, col = "red", cex = 3)
  }
}

mtext("rworldmap layout demo",outer=TRUE,line=-2)

par(oldPar)

这篇关于使用rworldmap库绘制多个地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆