5

I am trying to change the border colour on regions of and sf object when I plot it. Changing the colour of the border is fine, however, the lines are a bit thin so I want to make only the coloured lines thicker so they are more visible.

Reading this question suggests that using "lwd" will allow changes in thickness. However, when I try I either get no effect or extremly thick lines.

See the example below

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

enter image description here

How can I get only senible thicknesses like x% thicker? Ideally only the target edges will be changed.

Jonno Bourne
  • 1,931
  • 1
  • 22
  • 45
  • Not sure where `lwd` comes from. For `ggplot` this would be `size`, and then you may need to set `scale_size_*` with a manual or some other type of scale. You can't set the values you want inside `aes`, you set them with a scale – camille Sep 04 '19 at 20:57
  • 1
    I used lwd as they used that in the linked example. I tried with size I still end up with the same problem though. Can you make an example where one set of edges is controllably larger than the other, I don't really understand your comment. – Jonno Bourne Sep 04 '19 at 21:22
  • 1
    You can put `size = type == "High"` inside `aes`. Then there will be one size for true and one size for false. Then add `scale_size_manual` (or `_discrete`) and set the values there. You can't set the exact size (or any other aesthetics) you want inside `aes`. – camille Sep 04 '19 at 21:28

1 Answers1

4

You might want to use the lwd parameter outside of the 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"))

Good luck!

CRP
  • 405
  • 2
  • 11
  • This throws an error. `lwd` seems to have been a beta version of the `size` aesthetic, but the error comes because you need to set aesthetics inside `aes`, and you need to do it without the `nc$` – camille Sep 05 '19 at 00:04
  • 3
    ggplot2 3.4.0 replaces `size` with `linewidth` so ensure you are using that now. https://www.tidyverse.org/blog/2022/11/ggplot2-3-4-0/ – Anthony Martinez Dec 19 '22 at 22:25