2016-12-20 101 views
0

我有以下示例代碼:光柵與灰度繪圖:0和1是在繪製白色

library(raster) 
library(SpaDES) 

m = matrix(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0.1, 0.2, 0.3, 0.4, 0.4, 0.3, 0.2, 0.1, 0, 
     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
     0.4, 0.3, 0.2, 0.1, 0, 0.1, 0.2, 0.3, 0.4, 0.5),nrow=4, 
     ncol=10, byrow=TRUE) 
r <- raster(m) 
Plot(r, cols = grey.colors(10, start=0, end=1), title = "4x10 Raster") 

這應該產生一個4×10的柵格,其中每個0表示黑色正方形,1白色正方形和其他數字灰色方塊。不幸的是,一個和零(以及背景)都是白色的。我怎樣才能讓零顯示爲黑色方塊?

Raster with white instead of black

編輯:我使用R包「黑桃」生態應用,Plot是從包裝的功能。柵格正方形繪製爲正方形非常重要,無論繪圖器的形狀或柵格的大小如何,因爲它代表了一張光柵圖。

謝謝你的幫助。

+0

我認爲這是與zero.color = NULL:「zero.color \t 指示零個值的顏色字符串,當零是最小值時,否則,零被視爲任何其他顏色。默認透明。「 – Apatura

+0

出於某種原因,以下工作:'Plot(r,cols = grey.colors(10,start = 0,end = 1),title =「4x10 Raster」,na.color =「black」)' I don不明白爲什麼,因爲'na.color'應該是「指示NA值顏色的字符串,默認透明」。另外,我不希望將其設置爲「黑色」,而只是將其作爲普通數字包含在grey.colors漸變中。 – Apatura

回答

0

因此,雖然依然採用了SpaDES包,而不是plotPlot功能來解決我的問題的方式似乎是以下幾點:

library(raster) 
library(SpaDES) 

m = matrix(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
     0, 0.1, 0.2, 0.3, 0.4, 0.4, 0.3, 0.2, 0.1, 0, 
     0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
     0.4, 0.3, 0.2, 0.1, 0, 0.1, 0.2, 0.3, 0.4, 0.5),nrow=4, 
     ncol=10, byrow=TRUE) 
r <- raster(m) 

Plot(r, cols = grey.colors(10, start=0, end=1), title = "4x10 Raster", na.color = "black") 

raster plot with zeros plotted in black

我仍然不明白爲什麼要更改的選項不起作用。下面是從Plot函數的定義從SpaDES

na.color Character string indicating the color for NA values. Default transparent. 
zero.color Character string indicating the color for zero values, when zero is the minimum value, otherwise, zero is treated as any other color. Default transparent. 
2

我這樣做:

plot(r, col = grey.colors(10, start=0, end=1), main = "4x10 Raster") 

,並得到這個:

enter image description here

改變Plotplotcolscol,並titlemain

+0

這解決了黑/白問題,但並不適合我。在我的情況下,我有一個柵格地圖,方格確實需要保持正方形。原來,「Plot」是一個{SpaDES}函數,我想用它,因爲它與我的程序的其餘部分一致。我將相應地編輯我的帖子。謝謝,雖然, – Apatura

+0

當然@patura,我希望我可以回答這個問題,你可能想問另一個問題,而不是編輯這個問題,因爲這個答案可能會幫助其他人。 –

+1

這個答案幫了我很多(謝謝!!!),因爲它告訴我這個問題在'Plot'函數中。我甚至沒有注意到這是一個專門包裝的功能。但我不確定它如何幫助別人,因爲使用'plot'函數我的問題不會發生。 – Apatura

0

從@Derek Corcoran的更正。我添加了一些代碼來保留柵格的高寬比。

library(raster) 
library(SpaDES) 
m = matrix(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
      0, 0.1, 0.2, 0.3, 0.4, 0.4, 0.3, 0.2, 0.1, 0, 
      0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 
      0.4, 0.3, 0.2, 0.1, 0, 0.1, 0.2, 0.3, 0.4, 0.5),nrow=4, 
      ncol=10, byrow=TRUE) 
r <- raster(m) 
#the aspect ratio should be modified to "0.4". 
plot(r,asp=0.4,col = grey.colors(10, start=0, end=1)) 
#Because it is the relation beetween columns and rows. 

enter image description here