在 R 中创建栅格直方图

Create histogram of raster in R

我想使用以下方法创建光栅图像的值分布图,如直方图或图形:

library(raster)
library(sp)
library(rgdal)
DEM <- raster("NR.tif")
hist(DEM)
plot(DEM)

plot() 用于验证我的数据并向我显示全绿色图像。据说乐队 1 of 3。 但是我看不到其他乐队? 显然,直方图中的分布并不代表图像文件中的插值。 在 ARCgis 中创建的直方图位于此处hist,我认为它代表了真实值。

关于如何创建像 image 这样的真实值直方图的任何建议。

最好的, 马蒂亚斯

你可以试试

download.file("https://www.dropbox.com/s/t279m5ojners7fl/NR.tif?dl=1", 
              tf <- tempfile(fileext = ".tif"), mode="wb")
library(raster)
library(tiff)
library(ggplot2)
library(reshape2)
DEM <- readTIFF(tf)
plot(as.raster(DEM))

ggplot(melt(DEM), 
       aes(value, fill=as.factor(Var3))) + 
  geom_histogram(position="dodge") 


或者,关于您的更新

r <- as.raster(DEM)
tab <- as.data.frame(sort(table(r)))
ggplot(subset(tab, !r %in% c("#F0F0F0", "#000000")), 
              aes(x=r, y=Freq, fill=I(r))) + 
         geom_bar(stat="identity") + 
  theme(axis.text.x = element_text(angle=90))

尝试将 NA 值设置为背景色,然后调用 hist() 函数或使用 lukeA 的 ggplot 命令:

library(raster)
ras <- stack("Downloads/NR.tif")
NAvalue(ras) <- 240
hist(ras)

这会产生以下图表: