【问题标题】:Setting edge/border thickness using geom_sf in ggplot [duplicate]在ggplot中使用geom_sf设置边缘/边框厚度[重复]
【发布时间】:2019-09-04 20:50:52
【问题描述】:

当我绘制它时,我正在尝试更改 sf 对象区域的边框颜色。更改边框的颜色很好,但是线条有点细,所以我只想让彩色线条更粗,以便更明显。

阅读this question 建议使用“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"))

我怎样才能只获得 x% 厚这样的明显厚度?理想情况下,只会更改目标边缘。

【问题讨论】:

  • 不确定lwd 来自哪里。对于ggplot,这将是size,然后您可能需要使用手动或其他类型的刻度设置scale_size_*。你不能在aes中设置你想要的值,你用一个比例来设置它们
  • 我使用了 lwd,就像他们在链接示例中使用的那样。我尝试了尺寸,但最终还是遇到了同样的问题。你能举一个例子,其中一组边可以控制地大于另一边吗,我不太明白你的评论。
  • 你可以把size = type == "High"放在aes里面。然后将有一种尺寸为真,一种尺寸为假。然后添加scale_size_manual(或_discrete)并在那里设置值。您无法在 aes 中设置您想要的确切大小(或任何其他美学)。

标签: r ggplot2 sf


【解决方案1】:

您可能希望在aes 之外使用lwd 参数:

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

祝你好运!

【讨论】:

  • 这会引发错误。 lwd 似乎是 size 美学的 beta 版本,但出现错误是因为您需要在 aes 中设置美学,并且您需要在没有 nc$ 的情况下执行此操作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
  • 1970-01-01
  • 2013-09-06
  • 1970-01-01
  • 2019-03-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多