0

我正在尝试将世界地图放在我当前由关卡绘制的地块上。这个情节是这样制作的:

library(raster)
library(ncdf4)
library(maps)
library(maptools)
library(rasterVis)
library(ggplot2)
library(rgdal)
library(sp)
library(gridExtra)

MFplot4<-levelplot(MFMeaner3,margin=F, at=Fcutpoints4,cuts=11, 
pretty=TRUE,par.settings=mapTheme, main="Historical five-day maximum   
precipitation (mm/day) model mean")

对象“MFMeener3”具有以下属性:

class       : RasterLayer 
dimensions  : 64, 128, 8192  (nrow, ncol, ncell)
resolution  : 2.8125, 2.789327  (x, y)
extent      : -181.4062, 178.5938, -89.25846, 89.25846  (xmin, xmax, ymin, 
ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : layer 
values      : 0.1583802, 164.2064  (min, max)

这是我在上面的图上放置世界地图叠加层的尝试:

world.outlines<-map("world", plot=FALSE) 
world.outlines.sp<-map2SpatialLines(world.outlines,proj4string =   
CRS("+proj=longlat"))
MFplot4 + layer(sp.lines(world.outlines.sp,col="black",lwd=0.5))

但是,这会导致以下错误:

Error: Attempted to create layer with no stat.

我还尝试使用以下方法放置一个简单的世界地图:

MFplot4 + plot(wrld_simpl)

但我收到此错误:

Error in UseMethod("as.layer") : 
no applicable method for 'as.layer' applied to an object of class "NULL"

为什么会出现这些错误?

对此的任何帮助将不胜感激!

4

1 回答 1

3

问题是,通过加载ggplot2 已经用. 如果您必须加载ggplot2,那么您需要完全限定您对屏蔽函数的调用,代替.latticeExtra::layer()ggplot2::layer()latticeExtra::layer()layer()

这是一个可重现的示例,在未加载ggplot时对我有用,但在加载时失败:

library(rasterVis)
library(sp)
library(maps)
library(maptools)
## library(ggplot2)  ## `levelplot() + layer()` fails when this is loaded

## Read in a RasterLayer
tmax <- getData('worldclim', var='tmax', res=10)[[6]]

## Create a SpatialLines object
countries <- map("world", plot=FALSE) 
countries <- map2SpatialLines(countries, proj4string = CRS("+proj=longlat"))

## Overlay lines on levelplot
levelplot(tmax, margin=FALSE) + 
    layer(sp.lines(countries))

在此处输入图像描述

于 2019-05-21T18:38:36.713 回答