r - Add geom_hline legend to existing geom bar legend -


i want add legend under existing legend represents dashed line, such dashed line labeled "avg tx effect" , placed under study 3.

enter image description here

library(ggplot2) library(ggthemes)  #dput(df)  df=structure(list(study = structure(c(1l, 2l, 3l, 1l, 2l, 3l, 1l,  2l, 3l, 1l, 2l, 3l, 1l, 2l, 3l), .label = c("study1", "study2",  "study3"), class = "factor"), d = c(-0.205, 0.1075, 0.3525, -0.37,  0.3, 0.42, -0.28, 0.09, 0.59, 0.11, -0.05, 0.25, 0, 0.25, 0.49 ), outcome = c(1l, 1l, 1l, 2l, 2l, 2l, 3l, 3l, 3l, 4l, 4l, 4l,  5l, 5l, 5l), outcome2 = structure(c(1l, 1l, 1l, 4l, 4l, 4l, 7l,  7l, 7l, 10l, 10l, 10l, 13l, 13l, 13l), .label = c("1", "1", "1",  "2", "2", "2", "3", "3", "3", "4", "4", "4", "5", "5", "5"), class = "factor")), .names = c("study",  "d", "outcome", "outcome2"), row.names = c(na, -15l), class = "data.frame")  ggplot(df, aes(x=outcome2, y=d, fill=study)) +     geom_bar(position=position_dodge(), aes(x=outcome2),stat="identity",              colour="black", # use black outlines,              size=.3) +      # thinner lines     xlab("outcome") +     ylab("cohen's d effect size") +     scale_fill_grey(name="study",                     labels=c("study1","study2", "study3"))+         theme_bw()+     geom_hline(yintercept=.15,linetype=2)    

as @gregor suggested, use direct label line adding annotate() shown below:

ggplot(df, aes(x=outcome2, y=d, fill=study)) +     geom_bar(position=position_dodge(), aes(x=outcome2),stat="identity",              colour="black", # use black outlines,              size=.3) +      # thinner lines     xlab("outcome") +     ylab("cohen's d effect size") +     scale_fill_grey(name="study",                     labels=c("study1","study2", "study3"))+         theme_bw()+     geom_hline(yintercept=.15,linetype=2) +annotate("text",x=.7,y=.17,size=3,label=c('avg tx ef'))  

enter image description here

if space issue can use wrapper described here wrap text. run wrapper <- function(x, ...) paste(strwrap(x, ...), collapse = "\n") add +annotate("text",x=.7,y=.18,size=3,label=wrapper('avg tx effect',10)). produces:

enter image description here


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -