2015-07-19 56 views
4

我試圖在空間上繪製一個連續變量。我看到這個例子得到我需要的相同的結果:在ggplot中繪製與geom_tile連續的強度

library("MASS") 
library("ggplot2") 
library(reshape2) 

DB<-melt(volcano) 
ggplot(DB, aes(x=Var1, y=Var2, fill=value)) +geom_tile() 

enter image description here

所以我跟我的數據所做的:

library(repmis) 
url<-"https://www.dropbox.com/s/4m5qk32wjgrjq40/dato.RDATA" 
source_data(url) 

library(ggplot2) 
ggplot(dato,aes(y=variable,x=y,fill=value))+geom_tile() 

enter image description here

那太好了。但是我的「x」和「y」距離空間點的公里距離(東部和北部)。我將它們在經緯度上進行了轉換。但現在我的情節不起作用!

ggplot(dato,aes(y=lat,x=long,fill=value))+geom_tile() 

enter image description here

我不明白爲什麼。反正繪製我的數據,如點結果非常相似:

ggplot(dato,aes(y=lat,x=long,fill=value))+geom_point() 
ggplot(dato,aes(y=variable,x=y,fill=value))+geom_point() 
+0

可能相關:http://docs.ggplot2.org/0.9.3.1 /scale_gradient.html, http://docs.ggplot2.org/0.9.2.1/scale_gradient2.html – VermillionAzure

+1

我認爲這是因爲緯度和經度點不是均勻分佈的,因此不能形成網格。如果距離均勻分佈,那麼座標不能成立,反之亦然。如果您繪製經緯度點,則會發現它們非常輕微地傾斜。你可能需要做一些插值才能使其工作。 – christoph

+0

我發現插值有點複雜。是否有geom_tile的替代方法來獲得相同的結果? – dax90

回答

12

你可以欺騙了一下,使用geom_point用矩形形狀:

#devtools::install_github("sjmgarnier/viridis") 
library(viridis) 
library(ggplot2) 
library(ggthemes) 
library(scales) 
library(grid) 

gg <- ggplot(dato) 
gg <- gg + geom_point(aes(x=long, y=lat, color=value), shape=15, size=5) 
gg <- gg + coord_equal() 
gg <- gg + scale_color_viridis(na.value="#FFFFFF00") 
gg <- gg + theme_map() 
gg <- gg + theme(legend.position="right") 
gg 

enter image description here

我沒有預測的LAT /長對,只用coord_equal。您應該爲正在映射的區域使用適當的投影。

而且,現在你有我好奇什麼這些熱點都是圍繞米蘭:-)

gmap <- get_map(location=c(9.051062, 45.38804, 9.277473, 45.53438), 
       source="stamen", maptype="toner", crop=TRUE) 
gg <- ggmap(gmap) 
gg <- gg + geom_point(data=dato, aes(x=long, y=lat, color=value), shape=15, size=5, alpha=0.25) 
gg <- gg + coord_map() 
gg <- gg + scale_color_viridis(na.value="#FFFFFF00") 
gg <- gg + theme_map() 
gg <- gg + theme(legend.position="right") 
gg 

enter image description here

+0

謝謝!這真是太棒了! – dax90