在 R 中的 ggplot2 中向堆叠条形图添加水平线,并在图例中显示

Add horizontal lines to stacked barplot in ggplot2 in R, and show in legend

我有一个堆叠条形图,类似于下面的示例。

我想在每个条上添加一组或两组水平线(指定颜色和线条类型),每个条的值不同,并将它们添加到图例中。

Titanic.df <- as.data.frame(Titanic)

Titanic.ag <- aggregate( Freq ~ Sex + Class + Age, data=Titanic.df, sum, subset = Survived == "Yes")

bars <- rep(c(0.5, NA, 0.7, NA, 0.6, NA, 0.9, NA), 2)

ggplot(Titanic.ag, aes(x = Class, y = Freq, fill = Sex)) + 
  geom_bar(position = "fill", stat = "identity") + 
  facet_grid(~Age) +
  geom_errorbar(aes(y = bars, ymin = bars, ymax = bars,  col = "Ref1")) + 
  scale_fill_manual(values = c("darkgreen", "darkblue") ) + 
  labs(col = "Reference",
       fill= "",
       y = "Proportion",
       x = "Class")

我已经尝试按照几个问题的建议使用 geom_errorbar(),但我被两件事困住了:

如果我为误差条添加一个值向量,则 ggplot 期望与数据帧中的长度相同(例如 Titanic.ag 中的 16),但只有 8 个条堆叠。这就是为什么我在上面的 bars 中使用了 NA。 有替代方案吗?

更重要的是,我想控制颜色和线型,但如果我将这些添加到 geom_bar(),我就失去了我的图例。例如

  geom_errorbar(aes(y = bars, ymin=bars, ymax=bars,  col = "Ref1"), col = "red", linetype = 2)

geom_segment() 是另一种选择吗?

编辑了拼写错误,澄清了水平线的不同值。

我不完全确定你需要这个但是。单独的 geom_abline 调用可让您轻松自定义每一行。

ggplot(Titanic.ag, aes(x = Class, y = Freq, fill = Sex)) + 
  geom_bar(position = "fill", stat = "identity") + 
  facet_grid(~Age) +
  geom_abline(slope=0, intercept=0.5,  col = "red",lty=2) +
  geom_abline(slope=0, intercept=0.75,  col = "lightblue",lty=2) +
  scale_fill_manual(values = c("darkgreen", "darkblue") ) + 
  labs(col = "Reference",
       fill= "",
       y = "Proportion",
       x = "Class")

制作这个情节

此外,也许 可以帮助处理标签。

更新

好的,现在我明白了...使用相同方法的快速近似是使用多个条向量和误差条调用。这有点让人筋疲力尽,但可能会奏效,而 data.frame 可能不会奏效,因为您希望每个栏都自定义多次。此外,考虑到 NA 已被删除,因此它们无法像人们希望的那样工作......也许使用大于 1 的数字并将 ylim 调整为 1 这样它们就不会显示

bars1 <- rep(c(0.5, NA, 0.7, NA, 0.6, NA, 0.9, NA), 2)
bars2 <- rep(c(NA, 0.9, 0.1, 0.1, NA, NA, NA, 0.5), 2)
bars3 <- rep(c(0.1, NA, NA, NA, NA, NA, NA, 0.1), 2)

ggplot(Titanic.ag, aes(x = Class, y = Freq, fill = Sex)) + 
  geom_bar(position = "fill", stat = "identity") + 
  facet_grid(~Age) +
  geom_errorbar(aes(y = bars1, ymin = bars1, ymax = bars1), color="Orange",lty=2) + 
  geom_errorbar(aes(y=bars2,ymin=bars2,ymax=bars2),color="White",lty=2)+
  geom_errorbar(aes(y=bars3,ymin=bars3,ymax=bars3),color="Purple",lty=2)+
  scale_fill_manual(values = c("darkgreen", "darkblue") ) + 
  labs(col = "Reference",
       fill= "",
       y = "Proportion",
       x = "Class")

上面的答案不会产生图例。它确实改变了线型和颜色,但我也在我原来的问题中展示了这一点。经过一番搜索,我找到了一种添加图例的方法,所以我在这里发布了。

library(ggplot2)

Titanic.df <- as.data.frame(Titanic)

Titanic.ag <- aggregate( Freq ~ Sex + Class + Age, data=Titanic.df, sum, subset = Survived == "Yes")

bars <- rep(c(0.5, NA, 0.7, NA, 0.6, NA, 0.9, NA), 2)

ggplot(Titanic.ag, aes(x = Class, y = Freq, fill = Sex)) + 
  geom_bar(position = "fill", stat = "identity") + 
  facet_grid(~Age) +
  geom_errorbar(aes(y = bars, ymin = bars, ymax = bars,  col = "Ref1"), linetype = 2, size = 1) + 
  scale_fill_manual(values = c("darkgreen", "darkblue") ) + 

  scale_colour_manual(name='', values=c("Ref1" = "darkred"), guide ="legend") +
  guides(col = guide_legend(override.aes = list(linetype=2), title = "Reference")) + 

  labs(fill= "",
       y = "Proportion",
       x = "Class")

这可以进一步添加 geom_errorbar()。

由于 geom_errorbar() 向量中的 NA 工作轮 yyminymax,我仍然收到警告,这变得更加复杂有更多方面,但它有效。