在 ggplot 中使用 geom_sf 设置边缘/边框厚度

Jon*_*rne 5 r ggplot2 r-sf

当我绘制它时,我试图更改 sf 对象区域的边框颜色。改变边框的颜色很好,但是,线条有点细,所以我只想让彩色线条变粗,这样它们就更明显了。

阅读这个问题表明使用“lwd”将允许改变厚度。然而,当我尝试时,要么没有效果,要么线条非常粗。

请参阅下面的示例

library(sf)
library(ggplot)
library(dplyr)

nc <- st_read(system.file("shape/nc.shp", package="sf")) %>%
  mutate(type= case_when(
    BIR74>16000 ~"High",
    TRUE ~"Low"
  ) %>% factor(levels = c("Low", "High"))) #the levels are ordered to avoid the grey lines overwriting the red ones

nc %>%
  ggplot(.) + 
  geom_sf(aes(fill = BIR74, colour = type)) + #does the job but the coloured borders are quite thing
  scale_color_manual(values = c( "#666666","#F8766D"))


nc %>%
  ggplot(.) + 
  geom_sf(aes(fill = BIR74, colour = type, 
              lwd = ifelse(type =="High", 1, 0.5)) #The values can be anything and it still looks rubbish
          ) + 
  scale_color_manual(values = c( "#666666","#F8766D"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我怎样才能得到合理的厚度,比如x%厚?理想情况下,只会更改目标边缘。

小智 5

您可能想使用lwd以下参数之外的参数aes

nc %>%
  ggplot(.) + 
  geom_sf(aes(fill = BIR74, colour = type),
          lwd = ifelse(nc$type =="High", 1.5, 0.5)
  ) + 
  scale_color_manual(values = c( "#666666","#F8766D"))
Run Code Online (Sandbox Code Playgroud)

祝你好运!

  • ggplot2 3.4.0 将“size”替换为“linewidth”,因此请确保您现在正在使用它。https://www.tidyverse.org/blog/2022/11/ggplot2-3-4-0/ (4认同)