使用rasterVis绘制离散颜色的栅格 [英] plot raster with discrete colors using rasterVis
问题描述
我想用rasterVis软件包中的gplot绘制一些栅格。我刚刚发现gplot(这太棒了,比data.frame(rasterToPoints(r)))快得多。但是,我无法得到一个独立的图像显示。通常如果r是一个栅格,我会这样做:
rdf = data.frame(rasterToPoints(r))
dfdf $ cut = cut(rdf $ value,breaks = seq(0,max(rdf $ value),length.out = 5))
ggplot(rdf)+ geom_raster(aes(x,y,fill = )
但是有没有办法避免调用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) 2)))
names(rdf)= c('x','y','value')
r = rasterFromXYZ(rdf)
$ b $ p感谢是 ggplot
的一个非常简单的包装,所以不要期望它太多。相反,您可以使用其部分代码来构建您自己的
解决方案。这里的要点是使用
sampleRegular
来减少要显示的点数。
<$ p
$ bx< - sampleRegular(r,size = 5000,asRaster = TRUE)
dat< code> library(raster)
library(ggplot2)
x ; - 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
,您可能会发现有用的
this other
答案。
I have a few rasters I would like to plot using gplot in the rasterVis package. I just discovered gplot (which is fantastic and so much faster than doing data.frame(rasterToPoints(r))). However, I can't get a discrete image to show. Normally if r is a raster, I'd do:
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? 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. 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.
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.
这篇关于使用rasterVis绘制离散颜色的栅格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!