在geom_histogram中为从geom_vline创建的元素创建图例

时间:2014-06-26 19:17:28

标签: r plot ggplot2 histogram legend

以下是一个示例数据集:

structure(list(Age = c(6L, 7L, 5L, 6L, 7L, 9L,6L, 7L, 5L, 6L, 7L, 9L,6L, 7L, 5L, 6L, 7L, 9L), Year = c(2011, 
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011,     2011, 2011 )), .Names = c("Age", "Year"), row.names = c(NA, 6L), class = "data.frame")

我正在尝试创建一个图例,该图例将显示我在下面的geom_vline命令中列出的三个组件。我已经阅读了几个关于s.overflow的例子,但似乎没有任何工作......

这是我到目前为止所做的:

# to create standard errors and mean lines to plot on histogram
se <- function(x) sqrt(var(x)/length(x))
se_11 <- se(Age_2011$Age)
mean_11 <- mean(Age_2011$Age)
se_11_plus <- mean_11 + se_11
se_11_minus <- mean_11 - se_11

#plot
p11_age <- ggplot(Age_2011, aes(x=Age))+ 
geom_histogram(aes(y=(..count..)/sum(..count..)), binwidth=1, origin=-.5, fill="white",    color="black", show_guide=TRUE)+             
scale_y_continuous(labels=percent_format(), name="Frequency (%)")+  ## plotting in     percent frequency
xlab("Age (years)")+
scale_x_continuous(limits=c(1,45), breaks=seq(1,45,1))+
scale_colour_discrete(name="Units", guide="legend")+ #attempting to create legend

# vertical lines for mean and standard errors
geom_vline(aes(xintercept=mean(Age_2011$Age), na.rm=T), color="red", linetype="dashed",     size=1, show_guide=TRUE)+
geom_vline(aes(xintercept=se_11_plus), color ="blue", show_guide=TRUE)+
geom_vline(aes(xintercept=se_11_minus), color="blue", show_guide=TRUE)+

# creating custom legends using guides
scale_linetype_manual(name="test", labels =c("median", "test", "test2"), values =     c("median"=1, "test"=2, "test3"=3))+
theme(legend.key=element_rect(fill="white", color ="white"))+
theme(legend.background=element_blank())+
guides(colour=guide_legend(override.aes=list(linetype=0)),   fill=guide_legend(override.aes=list(linetype=0)), 
    shape=guide_legend(override.aes=list(linetype=0)),
    linetype=guide_legend())+

#title and background
ggtitle("Age Frequency Histogram of 2011 Catch")+
theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(),     panel.background=element_rect(colour="black", fill="white"))

所有geom_vlines显示但是当我只有一个直方图“系列”时我无法弄清楚如何获得一个图例,而我在传奇中想要的只是垂直线。

感谢任何帮助。感谢

1 个答案:

答案 0 :(得分:1)

这或多或少是你要求的吗?

library(ggplot2)
library(scales)
p11_age <- ggplot(Age_2011, aes(x=Age))+ 
  geom_histogram(aes(y=..count../sum(..count..)), binwidth=1, origin=-0.5, fill=NA, color="black")+             
  scale_y_continuous(name="Frequency (%)", labels=percent_format())+
  scale_x_continuous(name="Age (years)",limits=c(1,45), breaks=seq(1,45,1))+  
  # vertical lines for mean and standard errors
  geom_vline(aes(xintercept=mean_11, color="Mean", linetype="Mean"), size=1, show_guide=TRUE)+
  geom_vline(aes(xintercept=se_11_plus, color="Std.Err", linetype="Std.Err"), show_guide=TRUE)+
  geom_vline(aes(xintercept=se_11_minus, color="Std.Err", linetype="Std.Err"), show_guide=TRUE)+
  scale_colour_manual(name="Units", values=c(Std.Err="blue",Mean="red"))+
  scale_linetype_manual(name="Units", values=c(Mean="dashed",Std.Err="solid"), guide=FALSE)+
  ggtitle("Age Frequency Histogram of 2011 Catch")+
  theme(legend.background=element_blank(),
        panel.grid.major=element_blank(), panel.grid.minor=element_blank(), 
        panel.background=element_rect(colour="black", fill="white"))
p11_age

所以我们在geom_vline 的调用中使用颜色和线型美学添加了3个aes(...)图层。然后我们映射&#34; Mean&#34;和#34; Std.Err&#34;颜色为&#34;红色&#34;和&#34;蓝&#34;使用scale_color_manual(...),我们映射&#34;均值&#34;和#34; Std.Err&#34; linetypes to&#34; dashed&#34;和&#34;固体&#34;在致电scale_linetype_manual(...)时。注意在values=...参数中使用命名向量。我们还会在guide=FALSE的调用中使用scale_linetype_manual(...)来关闭线型指南的显示。这样做的原因是,否则图例中的线条将是实线和虚线(我认为这是您尝试使用override_aes)。