在ggplot2中绘制地图时避免水平线和疯狂的形状 [英] Avoiding hoizontal lines and crazy shapes when plotting maps in ggplot2
问题描述
我想要一个区域的阴谋,例如拉丁美洲,使用来自IPUMSI的世界形状文件...
当我想专注于拉丁美洲我得到了一些不需要的水平线:
$ $ $ $ $ $ $ $ $ ggplot(data = df0,mapping = aes(x = long,y = lat ,group = group))+
geom_polygon(fill =black,color =black)+
coord_map(xlim = c(-125,-30),ylim = c(-60, 35))
我尝试使用 clipPolys
函数来解决此问题,请遵循
对这个情节也不是很满意。我认为这是一个漏洞问题,就像这个问题一样,但是建议的解决方案会产生与上面相同的图:
gghole < - 函数(fort){
poly < - fort [fort% [$ fort $洞,$ $ id,]
洞< - 堡垒[!堡垒$ id%在堡垒[堡垒$洞,] $ id,]
出< - 列表)
名(out)< -c('poly','hole')
return(out)
}
ggplot(df2,aes(x = long,y = lat,group = group))+
geom_polygon(data = gghole(df2)[[1]],fill =black,color =black)+
geom_polygon data = gghole(df2)[[2]],fill =black,color =black)
ggplot(df0,aes(x = long,y = lat,group = group) )+
geom_polygon(data = gghole(df0)[[1]],fill =black,color =black)+
geom_polygon(data = gghole(df0)[[2]] ,fill =black,color =black)+
coord_map(xlim = c(-125,-30),ylim = c(-60,35))
I want a plot of an area, such as Latin America, using the world shape file from IPUMSI... https://international.ipums.org/international/resources/gis/IPUMSI_world.zip ... I will add some more IPUMS districts later so I really want to use this as my template layer. I having difficulty with the plot when I add limits through The initial spatial file looks alright When I want to focus on Latin America I get some unwanted horizontal lines: I tried to fix this using the Not really super happy with this plot either. I thought it was a problem with holes, as in this question, but the suggested solution produces the same plots as above:
The other solution is to restrict the view, rather than remove point from the render:
这篇关于在ggplot2中绘制地图时避免水平线和疯狂的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
库(ggplot2)
库(maptools)
库(mapproj)
#Maptools数据集
数据(wrld_simpl )
world < - fortify(wrld_simpl)
#同样的情节,但限制视图而不是删除点
#允许完成渲染
ggplot(世界,mapping = aes(x = long,y = lat,group = group))+
geom_polygon(fill =black,color =black)+
coord_cartesian(xlim = c( - 125,-30),ylim = c(-60,35))
coord_map
in ggplot2.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")
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))
clipPolys
function, following the guidance here 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")
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))
library(ggplot2)
library(maptools)
library(mapproj)
# Maptools dataset
data(wrld_simpl)
world <- fortify(wrld_simpl)
# Same plot, but restrict the view instead of removing points
# allowing the complete render to happen
ggplot(world, mapping = aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "black", colour = "black") +
coord_cartesian(xlim = c(-125, -30), ylim = c(-60, 35))