问题
Here is an example data set:
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")
I am trying to create a legend that will show the three components that I list in my geom_vline command below. I've read several examples on s.overflow but nothing seems to be working..
This is what I have so far:
# 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"))
All of the geom_vlines show however I can't figure out how to get a legend when there is really only one histogram "series" and all I want in the legend are the vertical lines.
Any help is appreciated. Thanks
回答1:
Is this more or less what you're asking for?

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
So here we add the 3 geom_vline
layers, using color and linetype aesthetics inside the call to aes(...)
. Then we map the "Mean" and "Std.Err" colors to "red" and "blue" using scale_color_manual(...)
, and we map the "Mean" and "Std.Err" linetypes to "dashed" and "solid" in the call to scale_linetype_manual(...)
. Note the use of named vectors in the values=...
argument. We also turn off display of the linetype guide by using guide=FALSE
in the call to scale_linetype_manual(...)
. The reason for this is that otherwise the lines in the legend would be solid and dashed (I think this is what you were trying to do with override_aes
).
来源:https://stackoverflow.com/questions/24438462/creating-legend-in-geom-histogram-for-elements-created-from-geom-vline