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

Posted

技术标签:

【中文标题】将水平线添加到 R 中 ggplot2 中的堆叠条形图,并在图例中显示【英文标题】:Add horizontal lines to stacked barplot in ggplot2 in R, and show in legend 【发布时间】:2016-06-16 00:59:41 【问题描述】:

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

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

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(slope=0,intercept=yournumber, col= "yourcolor" , lty=2) 有用吗? 【参考方案1】:

我不完全确定您是否需要这个,但是。单独的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")

产生这个情节

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

更新

好的,现在我明白了……使用相同方法的快速近似是使用多个条形向量和误差条调用。这有点累,但可能会起作用,并且 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")

【讨论】:

谢谢,但为了澄清,我需要为每个条上的每条水平线设置不同的值。例如这里只有两个:bars &lt;- rep(c(0.5, NA, 0.7, NA), 4) @CCID 我还是没有得到你想要的,是不是像这里的最后一个例子? docs.ggplot2.org/0.9.3.1/geom_hline.html 我会再次编辑我的问题。我需要在每个条上绘制水平线,但每个条上的值不同。 geom_bar() 将执行此操作,但将我的工作区与 NA 一起使用,例如使用代码中上述注释中的bars 谢谢,这基本上是我上面的内容,你能指点我改变线条的类型和颜色吗?我在这方面的尝试改变了传奇 针对 lty=2 和自定义颜色进行了编辑。图例仍然很痛苦(考虑到您想为每个错误栏调用添加图例)【参考方案2】:

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

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

由于在yyminymaxgeom_errorbar() 向量中的NAs 的工作循环,我仍然收到警告,这会因更多方面而变得更加复杂,但它确实有效。

【讨论】:

以上是关于将水平线添加到 R 中 ggplot2 中的堆叠条形图,并在图例中显示的主要内容,如果未能解决你的问题,请参考以下文章

R语言ggplot2可视化整体排序的水平堆叠条形图(Ordered Stacked Horizontal Barplot)

R语言ggplot2可视化:可视化堆叠的直方图在bin中的每个分组部分添加数值标签为堆叠直方图中的每个分组部分添加数值标签

R语言使用ggplot2包使用geom_dotplot函数绘制分组点图(水平点图垂直点图点的大小堆叠比例)实战(dot plot)

R语言ggplot2可视化:可视化堆叠的直方图添加每个分组的每个bin的计数标签在堆叠直方图的bin中的每个分组部分添加数值标签

R语言ggplot2可视化整体排序的水平堆叠分离(dodge)条形图(Stacked Barplot Side By Side with position=dodge)

R语言ggplot2可视化:可视化堆叠的直方图在bin中的每个分组部分添加数值标签使用position_stack函数设置