2016-11-01 1 views
0

Ich arbeite daran, die konsolidierten Z-Wert-Abweichungen (für eine Reihe von Faktoren) vom nationalen Durchschnitt für Pakistan auf einem verstärkten SPDF zu plotten. Für die Zwecke dieser Frage sind meine Daten irrelevant. Ich könnte es bei Bedarf bereitstellen.Wie fügt man diagonale Linien in NA-Wert-Polygonen mit ggplot hinzu?

Ich verwende ggplot meine Ausgabe, wo der Befehl zu erstellen und so etwas wie dies zur Folge aussehen:

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

Ich versuche jetzt, um herauszufinden, ob es möglich ist, diagonale Linien hinzufügen in den Polygonen, die fehlende Werte haben und grau gefärbt sind. Kann dies mit ggplot getan werden?

+0

Sie Linien zeichnen. Suchen Sie einfach einen Punkt innerhalb des Polygons und platzieren Sie eine Linie. Sie können einen Schwerpunkt mit 'coordinates()' eines SPDF finden. –

+0

Hey, ich bin relativ neu, um auf R zu zeichnen. Könntest du mir genauere Anweisungen geben? Mein SPDF heißt * pak * und angereicherte SPDF heißt * pak_mod *. –

Antwort

0

Dies ist ein Beispiel, das ich von here genommen habe. Ich entschied mich für die horizontale Fehlerleiste geom. Beachten Sie, dass dies nicht die einzige Möglichkeit ist, dies zu tun.

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

Danke! Dies half definitiv einige Verwirrungen zu beseitigen. Gesundheit. –

Verwandte Themen