2016-06-10 100 views
3

我試圖根據我有的數據創建透視圖,以經度/緯度格式。persp():我的顏色有什麼問題?

enter image description here

緯度和經度正確繪製,而背景很奇怪的黑色。我該如何改變這一點。

persp(lga_crop_05, expand =0.5, phi = 35, col= "lightblue", ticktype = "detailed") 

這就是我創建劇情的代碼。 lga_crop_05是我的柵格圖層,它包含我上面附加的我的圖的緯度/經度/值。

我將不勝感激任何幫助。 謝謝

+2

歡迎StackOverflow上。請看看這些關於如何產生[最小,完整和可驗證的例子](http://stackoverflow.com/help/mcve)的技巧,以及這篇文章[在R中創建一個很好的例子](http ://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。 – lmo

回答

4

出了什麼問題?

代碼沒有問題;但用我們的眼睛。試試以下代碼:

## a function to produce a perspective plot 
## "n" controls how refined your grid is 
foo <- function(n) { 
    x <- seq(-1.95, 1.95, length = n) 
    y <- seq(-1.95, 1.95, length = n) 
    z <- outer(x, y, function(a, b) a*b^2) 
    persp(x, y, z, col = "lightblue") 
    } 

如果我們有一個10 * 10的網格,顏色看起來很完美。

foo(10) 

nice

如果我們有一個100 * 100格,顏色看起來還是OK。

foo(100) 

ok

現在,如果我們有非常精緻的數據,例如,在1000 * 1000的網格。一切都會看起來像黑色。

foo(1000) 

oh no!

請注意,你有一個光柵。我會懷疑你的數據太簡單了。你有沒有檢查過你的lga_crop_05有多少個電池?

如何解決?

Set border = NA。嘗試修改功能:

foo1 <- function(n) { 
    x <- seq(-1.95, 1.95, length = n) 
    y <- seq(-1.95, 1.95, length = n) 
    z <- outer(x, y, function(a, b) a*b^2) 
    persp(x, y, z, col = "lightblue", border = NA) 
    } 

Great

+0

有沒有辦法減少透視圖上繪製的網格線的數量?你可以通過減少數據的分辨率來做到這一點,但是最好能夠使用所有的數據,而不是每一個點都要繪製網格線,比如說每10點繪製網格線。如果這是不可能的,則另一種選擇是減小網格線的線寬(例如,將'lwd = 0.2'作爲參數添加到'persp')。 – eipi10

+0

這會完全移除網格線,使其很難看到曲面的拓撲結構。有沒有辦法保留所有的數據,但繪製更少的網格? – eipi10

+0

非常感謝@ZheyuanLi – Alex

3

我用Mr.Li的示例數據,謝謝。我認爲這是你想要的。

# I changed x and y length irregular. 
x <- seq(-1.95, 1.95, length = 500) 
y <- seq(-1.95, 1.95, length = 200) 
z <- outer(x, y, function(a, b) a*b^2) 

# make persp.object and draw it 
surf <- persp(x, y, z, col = "lightblue", border = NA, theta = -30) 

# draw lines parallel to x axis. seq(...) depends on your data's length(y) 
for(i in seq(10, 190, length=10)) lines(trans3d(x, y[i], z[,i], pmat = surf), col = "red") 
# draw lines parallel to y axis. seq(...) depends on your data's length(x) 
for(i in seq(25, 475, length=10)) lines(trans3d(x[i], y, z[i,], pmat = surf), col = "blue") 

plot