2016-03-02 91 views
3

我有一个堆叠barplot,如下面的示例。将水平线添加到R中ggplot2中的堆叠barplot中,并在图例中显示

我想在每个小节中为每个小节添加一组或两组水平线(指定颜色和线型),并将其添加到图例中。

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

titanic stacked bar plot with horizontal lines

我一直在使用的建议的几个问题geom_errorbar()试过了,但我坚持了两件事情:

如果我添加值的向量误差线,然后ggplot需要的长度与数据帧中的长度相同(例如,Titanic.ag中的16),但叠加时只有8个小节。这就是为什么我在上面的bars中使用了NAs。 有其他选择吗?

更重要的是,我想控制颜色和线条类型,但是如果我将这些添加到geom_bar(),我会失去我的传奇。例如

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

是geom_segment()的替代?

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

+0

geom_abline(斜率= 0,截距= yournumber,col =“yourcolor”,lty = 2)是否有效? –

回答

0

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

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,这得到更多方面更复杂,但它的工作原理。

4

我不完全确定你需要这个,但是。个人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") 

主要生产该地块

enter image description here

而且,也许this可以与标签帮助。

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

enter image description here

+0

谢谢,但为了澄清,我需要每个横条上的每条水平线的不同值。例如这里只有两个:'bars < - rep(c(0.5,NA,0.7,NA),4)' – CCID

+0

@CCID我还没有得到你想要的,就像这里的最后一个例子? http://docs.ggplot2.org/0.9.3.1/geom_hline.html –

+0

我会再次编辑我的问题。我需要在每个小节上绘制水平线,但在每个小节上绘制不同的值。 geom_bar()会做到这一点,但使用我与NAs的workround,例如使用代码 – CCID