如何绘制和叠加几个栅格的密度图? [英] How can I plot and overlay density plot of several rasters?

查看:59
本文介绍了如何绘制和叠加几个栅格的密度图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个栅格(有效地从数字高程模型计算得出的栅格)(使用栅格数据包创建),我想比较这些值的分布.一种可能的方法是绘制密度图并将其覆盖在聊天中.

I have several rasters (effectively resulted calculated from digital elevation models) (created using the raster package) and I would like to compare the distribution of the values. One possible way is to plot a density plot and overlay them on a chat.

我意识到先堆叠然后再进行晶格封装,但是栅格具有不同的分辨率(目的是测试不同分辨率对结果计算的影响.

I realise that making a stack and then the lattice package, but the rasters have different resolutions (the purpose is to test the effect of different resolutions of the resulting calculation.

我最近对ggplot2软件包有些适应了,但是据我所知,它不适用于栅格数据类型.

I have recently become a little comfortable with the ggplot2 package, but it doesn't as I understand it, work with raster data types.

请问有人可以建议一种包装或技术来绘制多个密度图(甚至其他类型,例如箱形和晶须图)以比较不同栅格的特征吗?

Can anyone please advise a package or technique to plot multiple density plots (and even other types such as box and whisker plots) for comparing the characteristics of different rasters please?

推荐答案

我不确定您要干什么,但是无论如何我还是要开枪.

I'm not quite sure what you are after, but I'm going to take a shot anyway.

假设我们具有以下不同分辨率的栅格列表,并且我们有兴趣使用ggplot2软件包绘制栅格内值的分布.

Suppose we have the following list of rasters of different resolutions and we're interested in plotting the distributions of the values inside the raster with the ggplot2 package.

library(raster)
#> Loading required package: sp
library(ggplot2)

rasterlist <- list(
  raster1 = raster(matrix(runif(100), 10)),
  raster2 = raster(matrix(rnorm(25), 5)),
  raster3 = raster(matrix(rpois(64, 2), 8))
)

我们要做的是将栅格数据转换成ggplot2可以理解的格式,即长格式(与宽数据相反).这意味着,在栅格情况下,每个像元的每个观测值都应位于data.frame中它们自己的行上.我们通过使用 as.vector()变换每个栅格并指明原点栅格来完成此操作.

What we would have to do is get this raster data into a format that ggplot2 understand, which is the long format (as opposed to wide data). This means that every observation, in the raster case every cell, should be on their own row in a data.frame. We do this by transforming each raster with as.vector() and indicate the raster of origin.

df <- lapply(names(rasterlist), function(i) {
  data.frame(
    rastername = i,
    value = as.vector(rasterlist[[i]])
  )
})
df <- do.call(rbind, df)

现在数据格式正确,您可以将其提供给ggplot.对于密度图,x位置应为值.设置 fill =栅格名称将自动确定分组.

Now that the data is in the correct format, you can feed it to ggplot. For density plots, the x positions should be the values. Setting the fill = rastername will automatically determine grouping.

ggplot(df, aes(x = value, fill = rastername)) +
  geom_density(alpha = 0.3)

对于箱式或小提琴绘图,该组通常在x轴上,并且值映射到y轴上.

For box- or violin-plots, the group is typically on the x-axis and the values are mapped to the y-axis.

ggplot(df, aes(x = rastername, y = value)) +
  geom_boxplot()

ggplot(df, aes(x = rastername, y = value)) +
  geom_violin()

reprex软件包(v0.3.0)创建于2020-10-04 sup>

Created on 2020-10-04 by the reprex package (v0.3.0)

希望这大致就是您想要的.

Hope that this is roughly what you were looking for.

这篇关于如何绘制和叠加几个栅格的密度图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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