CCI*_*CID 4 r legend bar-chart ggplot2 legend-properties
我有一个堆积的条形图,类似于下面的示例。
我想在每个条形图中添加一两套水平线(指定颜色和线型),每个条形以不同的值添加到图例中。
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")
Run Code Online (Sandbox Code Playgroud)
我曾尝试按照几个问题的建议使用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)
Run Code Online (Sandbox Code Playgroud)
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")
Run Code Online (Sandbox Code Playgroud)
产生这个情节
另外,也许这对标签有帮助。
更新
好的,现在我明白了……使用相同方法的快速近似将是使用多个柱状矢量和误差柱状调用。这有点累,但可能会工作,而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")
Run Code Online (Sandbox Code Playgroud)
上面的答案不会产生图例。它确实改变了线型和颜色,但我也在我原来的问题中展示了这一点。经过一番搜索,我找到了一种添加图例的方法,所以我在这里发布了。
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")
Run Code Online (Sandbox Code Playgroud)
可以对此进行修改以添加进一步的 geom_errorbar()。
geom_errorbar()由于 、 和的向量中的 NA 的解决方法,我仍然收到警告,随着y面ymin数ymax的增加,这会变得更加复杂,但它有效。