Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add shaded confidence intervals to line plot with specified values

Tags:

I have a small table of summary data with the odds ratio, upper and lower confidence limits for four categories, with six levels within each category. I'd like to produce a chart using ggplot2 that looks similar to the usual one created when you specify a lm and it's se, but I'd like R just to use the pre-specified values I have in my table. I've managed to create the line graph with error bars, but these overlap and make it unclear. The data look like this:

interval    OR  Drug    lower   upper 14  0.004   a   0.002   0.205 30  0.022   a   0.001   0.101 60  0.13    a   0.061   0.23 90  0.22    a   0.14    0.34 180 0.25    a   0.17    0.35 365 0.31    a   0.23    0.41 14  0.84    b   0.59    1.19 30  0.85    b   0.66    1.084 60  0.94    b   0.75    1.17 90  0.83    b   0.68    1.01 180 1.28    b   1.09    1.51 365 1.58    b   1.38    1.82 14  1.9 c   0.9 4.27 30  2.91    c   1.47    6.29 60  2.57    c   1.52    4.55 90  2.05    c   1.31    3.27 180 2.422   c   1.596   3.769 365 2.83    c   1.93    4.26 14  0.29    d   0.04    1.18 30  0.09    d   0.01    0.29 60  0.39    d   0.17    0.82 90  0.39    d   0.2 0.7 180 0.37    d   0.22    0.59 365 0.34    d   0.21    0.53 

I have tried this:

limits <- aes(ymax=upper, ymin=lower) dodge <- position_dodge(width=0.9) ggplot(data, aes(y=OR, x=days, colour=Drug)) +    geom_line(stat="identity") +    geom_errorbar(limits, position=dodge) 

and searched for a suitable answer to create a pretty plot, but I'm flummoxed!

Any help greatly appreciated!

like image 848
user4575913 Avatar asked Apr 20 '15 09:04

user4575913


People also ask

Can you add confidence intervals together?

Since 95% of values fall within two standard deviations of the mean according to the 68-95-99.7 Rule, simply add and subtract two standard deviations from the mean in order to obtain the 95% confidence interval. Notice that with higher confidence levels the confidence interval gets large so there is less precision.

What is confidence interval in line plot?

A confidence interval for the mean is a range of values between which the population mean possibly lies. If I'd make a weather prediction for tomorrow of somewhere between -100 degrees and +100 degrees, I can be 100% sure that this will be correct.


1 Answers

You need the following lines:

p<-ggplot(data=data, aes(x=interval, y=OR, colour=Drug)) + geom_point() + geom_line() p<-p+geom_ribbon(aes(ymin=data$lower, ymax=data$upper), linetype=2, alpha=0.1) 

enter image description here

like image 196
Ruthger Righart Avatar answered Oct 15 '22 13:10

Ruthger Righart