使用R中的ggplot2绘制分箱类中的连续栅格数据 [英] Plot continuous raster data in binned classes with ggplot2 in R

查看:478
本文介绍了使用R中的ggplot2绘制分箱类中的连续栅格数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常喜欢 ggplot2 的外观和感觉,并经常使用它们来显示栅格数据(例如,时间降雨场的时间步长切面非常有用)。



然而,我仍然想知道是否可以轻松地将连续的栅格值分隔为多个分隔区并为每个分区分配一种颜色 ,在图例中显示(与许多GIS系统一样)。



我试着用 guide =legend中断 scale_fill_gradient 选项的参数。然而,这些只影响图形侧的图例,但绘制的值仍然是连续的。

  library(ggplot2)$ (seq(1:10),times = 10),y = rep(seq(1:10),each = 10),value = runif(100, - 10,10))
ggplot(data = data,aes(x = x,y = y))+
geom_raster(aes(fill = value))+
coord_equal()+
scale_fill_gradient2(low =darkred,mid =white,high =midnightblue,
guide =legend,breaks = c(-8,-4,0,4,8))

我的问题主要是如何离散绘制在 ggplot ,这样图表的读者可以对颜色表示的值作出定量的结论。其次,我怎样才能仍然使用发散调色板(类似于 scale_fill_gradient2 ),即以0或其他特定值为中心? / div>

你应该使用栅格包可与栅格数据一起使用。这个
包提供了几个函数来处理分类
栅格。例如,使用重新分类,您可以将连续的
文件转换为离散的光栅。下一个示例是根据
这个问题进行调整的:

  library(raster)

f< - system.file(external / test.grd,package = (r,c(0,500,1,
500,2000,2))
< - 光栅(f)
r < - 重分类/ code>

另一方面,如果您想使用 ggplot2 函数,
rasterVis 包在 ggplot 附近提供了一个简单的包装,即
与<$ c一起工作

$ p $ library $($)

$ p






$ b $ (r)+
geom_raster(aes(fill = factor(value)))+
coord_equal()



定义你自己可以添加的颜色,然后:

  scale_fill_manual(values = c('red','绿色')))


I quite like the look and feel of ggplot2 and use them often to display raster data (e.g facetting over timesteps for time-varying precipitation fields is very useful).

However, I'm still wondering whether it is easily possible to bin the continuous raster values into discrete bins and assign to each bin a single colour, that is shown in the legend (as many GIS systems do).

I tried with the guide = "legend", and breaks arguments of the scale_fill_gradient option. However these affect just the legend on the side of the graph, but the plotted values are still continuous.

library(ggplot2)
data <- data.frame(x=rep(seq(1:10),times = 10), y=rep(seq(1:10),each = 10), value = runif(100,-10,10))
ggplot(data = data, aes(x=x,y=y)) +
  geom_raster(aes(fill = value)) +
  coord_equal() +
  scale_fill_gradient2(low = "darkred", mid = "white", high = "midnightblue",
                       guide = "legend", breaks = c(-8,-4,0,4,8))

My question is mainly how to discretize the data that is plotted in ggplot, so that the reader of the graph can make quantitative conclusions on the values represented by the colors.

Secondly, how can I still use a diverging color palette (similar to scale_fill_gradient2), that is centered around zero or another specific value?

解决方案

You should use the raster package to work with raster data. This package provides several function to work with categorical rasters. For example, with reclassify you can convert a continuous file into a discrete raster. The next example is adapted from this question:

library(raster)

f <- system.file("external/test.grd", package="raster")
r <- raster(f)
r <- reclassify(r, c(0, 500, 1,
                     500, 2000, 2))

On the other hand, if you want to use the ggplot2 functions, the rasterVis package provides a simple wrapper around ggplot that works with RasterLayer objects:

library(rasterVis)

gplot(r) +
    geom_raster(aes(fill = factor(value))) +
    coord_equal()

to define your own colors you can add then:

scale_fill_manual(values=c('red','green')))

这篇关于使用R中的ggplot2绘制分箱类中的连续栅格数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆