繁体   English   中英

将水平线添加到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()是另一种选择吗?

编辑错别字,澄清了不同的horziontal线的价值。

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

产生这个情节

在此输入图像描述

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

UPDATE

好的,现在我明白了...使用相同方法的快速近似将是使用多个条形向量和错误条调用。 这有点令人筋疲力尽,但可能会工作,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()。

我仍然得到警告,由于工作轮与在NAS中geom_errorbar()为载体yyminymax ,这得到更多方面更复杂,但它的工作原理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM