【问题标题】:Reorder sf polygons by type in ggplot map在 ggplot 图中按类型重新排序 sf 多边形
【发布时间】:2021-11-14 18:07:08
【问题描述】:

我正在尝试使用 sf 包和 ggplot 将矢量对象从 shp 文件绘制到地图上。基本上:我创建了相关区域(美国西海岸)的底图,然后下载了显示鲸类生物重要区域 (BIA) 的相关形状文件(来自:https://cetsound.noaa.gov/important)。我为相关区域(美国西海岸)和区域类型(喂养和迁移)对 .shp 文件进行了子集化。我重新排序了 bia_select 列表,以便首先出现迁移区域。然后我将 BIA 分层到地图上,按类型着色。这是代码和生成的地图。

    # useful on MacOS to speed up rendering of geom_sf() objects
    if (!identical(getOption("bitmapType"), "cairo") && isTRUE(capabilities()[["cairo"]])
    {options(bitmapType = "cairo")
        }    
    
     #base map
    boundaries <- data.frame(x1 = c(-125.5, -125.5, -125.5), y1 = c(34.448, 36.306, 37.948), x2 = c(-120.472, -121.901, -122.785), y2 = c(34.448, 36.306, 37.948))#create gridpoints for regional boundaries
    
    states <- sf::st_as_sf(map("state", plot = FALSE, fill = TRUE))#state lines and coast
    CAfromstates <- states %>%
      subset(., states$ID == "california")
    
    world <- ne_countries(scale = "medium", returnclass = "sf")#national boundaries

    #import data
            biaf <- st_read("/Users/elizaoldach/Desktop/CetMap_BIA_WGS84-1/CetMap_BIA_WGS84.shp")
        
    #select geography and area types
    bia_select <- biaf  %>%
      filter(region %in% c("West Coast"),
             BIA_type %in% c("Feeding", "Migration"))

   #reorder features so migration comes first  
   bia_select_ordered <- bia_select[order(bia_select$OBJECTID, decreasing=FALSE),]

    #choose colors
    bia_colors <- c("plum2", "#CAB7A500")
    
    #create map
    map3 <- ggplot() +
      geom_sf(data = world, fill = "#CAB7A5") +
      geom_sf(data = states, fill = "#CAB7A5") +
      geom_sf(data = bia_select_ordered, alpha=0.5, aes(fill=BIA_type)) +
      scale_color_manual(values=bia_colors)+
      labs(fill="BIA Type")+
      coord_sf(
        xlim = c(-130,-108),
        ylim = c(22, 51),
        expand = FALSE
      )+
      theme_classic()
    map3

enter image description here

这张地图的问题在于,迁徙区域(蓝色,较大)被绘制在觅食区域(粉色,较小)之上。我想绘制这个,以便喂养区域位于顶部。重新排序列表以便首先出现迁移区域并没有帮助。我认为它必须相对简单,但我无法破解这个!有人给点建议吗?

【问题讨论】:

  • 我没有尝试下载你的数据并运行你的代码,但它似乎类似于stackoverflow.com/q/53036873/5325862
  • hhhmmm ...通常在ggplot中是相反的。在 ggplot_2 中。 2.0,执行顺序是基于因子水平的顺序。默认顺序将在堆栈顶部而不是底部绘制第一级。如果你想要堆栈底部的第一级,你可以使用 reverse = TRUE。如果一切都失败了,您可以创建 2 个单独的数据集,即 filter() 您的数据并将子数据集提供给 2 个不同的 geom_sf() 层。请注意,您必须对因子水平而不是数据集进行排序(无论您看到什么,ggplot 都将始终使用默认排序)。
  • 谢谢@camille - 我可以看到的区别是链接的帖子正在绘制单独的 sf 对象,而这个问题是同一对象中的类别。我可以将其拆分为单独的对象,但这似乎更容易创建图例。
  • 感谢@Ray,我认为这个问题——排序因子水平,而不是数据集——是我面临的一个关键问题。
  • 酷。很高兴看到你把它修好了。我经常与 ggplot 的默认排序作斗争。 :)

标签: r ggplot2 maps layer orders


【解决方案1】:

感谢您的回复。问题似乎是 BIA_type 仍然是一个字符,因此重新排序不起作用。这是一个简单的解决方法:

#check class of variable    
class(bia_select$BIA_type) 

#BIA_type is character, change to factor and specify levels 
bia_select$BIA_type<-factor(bia_select$BIA_type, levels=c("Migration","Feeding"))

这样,我能够重新运行 ggplot 代码并最终得到一张地图,其中饲养区位于迁移区之上。

【讨论】:

    猜你喜欢
    • 2019-03-10
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多