2015-02-11 47 views
1

我正在努力產生一個面圖/格圖的各分佈圖,每個圖顯示不同的模型運行如何影響一個變量被映射到多個多邊形。問題在於輸出圖形會在每個繪圖中的多邊形之間產生奇怪的線條(請參見下圖)。ggplot2面shapefile多邊形的產生奇怪的線

雖然我已經操縱Shapefile並將其轉換爲具有適合ggplot2屬性的數據框,但我不熟悉如何使用該軟件包的詳細信息,並且聯機文檔僅限於這種複雜的軟件包。我不確定哪個參數導致此問題,但我懷疑它可能是aes參數。

腳本:出現

library(rgdal, tidyr, maptools, ggplot2, dplyr, reshape2) 

setwd('D:/path/to/wd') 

waterloo <- read.table("waterloo-data.txt", header=TRUE, sep=',', stringsAsFactors=FALSE) 
waterloo <- data.frame(waterloo$DAUID, waterloo$LA0km, waterloo$LA4_exp, waterloo$LA20km, waterloo$LA30km, waterloo$LA40km, waterloo$LA50km) 
colnames(waterloo) <- c("DAUID", "LA0km", "LA10km","LA20km", "LA30km", "LA40km", "LA50km") 

## Produces expenditure measurements by ID variable DAUID, using reshape2/melt 
wtidy <- melt(waterloo, id.vars=c("DAUID"), measure.vars = c("LA0km", "LA10km", "LA20km", "LA30km", "LA40km", "LA50km")) 
colnames(wtidy) <- c("DAUID", "BufferSize", "Expenditure") 

wtidy$DAUID <- as.factor(wtidy$DAUID) # for subsequent join with wtrl_f 

### READ SPATIAL DATA ### 
#wtrl <- readOGR(".", "Waterloo_DA_2011_new") 
wtrl <- readShapeSpatial("Waterloo_DA_2011_new") 
wtrl$id <- row.names(wtrl) 

wtrl_f <- fortify(wtrl) 
wtrl_f <- left_join(wtrl_f, [email protected], by="id") 

# Join wtrl fortified (wtrl_f) to either twaterloo or wtidy 
wtrl_f <- left_join(wtrl_f, wtidy, by="DAUID") 

### PLOT SPATIAL DATA ### 
ggplot(data = wtrl_f, # the input data 
     aes(x = long.x, y = lat.x, fill = Variable/1000, group = BufferSize)) + # define variables 
    geom_polygon() + # plot the DAs 
    geom_path(colour="black", lwd=0.05) + # polygon borders 
    coord_equal() + # fixed x and y scales 
    facet_wrap(~ BufferSize, ncol = 2) + # one plot per buffer size 
    scale_fill_gradient2(low = "green", mid = "grey", high = "red", # colors 
         midpoint = 10000, name = "Variable\n(thousands)") + # legend options 
    theme(axis.text = element_blank(), # change the theme options 
     axis.title = element_blank(), # remove axis titles 
     axis.ticks = element_blank()) # remove axis ticks 

輸出圖形如下:

enter image description here 奇怪!我取得了很好的進展,但我不知道ggplot在哪裏獲得這些線。任何幫助,將不勝感激!

PS;作爲另外一個無關的問題,多邊形線條相當不規則。我將如何平滑這些線?

+0

如果你能提供一個最小的,可重複的例子,它會容易得多。最小含義只是足夠的代碼和數據來顯示問題。可重複的意思是,我可以將您的代碼完全按照提供的方式粘貼到新的R會話中,並無誤地運行它(除了錯誤,您當然需要幫助......) – bdemarest 2015-02-11 23:06:17

回答

0

OK,我設法通過改變GGPLOT2說明書的第11頁上找到的審美組參數來解決這個問題: http://cran.r-project.org/web/packages/ggplot2/ggplot2.pdf

正確的參數是「組」,而不是因子用來組地塊。正確ggplot代碼:

ggplot(data = wtrl_f, # the input data 
     aes(x = long.x, y = lat.x, fill = Expenditure/1000, group = group)) + # define variables 
    geom_polygon() + # plot the DAs 
    geom_path(colour="black", lwd=0.025) + # DA borders 
    coord_equal() + # fixed x and y scales 
    facet_wrap(~ BufferSize, ncol = 2) + # one plot per buffer size 
    scale_fill_gradient2(low = "green", mid = "grey", high = "red", # colors 
         midpoint = 10000, name = "Expenditures\n(thousands)") + # legend options 
    theme(axis.text = element_blank(), # change the theme options 
     axis.title = element_blank(), # remove axis titles 
     axis.ticks = element_blank()) # remove axis ticks 
2

這個答案幫助我解決我的問題,但在此之前我做了這個小例子,準備張貼。我在這裏分享它,以防止他們更快地解決相同的問題。

問題: 我想在g中使用ggplot2製作一個基本的地圖。多邊形填充錯誤,製造多餘的線條。

library("ggplot2") 
library("maps") 

map <- ggplot(map_data("world", region = "UK"), aes(x = long, y = lat)) + geom_polygon() 
map 

wrong map image

解決方案: 我必須設置唯美「組」參數把多邊形點以正確的順序,否則ggplot會嘗試繪圖蘇格蘭海岸線的補丁在中間南岸(例如)。

map <- ggplot(map_data("world", region = "UK"), aes(x = long, y = lat, group = group)) + geom_polygon() 
map