r - Creating legend in geom_histogram for elements created from geom_vline -
r - Creating legend in geom_histogram for elements created from geom_vline -
here illustration info 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 trying create legend show 3 components list in geom_vline command below. i've read several examples on s.overflow nil seems working..
this have far:
# create standard errors , mean lines 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 create legend # vertical lines mean , 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 , 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 geom_vlines show can't figure out how legend when there 1 histogram "series" , want in legend vertical lines.
any help appreciated. thanks
is more or less 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 mean , 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 add together 3 geom_vline
layers, using color , linetype aesthetics inside phone call aes(...)
. map "mean" , "std.err" colors "red" , "blue" using scale_color_manual(...)
, , map "mean" , "std.err" linetypes "dashed" , "solid" in phone call scale_linetype_manual(...)
. note utilize of named vectors in values=...
argument. turn off display of linetype guide using guide=false
in phone call scale_linetype_manual(...)
. reason otherwise lines in legend solid , dashed (i think trying override_aes
).
r plot ggplot2 histogram legend
Comments
Post a Comment