r-使用rasterVis绘制具有离散颜色的光栅



我想在rasterVis包中使用gplot绘制一些光栅。我刚刚发现了gplot(它非常棒,比做data.frame(rasterToPoints(r))快得多)。然而,我无法得到一个离散的图像来显示。通常,如果r是光栅,我会这样做:

rdf=data.frame(rasterToPoints(r))
rdf$cuts=cut(rdf$value,breaks=seq(0,max(rdf$value),length.out=5))
ggplot(rdf)+geom_raster(aes(x,y,fill=cuts))

但是有没有办法避免调用rasterToPoints?使用大型光栅时速度非常慢。我确实发现我可以做:

cuts=cut_interval(r@data@values,n=5)

但如果将填充设置为剪切,则会绘制因子的整数表示。

以下是一些可复制的数据:

x=seq(-107,-106,.1)
y=seq(33,34,.1)
coords=expand.grid(x,y)
rdf=data.frame(coords,depth=runif(nrow(coords),0,2)))
names(rdf)=c('x','y','value')
r=rasterFromXYZ(rdf)

感谢

gplotggplot的一个非常简单的包装器,所以不要期望太多相反,你可以使用它的一部分代码来构建你自己的解决方案这里的要点是使用sampleRegular来减少要显示的点数。

library(raster)
library(ggplot2)
x <- sampleRegular(r, size=5000, asRaster = TRUE)
dat <- as.data.frame(r, xy=TRUE)
dat$cuts <- cut(dat$value,
    breaks=seq(0, max(dat$value), length.out=5))
ggplot(aes(x = x, y = y), data = dat) +
    geom_raster(aes(x, y, fill=cuts))

但是,如果您愿意在没有ggplot2的情况下进行绘图,您可能会发现这很有用这个其他答复

最新更新