0

I'm sure that this question has been asked some how some way. But I've tried and I can't figure it! I'm trying to manage my legends in ggplot2. here's my code:

g <- ggplot(results, aes(x=index, color=includes_truth))
g <- g + geom_linerange(aes(ymax=ub.CACE_hat, ymin=lb.CACE_hat), alpha=.5)
g <- g + theme_bw()
g <- g + theme(axis.ticks.y = element_blank(), 
                axis.text.y = element_blank(), 
                axis.title.y = element_blank(),
                legend.position="bottom")
g <- g + ylab(expression("Estimate" %+-% 1.96*" Bootstrapped Standard Error"))
g <- g + ggtitle("Coverage of Bootstrapped 95% Confidence Interval")
g <- g + geom_hline(yintercept=true_cace,colour="red", linetype = "longdash")
g <- g + coord_flip()

I'm happy with the legend for the two kinds of intervals (includes truth or not) But I want the geom_hline to have a legend too that reads "True CACE".

I'd post a pic, but don't have enough rep just yet....

Here's the first 10 lines of the dataset:

    lb.CACE_hat   ub.CACE_hat      index includes_truth
910  -0.2925184  0.0081482459 -0.3425184          FALSE
835  -0.2760973  0.0009649981 -0.3260973          FALSE
382  -0.2749657 -0.0095546577 -0.3249657          FALSE
164  -0.2726825 -0.0171725490 -0.3226825          FALSE
931  -0.2615927 -0.0092937132 -0.3115927          FALSE
451  -0.2614677  0.0094599885 -0.3114677          FALSE
876  -0.2585660  0.0187700913 -0.3085660          FALSE
827  -0.2565962  0.0316608976 -0.3065962          FALSE
671  -0.2565856  0.0078546241 -0.3065856          FALSE
203  -0.2491911  0.0304410876 -0.2991911          FALSE
Alex Coppock
  • 2,122
  • 3
  • 15
  • 31
  • Can you provide a sample of your data so that we can test out your code? For example, to provide the first 10 rows of data, paste in the entire output of `dput(results[1:10,])`. Also, you don't need all the `g <- g + ...`. you can create the whole plot in one go by just adding a `+` to the end of each individual statement. See [here](http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_%28ggplot2%29/) for examples of the syntax. – eipi10 Nov 18 '14 at 05:58
  • Thanks for the suggestion eipi! I added the head of the dataset. I oscillate between the `g <- g +` and `+` stles, when I'm trying to understand each layer... – Alex Coppock Nov 18 '14 at 06:15
  • `true_cace` appears in your code but not in your data. To get a legend from `geom_hline` you need to map a variable in your data to some aesthetic. See [this SO question](http://stackoverflow.com/questions/12545322/add-vline-to-existing-plot-and-have-it-appear-in-ggplot2-legend) for an example. – eipi10 Nov 18 '14 at 06:39

0 Answers0