1

I have a simple feature collection (lines) with the geometry type "MULTILINESTRING" that i want to plot with ggplot2. The line thickness should represent the value of a field. Unfortunately I am not able to provide a reprex. But maybe there is an obvious mistake that can be easily identified.

lines

Output:

Simple feature collection with 171 features and 1 field
geometry type:  MULTILINESTRING
dimension:      XY
bbox:           xmin: 579649.7 ymin: 5801899 xmax: 625387 ymax: 5851019
projected CRS:  ETRS89 / UTM zone 32N
First 10 features:
   strahler                       geometry
1         1 MULTILINESTRING ((580016.3 ...
2         1 MULTILINESTRING ((582188.7 ...
3         1 MULTILINESTRING ((581499.9 ...
4         1 MULTILINESTRING ((586581.9 ...
5         1 MULTILINESTRING ((584296.4 ...
6         1 MULTILINESTRING ((583833.5 ...
7         1 MULTILINESTRING ((584608.8 ...
8         1 MULTILINESTRING ((583096.1 ...
9         1 MULTILINESTRING ((588869.2 ...
10        1 MULTILINESTRING ((587474.7 ...

I am plotting it with

lines %>% 
  mutate(strahler = as.integer(strahler)) %>% 
  ggplot() +
  geom_sf(aes(size = strahler))

The result looks really messy. It seems that using size this way affects the vertices of the lines.

enter image description here

Reprex

When I tried to provide a reprex, this messy look didn't occur

lines <-
  tibble(
    x = c(1, 2, 5, 2, 4),
    y = c(0, 3, 4, 3, 2),
    order = c(2L, 2L, 2L, 1L, 1L)
  ) %>%
  st_as_sf(coords = c("x", "y")) %>%
  st_sf(crs = 25832) %>%
  group_by(order) %>%
  summarise() %>%
  st_cast("MULTILINESTRING")
lines
Simple feature collection with 2 features and 1 field
geometry type:  MULTILINESTRING
dimension:      XY
bbox:           xmin: 1 ymin: 0 xmax: 5 ymax: 4
projected CRS:  ETRS89 / UTM zone 32N
# A tibble: 2 x 2
  strahler              geometry
     <int> <MULTILINESTRING [m]>
1        1          ((2 3, 4 2))
2        2     ((1 0, 2 3, 5 4))

Plotting it

lines %>% 
  mutate(strahler = as.integer(strahler)) %>% 
  ggplot() +
  geom_sf(aes(size = strahler))

enter image description here

I guess it has to do with the geometry field which looks different but I don't know how to convert it. I am happy about any hint!

MxNl
  • 371
  • 2
  • 9
  • Take at this for possible solutions: https://stackoverflow.com/a/54613325/7547327 – mrhellmann Oct 29 '20 at 10:23
  • @mrhellmann: Thanks, but I've seen this question before and was not helping me with this problem! – MxNl Oct 29 '20 at 10:49
  • Try setting the arguments 'lineend = ' and/or 'linejoin = ' in the call to 'geom_sf'. Options are "round, butt, square", and "round, mitre, and bevel" respectively. I'd start with 'round'. – mrhellmann Oct 29 '20 at 11:21

1 Answers1

0

Thanks to @mrhellmann: The proposed setting of the argument lineend = "round" in geom_sf() in combination with scale_size_identity() did solve this issue although it seems more like a work-around than a proper solution to me.

Using this code works:

lines %>% 
  mutate(strahler = as.integer(strahler)) %>% 
  ggplot() +
  geom_sf(aes(size = strahler)) +
  scale_size_identity()

enter image description here

However, a legend is not show using this solution!

MxNl
  • 371
  • 2
  • 9