ggplot2 geom_segment 大小可以指定为轴大小的比例吗?

问题描述

我在图中有一个 geom_segment 并且希望它的大小相对于 y 轴进行缩放。

例如:

iris %>%
  group_by(Species) %>%
  summarise(avg_sepal_length = mean(Sepal.Length)) %>%
  ggplot(aes(x = Species,y = avg_sepal_length)) +
  geom_bar(stat = "identity") +
  geom_segment(aes(x = 0.5,y = 10,xend = 3.5,yend = 10),size = 20)

在条形图上方 y=10 处生成一个水平段,固定大小为 20(我认为是毫米)。但是,当您调整绘图大小时,无论绘图的大小或轴的尺寸如何,它都会保持为 20。

我尝试使用 scale_size_continuous() 但似乎无法获得我正在寻找的结果。

有没有办法设置大小,以便在绘图大小改变时它会改变?

编辑: 附上200x200和1000x1000的导出,可以看到段高比例没有保持,段的高度是绝对的。

200x200

1000x1000

解决方法

类似的东西?

library(ggplot2)
my_max <- 10
iris %>%
  group_by(Species) %>%
  summarise(avg_sepal_length = mean(Sepal.Length)) %>%
  ggplot(aes(x = Species,y = avg_sepal_length)) +
  geom_bar(stat = "identity") +
  geom_segment(aes(x = 0.5,y = my_max,xend = 3.5,yend = my_max),size = my_max *2)

enter image description here

新比例:

my_max <- 100
iris %>%
  group_by(Species) %>%
  summarise(avg_sepal_length = mean(Sepal.Length)) %>%
  ggplot(aes(x = Species,size = my_max *2)

enter image description here