2015-01-20 29 views
1

感謝來自本網站某些用戶的幫助,我能夠使用geom_point爲某些數據獲得一個不錯的地圖圖。 (Get boundaries to come through on states)但是,現在我正試圖清理它,因爲我有更多年的陰謀,並希望確保情節正在工作並提供良好的信息。經過一些進一步的研究,似乎geom_tile會更好,因爲它會避開點並使用漸變。使用geom_tile清理地圖

我遇到的問題是讓代碼使用geom_tile。這不是什麼陰謀,我不知道爲什麼。

這裏的數據集:

https://www.dropbox.com/s/0evuvrlm49ab9up/PRISM_1895_db.csv?dl=0

這裏的原代碼與geom_points:

PRISM_1895_db <- read.csv("/.../PRISM_1895_db.csv") 

regions<- c("north dakota","south dakota","nebraska","kansas","oklahoma","texas","minnesota","iowa","missouri","arkansas", "illinois", "indiana", "wisconsin") 
ggplot() + 
    geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group)) + 
    geom_point(data = PRISM_1895_db, aes(x = longitude, y = latitude, color = APPT), alpha = .5, size = 3.5) + 
    geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group), color="white", fill=NA) 

enter image description here

這裏是我一直在努力的代碼,但沒有數據正在顯示。

ggplot() + 
    geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group)) + 
    geom_tile(data = PRISM_1895_db, aes(x = longitude, y = latitude, fill = APPT), alpha = 0.5, color = NA) 
    geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group), color="white", fill=NA) 

enter image description here

回答

4

geom_tile需要你的x和y的值上的規則網格進行採樣。它需要能夠以矩形平鋪表面。所以你的數據是不規則採樣的,不可能把原始數據分成一堆好的瓦片。

一種選擇是使用stat_summary2d圖層將數據劃分爲不同的框並計算該框中所有點的平均APPT。這將允許您創建常規瓷磚。例如

ggplot() + 
    geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group)) + 
    stat_summary2d(data=PRISM_1895_db, aes(x = longitude, y = latitude, z = APPT)) + 
    geom_polygon(data=subset(map_data("state"), region %in% regions), aes(x=long, y=lat, group=group), color="white", fill=NA) 

產生

enter image description here

你可以看看其他的選擇,如果你想控制這個塊大小。但正如你所看到的那樣,通過在倉內取平均值來「平滑」數據。

+0

這真是太好了,謝謝!我試圖弄清楚如何通過做bin = 50(知道默認值是30)來縮小bin的大小,但它對圖形沒有任何影響。我想縮小它們的大小,使它看起來像一個漸變而不是盒子,並且也將它們放在狀態的邊界內。我應該看看什麼選項來獲得這種外觀?再次感謝 – Vedda 2015-01-20 19:38:51

+0

參數名稱是'bins =',而不是'bin ='。你永遠不會讓瓷磚完全適合非矩形狀態邊界。如果這就是你想要的,這不是一個好策略。但這是一個與原來所提出的問題不同的問題。 – MrFlick 2015-01-20 19:43:16

+0

好的謝謝!這工作,但我仍然不確定這是否是格式化地圖的正確方法....我會更多地處理它,並在稍後發佈另一個問題。謝謝你的幫助。 – Vedda 2015-01-20 19:46:47