【发布时间】:2016-10-23 01:57:40
【问题描述】:
我正在尝试为使用 ggplot2 制作的复合图表生成一个好的图例,其中有一个闪避的条形图和一条用于显示土壤中钡的参考值的水平线。问题是线图图例的特征也是“污染”的条形图之一。我已经看到类似的主题试图调整提出的其他解决方案,但没有任何显着结果。
这是我的例子:
数据框:
Position Year Value
Position 1 1999 12
Position 2 1999 14
Position 3 1999 15
Position 1 2000 13
Position 2 2000 11
Position 3 2000 21
Position 1 2001 12
Position 2 2001 13
Position 3 2001 16
代码:
ggplot(input, aes(fill=Year , x=Position, y=Value)) +
geom_bar(stat="identity",
position="dodge", size=0.1, width=0.5, show.legend=TRUE) +
labs(title="Barium concentration in the soil") +
xlab("Samples") +
ylab("Concentration in ng/kg") +
scale_y_continuous(limits = c(0, 30)) +
#color of barplot
scale_fill_manual(values=c("grey40", "grey70", "grey80")) +
#limit value
geom_hline(aes(yintercept=40, colour = "red"), size=0.9, alpha=0.8, show.legend=TRUE )
这是图表
如您所见,hline 图例的功能正在浪费条形图。
【问题讨论】:
-
您需要将
colour="red'从您的geom_hline移出aes。 -
通过将 'colour="red'" ' 命令移到 'aes' 之外,您只需从图表中删除线图例。相反,我想保留它:“geom_hline”图例是可以的。问题是barplot图例被bar simbols内的黑线污染了,这个问题与'geom_hline'图例的创建有关。
-
数据的表示方式不能与代码一起使用。例如,年份变量必须格式化为字符,否则将不起作用。我建议使用
dput(your_data.frame)并将其添加到您的问题中。