2016-05-26 84 views
1

這可能相當簡單,但我是R的新手。我已經嘗試了一段時間,現在可以使用包柵格中的boxplot繪製兩個柵格對彼此的繪圖。R - 用柵格創建boxplot

我有一個DEM光柵和包含4個羣集組,這是我想作爲如手冊中描述的「區」爲使用絕對光柵:

箱線圖(X,Y = NULL,maxpixels = 100000 ,...)

X柵格*對象

y提示如果x是一個RasterLayer對象,y可以是一個附加到RasterLayer組由 '區域'

> DEM 
class  : RasterLayer 
dimensions : 12381, 61922, 766656282 (nrow, ncol, ncell) 
resolution : 0.1, 0.1 (x, y) 
extent  : 478307.4, 484499.6, 6131862, 6133100 (xmin, xmax, ymin, ymax) 
coord. ref. : +proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs 
data source : /Users/Yvonne/Desktop/Boxplot/Ribe_DEM_0.1m.tif 
names  : Ribe_DEM_0.1m 
values  : -7.523334, -0.36 (min, max) 

> Cluster 
class  : RasterLayer 
dimensions : 12381, 61922, 766656282 (nrow, ncol, ncell) 
resolution : 0.1, 0.1 (x, y) 
extent  : 478307.4, 484499.6, 6131862, 6133100 (xmin, xmax, ymin, ymax) 
coord. ref. : +proj=utm +zone=32 +ellps=GRS80 +units=m +no_defs 
data source : /Users/Yvonne/Desktop/Boxplot/final_cluster.tif 
names  : final_cluster 
values  : 1, 4 (min, max) 
attributes : 
ID Rowid COUNT 
    1  0 463524 
    2  1 4118997 
    3  2 3390160 
    4  3 3218998 

> boxplot(DEM, Cluster, xlab="Cluster", ylab="Elevation") 
Error in parse(text = x, keep.source = FALSE) : 
    <text>:2:0: unexpected end of input 
1: ~ 
^
In addition: Warning message: 
In .local(x, ...) : taking a sample of 1e+05 cells 
的 值x的

更新:
我剛剛找到一個工作示例,它正是我想要的。但是,如果我用我自己的數據運行它,我總是會得到上述錯誤。也許有人可以解釋錯誤信息。會非常感激。

r1 <- r2 <- r3 <- raster(ncol=10, nrow=10) 
r1[] <- rnorm(ncell(r1), 100, 40) 
r2[] <- rnorm(ncell(r1), 80, 10) 
r3[] <- rnorm(ncell(r1), 120, 30) 
s <- stack(r1, r2, r3) 
names(s) <- c('A', 'B', 'C') 

rc <- round(r1[[1]]/100) 

hist(rc) 
summary(rc) 

boxplot(s[[1]],rc) 

回答

1

歐凱我找到了答案,我不知道到底爲什麼,但它爲我工作:
我不得不創建一個磚頭,然後我可以使用上面提到的boxplot。

s <- stack(DEM, Cluster) 
sbrick <- brick(s) 
boxplot(sbrick[[1]], sbrick[[2]], xlab="Cluster", ylab="Elevation") 

在這個情節得到的boxplot DEM against cluster groups
感謝大家的幫助!

0

可以在rasterVis庫使用bwplot功能。 下面是rasterVis一個例子:

library(raster) 
library(rasterVis) 

r <- raster(system.file("external/test.grd", package="raster")) 
s <- stack(r, r*2) 
bwplot(s,violin=FALSE,strip=strip.custom(strip.levels=TRUE)) 
+0

謝謝,我試了一下,但它導致了兩個柵格彼此相鄰而不是反對的情節![導致的boxplot](https://dl.dropboxusercontent.com/u/32919877/Rplot.jpeg?raw = 1)。 –

+0

bwplot(dem,cluster)應該可以解決您的問題。您也可以使用分區功能來創建箱線圖。 –

0

爲什麼你得到這個錯誤是我不清楚。也許你可以在下面運行代碼,看看自己:

x <- stack(DEM, Cluster) 
s <- sampleRegular(s, 100000, useGDAL=TRUE) 
cn <- colnames(s) 
f <- as.formula(paste(cn[1], '~', cn[2])) 
boxplot(f, data=s) 
+0

創建「f」對象後,我收到一條錯誤消息。 '解析錯誤(text = x,keep.source = FALSE)'也許因爲cn對象是NULL(空)。但我可能只是找到了我現在正在查看的解決方案。 –