简体   繁体   English

使用 R 创建分类栅格值的直方图? (或者,使用经纬度值创建 data.table)

[英]Use R to create a historgram of categorical raster values? (or, create data table with lat/long values)

I am very new to R and programming in general, please forgive my forthcoming ineptitude.我是 R 和一般编程的新手,请原谅我即将到来的无能。

I am working with a large categorical raster.我正在处理一个大型分类栅格。 Essentially, every pixel shallower than 10 meters on the Great Barrier Reef is assigned a value: 11,12,13, or 15. (Original file here ).本质上,大堡礁上每一个小于 10 米的像素都被分配了一个值:11、12、13 或 15。(原始文件在这里)。 My end goal is to create a histogram showing the frequency of the "rubble" category (which is the given by the value 12) by latitude.我的最终目标是创建一个直方图,按纬度显示“碎石”类别(由值 12 给出)的频率。 It would look very similar to the third panel of this figure , but where they show "coral habitat" I would be showing rubble.它看起来与该图的第三个面板非常相似,但在它们显示“珊瑚栖息地”的地方,我会显示瓦砾。

I thought the best way to do this would be to try to convert the original raster into a data frame where each row represents what was a pixel and there are three columns: categorical value (11,12,13, or 15), latitude, and longitude.我认为最好的方法是尝试将原始栅格转换为数据框,其中每一行代表一个像素,并且有三列:分类值(11、12、13 或 15)、纬度、和经度。 I could then use this data frame to create any number of basic plots.然后我可以使用这个数据框来创建任意数量的基本图。

Ideally I would like to omit NAs in the process of creating this data frame because the raster is 152,505 by 112,421, but over 99% of pixels are empty (due to the shape of the Great Barrier Reef).理想情况下,我想在创建此数据框的过程中省略 NA,因为栅格为 152,505 x 112,421,但超过 99% 的像素是空的(由于大堡礁的形状)。

I can easily read in the raster and plot it using the Raster or Terra packages:我可以使用 Raster 或 Terra 包轻松读取栅格和 plot:

benthic <- ("data/GBR10 GBRMP Benthic.tif")
benthic_r <-rast(benthic)
plot(benthic_r)

I tried using this to make it smaller so that future computations will be easier.我尝试使用它来缩小它,以便将来的计算更容易。

benthic2 <- na.omit(benthic)
benthic2_r <- rast(benthic2)

But found na.omit is for vector data only, so that was not successful.但是发现 na.omit 只针对矢量数据,所以没有成功。

I am trying to use Geocomputation with R and thought perhaps I would use the as.data.frame function somehow, but I have not been able to find a way to use that to create the sort of table I want.我正在尝试将Geocomputation 与 R一起使用,并想也许我会以某种方式使用 as.data.frame function,但我一直无法找到一种方法来使用它来创建我想要的那种表。

I have also been browsing information about the Stars and RasterVis packages.我也一直在浏览有关 Stars 和 RasterVis 包的信息。 I tried to skip over the data frame idea and create a histogram directly from the orginal raster using the rasterVis package:我试图跳过数据框的想法并使用 rasterVis package 直接从原始栅格创建直方图:

dirY <- xyLayer(benthic_r, y) #trying to assign y axis to be the y values of the pixels (which are hopefully latitude...)
dirXY <- xyLayer(count(12), benthic_r) #trying to assign x values to be a count of the number of pixels with a value of 12

histogram(benthic_r, dirXY,
maxpixels = 1e+05,
strip=TRUE,
par.settings=rasterTheme(),
att = 1)

#attempting to follow https://cran.r-project.org/web/packages/rasterVis/rasterVis.pdf "histogram methods" section

I have tweaked this in many ways but everything just results in a new error.我已经以多种方式对此进行了调整,但一切都会导致新的错误。

I am not expecting a full code solution, rather if you could help me understand where to look and how to progressively build up to creating that graph that would be much appreciated.我不期待一个完整的代码解决方案,而是如果你能帮助我理解在哪里看以及如何逐步构建以创建将非常感激的图表。 Is attempting to create a data frame first a good approach?首先尝试创建数据框是一种好方法吗? Or is that totally unnecessary?或者这完全没有必要?

The resources I find on how to work with Raster data seem to all be about manipulating satellite imagery (continuous rasters), I have not found many techniques for manipulating categorical rasters.我找到的关于如何处理栅格数据的资源似乎都是关于处理卫星图像(连续栅格)的,我没有找到很多处理分类栅格的技术。 If you know of good tutorials for working with categorical rasters that would be much appreciated too.如果您知道使用分类栅格的好教程,那也将不胜感激。

Thanks in advance for any advice.在此先感谢您的任何建议。

I think the easiest way is to aggregate the raster so that you get one column, with the value of interest.我认为最简单的方法是聚合栅格,以便获得一列,其中包含感兴趣的值。 That is, one cell for each latitude (row).也就是说,每个纬度(行)对应一个像元。 Here is an example:这是一个例子:

# example data 
library(terra)
r <- rast(nrow=18)
values(r) <- sample(c(11,12,13,15), ncell(r), replace=TRUE)

# count the number of cells that are 12
r12 <- r == 12    

# aggregate the columns
a <- aggregate(r12, c(1, ncol(r)), sum)

# get the latitude for each row
lat <- yFromRow(a, 1:nrow(a))

You can make different types of plots.您可以制作不同类型的图。 Here are two examples.这里有两个例子。 I prefer latitude on the vertical axis.我更喜欢纵轴上的纬度。

plot(values(a), lat, ylab="latitude", xlab="count")

barplot(rev(values(a)[,1]), horiz=T, names.arg=rev(lat), las=1)

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

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