【问题标题】:Strange behaviour when ggplotting line thickness depending on field of a sf "MULTILINESTRING"ggplotting线条粗细取决于sf“MULTILINESTRING”的字段时的奇怪行为
【发布时间】:2020-10-29 09:49:04
【问题描述】:

我有一个几何类型为“MULTILINESTRING”的简单特征集合(线),我想用 ggplot2 进行绘图。线条粗细应该代表一个字段的值。不幸的是,我无法提供代表。但也许有一个很容易识别的明显错误。

lines

输出:

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 ...

我正在绘制它

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

结果看起来真的很乱。似乎以这种方式使用大小会影响线条的顶点。

代表

当我尝试提供 reprex 时,并没有出现这种凌乱的外观

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

绘制它

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

我想这与看起来不同的几何字段有关,但我不知道如何转换它。我很高兴任何提示!

【问题讨论】:

  • 看看这个可能的解决方案:stackoverflow.com/a/54613325/7547327
  • @mrhellmann:谢谢,但我以前见过这个问题,并没有帮助我解决这个问题!
  • 尝试在对“geom_sf”的调用中设置参数“lineend =”和/或“linejoin =”。选项分别为“圆形、对接、方形”和“圆形、斜接和斜角”。我会从“圆形”开始。

标签: r ggplot2 sf


【解决方案1】:

感谢@mrhellmann:建议设置geom_sf() 中的参数lineend = "round"scale_size_identity() 相结合确实解决了这个问题,尽管对我来说这更像是一种变通方法而不是适当的解决方案。

使用此代码有效:

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

但是,使用此解决方案不会显示图例!

【讨论】:

    猜你喜欢
    • 2019-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多