2016-11-01 30 views
0

我正在努力繪製巴基斯坦全國平均強化SPDF的綜合Z值偏差(一系列因素)。就這個問題而言,我的數據是無關緊要的。如有必要,我可以提供。如何使用ggplot在NA值多邊形中添加對角線?

我使用ggplot創建我的輸出,其中的命令並導致這個樣子:

ggplot() + geom_polygon(data = plot.pakmod_sumZ, aes(x = long, y = lat, group = group, fill = SumZ.Cat), color = "black", size = 0.25, na.rm = TRUE) + scale_fill_manual(name = "Deviations from National Average", labels = c("-7", "-6", "-5", "-4", "-3", "-2", "-1", "Positive"), values = c("darkorange4","brown", "orangered1","tomato1","darkorange3","orange","yellow", "greenyellow"), na.value = "Grey", guide = guide_legend(reverse = TRUE)) + coord_map() + labs(x = NULL, y = NULL) + scale_x_discrete(breaks = NULL) + scale_y_discrete(breaks = NULL) + theme_minimal() 

Deviations from National Average

我想現在要弄清楚是否有可能添加斜線在具有缺失值且灰色的多邊形中。這可以使用ggplot

+0

你可以畫線。只需在多邊形內找到一個點並放置一條線即可。您可以使用SPDF的'coordinates()'找到質心。 –

+0

嗨,我在密謀R方面比較新。你能給我更具體的方向嗎? 我的SPDF被稱爲* pak *並且強化的SPDF被稱爲* pak_mod *。 –

回答

0

這是我從here拿下的一個例子。我選擇使用水平誤差棒幾何。請注意,這不是這樣做的唯一方法。

library(ggplot2) 
library(sp) 
library(rgdal) 
library(rgeos) 

# create a local directory for the data 
localDir <- "R_GIS_data" 
if (!file.exists(localDir)) { 
    dir.create(localDir) 
} 

# download and unzip the data 
url <- "ftp://www.ecy.wa.gov/gis_a/inlandWaters/wria.zip" 
file <- paste(localDir, basename(url), sep='/') 
if (!file.exists(file)) { 
    download.file(url, file) 
    unzip(file,exdir=localDir) 
} 

# create a layer name for the shapefiles (text before file extension) 
layerName <- "WRIA_poly" 

# read data into a SpatialPolygonsDataFrame object 
dataProjected <- readOGR(dsn=localDir, layer=layerName) 

[email protected]$id <- rownames([email protected]) 

# create a data.frame from our spatial object 
watershedPoints <- fortify(dataProjected) 

# merge the "fortified" data with the data from our spatial object 
watershedDF <- merge(watershedPoints, [email protected], by = "id") 

[email protected]$id <- rownames([email protected]) 

watershedPoints <- fortify(dataProjected) 

watershedDF <- merge(watershedPoints, [email protected], by = "id") 

ggWatershed <- ggplot(data = watershedDF, aes(x=long, y=lat, group = group, fill = WRIA_NM)) + 
    geom_polygon() + 
    geom_path(color = "white") + 
    scale_fill_hue(l = 40) + 
    coord_equal() + 
    theme(legend.position = "none", title = element_blank()) 

# Adding coordinates to the data part of SPDF. `sd` is the variable of interest 
# which is beign plotted here. Each line extends sd away from long coordinate 
[email protected]$sd <- rnorm(nrow(xy), mean = 50000, sd = 10000) 
xy <- coordinates(dataProjected) 
[email protected]$long <- xy[, 1] 
[email protected]$lat <- xy[, 2] 

ggWatershed + 
    geom_errorbarh(data = [email protected], aes(group = id, xmin = long - sd, xmax = long + sd)) 

enter image description here

+0

謝謝!這絕對有助於消除一些混亂。祝福你。 –