Vermeiden von hoizontalen Linien und verrückten Formen beim Zeichnen von Karten in ggplot2
Ich möchte eine Darstellung eines Gebiets wie Lateinamerika mit der World Shape-Datei von IPUMSI ...
https: //international.ipums.org/international/resources/gis/IPUMSI_world.zi
... Ich werde später einige weitere IPUMS-Distrikte hinzufügen, daher möchte ich diese unbedingt als Vorlagenebene verwenden.
Ich habe Schwierigkeiten mit dem Plot, wenn ich durch @ Limits hinzufücoord_map
in ggplot2.
Die anfängliche räumliche Datei sieht in Ordnung aus
library("ggplot2")
library("raster")
sd0 <- readShapePoly("./IPUMSI_world.shp")
df0 <- fortify(sd0)
ggplot(data = df0, mapping = aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "black", colour = "black")
Wenn ich mich auf Lateinamerika konzentrieren möchte, erhalte ich einige unerwünschte horizontale Linien:
ggplot(data = df0, mapping = aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "black", colour = "black") +
coord_map(xlim = c(-125, -30), ylim = c(-60, 35))
Ich habe versucht, dies mit dem @ zu behebclipPolys
Funktion, unter Beachtung der AnleitungHie
library("PBSmapping")
df1 <- df0
names(df1)[c(1,2,6,3)] <- c("X","Y","PID","POS")
df1$PID <- as.numeric(df1$PID)
df2 <- clipPolys(polys = df1, xlim = c(-125, -30), ylim = c(-60, 35), keepExtra = TRUE)
names(df2)[names(df2)=="X"] <- "long"
names(df2)[names(df2)=="Y"] <- "lat"
names(df2)[names(df2)=="PID"] <- "id"
ggplot(data = df2, mapping = aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "black", colour = "black")
Nicht wirklich super zufrieden mit dieser Handlung. Ich dachte, es wäre ein Problem mit Löchern, wie in diesemFrag, aber die vorgeschlagene Lösung erzeugt die gleichen Diagramme wie oben:
gghole <- function(fort){
poly <- fort[fort$id %in% fort[fort$hole,]$id,]
hole <- fort[!fort$id %in% fort[fort$hole,]$id,]
out <- list(poly,hole)
names(out) <- c('poly','hole')
return(out)
}
ggplot(df2, aes(x=long, y=lat, group=group)) +
geom_polygon(data = gghole(df2)[[1]], fill = "black", colour = "black") +
geom_polygon(data = gghole(df2)[[2]], fill = "black", colour = "black")
ggplot(df0, aes(x=long, y=lat, group=group)) +
geom_polygon(data = gghole(df0)[[1]], fill = "black", colour = "black") +
geom_polygon(data = gghole(df0)[[2]], fill = "black", colour = "black") +
coord_map(xlim = c(-125, -30), ylim = c(-60, 35))