【问题标题】:Avoiding hoizontal lines and crazy shapes when plotting maps in ggplot2在 ggplot2 中绘制地图时避免水平线和疯狂的形状
【发布时间】:2016-12-15 16:29:45
【问题描述】:

我想要使用 IPUMSI 的世界形状文件绘制一个区域的图,例如拉丁美洲...

https://international.ipums.org/international/resources/gis/IPUMSI_world.zip

...稍后我会添加更多的 IPUMS 区,所以我真的很想将其用作我的模板层。

当我在 ggplot2 中通过 coord_map 添加限制时,我对情节有困难。

初始空间文件看起来不错

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))

按照here 的指导,我尝试使用clipPolys 函数解决此问题

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") 

对这个情节也不是很满意。我认为这是孔的问题,就像在这个question 中一样,但是建议的解决方案会产生与上面相同的图:

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))

【问题讨论】:

    标签: r ggplot2 maps gis polygon


    【解决方案1】:

    另一种解决方案是限制视图,而不是从渲染中移除点:

    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))
    

    【讨论】:

    • 我认为这会比在 scale_continuous 中设置限制要慢,因为整个数据将被计算然后裁剪。 scale_continuous 只会呈现您想要绘制的数据。
    • 速度较慢,但​​您不会丢失一些国家/地区。
    • 公平点。因此,我想这取决于首选的最终用途。国家的“缺失”位是您通过设置限制告诉它省略的部分,这可能是也可能不是用户想要的。
    【解决方案2】:

    您应该删除绘图区域之外的数据。所以,使用scale_x/y_continuous 来设置限制而不是coord_map

    ggplot(data = df0, mapping = aes(x = long, y = lat, group = group)) +
      scale_x_continuous(limits = c(-125, -30)) +
      scale_y_continuous(limits = c(-60, 35)) +
      geom_polygon(fill = "black", colour = "black")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多