我想使用rworldmap绘制多个地图,其中每列是一个单独的月,行是年。我知道这可以使用facet_grid在ggmap中完成。我怎么能用rworldmap做到这一点?
例如,mydata
文件包含每个点的latitude
,longitude
,month
和year
的列。到目前为止我的代码:
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)
答案 0 :(得分:3)
要使用rworldmap绘制多个地图,您可以使用布局和几个循环使用下面的代码创建这样的图。
我知道这些天循环并不酷,但我仍然这么认为。可能可以将所有这些放入应用类型函数中,但在绘制时,循环的速度很少成为问题。
(另请参阅常见问题解答中的第19项多面板图 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)