Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to plot a dashed line 95% confidence interval in ggplot

I'm fairly new to R so this might be a simple problem. For a presentation I need to make a line graph with the confidence region.

The data represent Quality of life scores at different timepoints during chemotherapy treatment (timepoint 0 (baseline), timepoint 4 (after 4 cycles of treatment) and timepoint 8 (after 8 cycles).

Here is my data:

ID Time Score
146 8   0.0
4   0   16.7
51  0   16.7
94  4   25.0
117 4   25.0
26  0   25.0
149 8   33.3
151 8   33.3
153 8   33.3
93  4   33.3
103 4   33.3
31  0   33.3
57  0   33.6
145 8   41.6
33  0   41.6
79  4   41.7
5   0   41.7
6   0   41.7
15  0   41.7
38  0   41.7
133 8   50.0
177 8   50.0
75  4   50.0
80  4   50.0
87  4   50.0
88  4   50.0
98  4   50.0
100 4   50.0
114 4   50.0
12  0   50.0
28  0   50.0
32  0   50.0
37  0   50.0
44  0   50.0
47  0   50.0
49  0   50.0
50  0   50.0
54  0   50.0
122 8   58.3
135 8   58.3
139 8   58.3
154 8   58.3
158 8   58.3
176 8   58.3
82  4   58.3
104 4   58.3
105 4   58.3
2   0   58.3
3   0   58.3
21  0   58.3
25  0   58.3
42  0   58.3
45  0   58.3
140 8   66.7
147 8   66.7
148 8   66.7
160 8   66.7
162 8   66.7
169 8   66.7
180 8   66.7
62  4   66.7
73  4   66.7
77  4   66.7
85  4   66.7
92  4   66.7
101 4   66.7
106 4   66.7
109 4   66.7
118 4   66.7
10  0   66.7
13  0   66.7
16  0   66.7
17  0   66.7
18  0   66.7
24  0   66.7
27  0   66.7
29  0   66.7
30  0   66.7
34  0   66.7
43  0   66.7
53  0   66.7
55  0   66.7
56  0   66.7
58  0   66.7
1   0   67.0
125 8   75.0
129 8   75.0
137 8   75.0
155 8   75.0
71  4   75.0
74  4   75.0
95  4   75.0
113 4   75.0
22  0   75.0
46  0   75.0
59  0   75.0
121 8   83.3
126 8   83.3
127 8   83.3
128 8   83.3
130 8   83.3
131 8   83.3
134 8   83.3
136 8   83.3
142 8   83.3
143 8   83.3
150 8   83.3
152 8   83.3
170 8   83.3
172 8   83.3
173 8   83.3
178 8   83.3
179 8   83.3
61  4   83.3
65  4   83.3
66  4   83.3
68  4   83.3
69  4   83.3
76  4   83.3
84  4   83.3
90  4   83.3
96  4   83.3
97  4   83.3
99  4   83.3
110 4   83.3
112 4   83.3
116 4   83.3
119 4   83.3
7   0   83.3
8   0   83.3
9   0   83.3
11  0   83.3
14  0   83.3
20  0   83.3
35  0   83.3
36  0   83.3
39  0   83.3
40  0   83.3
41  0   83.3
52  0   83.3
144 8   91.7
156 8   91.7
159 8   91.7
168 8   91.7
120 4   91.7
19  0   91.7
60  0   91.7
166 8   100.0
67  4   100.0
83  4   100.0
23  0   100.0
48  0   100.0
123 8   NA
124 8   NA
132 8   NA
138 8   NA
141 8   NA
157 8   NA
161 8   NA
163 8   NA
164 8   NA
165 8   NA
167 8   NA
171 8   NA
174 8   NA
175 8   NA
63  4   NA
64  4   NA
70  4   NA
72  4   NA
78  4   NA
81  4   NA
86  4   NA
89  4   NA
91  4   NA
102 4   NA
107 4   NA
108 4   NA
111 4   NA
115 4   NA
Showing 1 to 21 of 180 entries, 2 total columns

This is the structure of my data (i have melted the database from wide to long format:

str(Qol3)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   180 obs. of  2 variables:
 $ Time : Factor w/ 3 levels "0","4","8": 1 1 1 1 1 1 1 1 1 1 ...
 $ Score: num  67 58.3 58.3 16.7 41.7 41.7 83.3 83.3 83.3 66.7 ...

summary(Qol3)
 Time       Score       
 0:60   Min.   :  0.00  
 4:60   1st Qu.: 56.23  
 8:60   Median : 66.70  
        Mean   : 66.94  
        3rd Qu.: 83.30  
        Max.   :100.00  
        NA's   :28      
> 

head(Qol3)
# A tibble: 6 x 2
  Time  Score
  <fct> <dbl>
1 0      67  
2 0      58.3
3 0      58.3
4 0      16.7
5 0      41.7
6 0      41.7

I would like to make a plot that looks like this: enter image description here

So far I got this to work with the following code:

ggplot(data=Qol3, aes(x=Time, y=Score, group=1)) +
  stat_summary(fun.y=mean, geom = "line") +
  stat_summary(fun.y=mean, geom = "point") +
  xlab("Induction cycle") + ylab("Score") +
  ggtitle("EORTC QLQ-C30")+
  stat_summary(fun.data = mean_cl_boot, geom = "errorbar", width = .2)+
  expand_limits(y=c(55, 85)) +
  theme(panel.background = element_rect(fill = "white"),
        panel.grid.minor = element_line(color = "gray90", size = 0.25),
        plot.background = element_rect(fill = "gray90"))

Which gives me the following plot:

enter image description here

But I want to change those errorbars with a dashed line (one above and one under the line that is drawn between the means of each chemotherapy cycle point on the X axis)

Can somebody please provide the solution for me? Also what settings should i use to make the dicrete variable '0' on the X-axis move to the left (i want to get rid of the white space at the start en end of the line (this is visually more appealing to me)

Many thanks!

like image 811
Karima21 Avatar asked Oct 16 '25 14:10

Karima21


1 Answers

You can use the following code

ggplot(Qol3, aes(x=Time , y=Score)) + 
  stat_summary(geom = "line", fun.y = mean) +
  stat_summary(geom="ribbon", fun.data=mean_cl_boot, 
               conf.int=0.95, alpha = 0.0, linetype="dashed", colour="red")

Using basic plot you can do like

#Calculation of confidence interval
library(Rmisc)

try <- group.CI(Score ~ Time,
         data=Qol3,
         ci = 0.95)

plot(try$Time, try$Score.mean, type = "l", xlab="Induction cycle", ylab= "Score")
lines(try$Time, try$Score.upper, lty = 2)
lines(try$Time, try$Score.lower, lty = 2)
like image 160
Bappa Das Avatar answered Oct 18 '25 07:10

Bappa Das



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!