我想使用以下方法创建栅格图像的值分布图,例如直方图或图形:
library(raster)
library(sp)
library(rgdal)
DEM <- raster("NR.tif")
hist(DEM)
plot(DEM)
Run Code Online (Sandbox Code Playgroud)
plot()用于验证我的数据并显示全绿色图像。可能是第1个频段,共3个。但是我看不到其他频段吗?显然,直方图中的分布不代表图像文件中的插值。在ARCgis中创建的直方图为hist,我相信它代表的是真实值。
关于如何创建像图像这样的真实值的直方图的任何建议。
最好,Mathias
你可以试试
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))
Run Code Online (Sandbox Code Playgroud)
ggplot(melt(DEM),
aes(value, fill=as.factor(Var3))) +
geom_histogram(position="dodge")
Run Code Online (Sandbox Code Playgroud)
或者,关于您的更新
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))
Run Code Online (Sandbox Code Playgroud)