简体   繁体   English

使用rasterVis绘制具有离散颜色的栅格

[英]plot raster with discrete colors using rasterVis

I have a few rasters I would like to plot using gplot in the rasterVis package. 我想在rasterVis包中使用gplot绘制几个栅格。 I just discovered gplot (which is fantastic and so much faster than doing data.frame(rasterToPoints(r))). 我刚刚发现了gplot(这非常棒,比使用data.frame(rasterToPoints(r))快得多)。 However, I can't get a discrete image to show. 但是,我无法显示离散图像。 Normally if r is a raster, I'd do: 通常,如果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))

But is there a way to avoid the call to rasterToPoints? 但有没有办法避免调用rasterToPoints? It is very slow with large rasters. 大型栅格很慢。 I did find I could do: 我发现我能做到:

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

but if you set the fill to cuts it plots the integer representation of the factors. 但如果将填充设置为切割,则绘制因子的整数表示。

Here is some reproducible data: 这是一些可重现的数据:

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)

Thanks 谢谢

gplot is a very simple wrapper around ggplot so don't expect too much from it. gplot是围绕ggplot一个非常简单的包装器,所以不要期望太多。 Instead, you can use part of its code to build your own solution. 相反,您可以使用其部分代码来构建自己的解决方案。 The main point here is to use sampleRegular to reduce the number of points to be displayed. 这里的要点是使用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))

However, if you are open to plot without ggplot2 you may find useful this other answer . 但是,如果您打开没有ggplot2情节,您可能会发现这个其他答案很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM