如何在 ggplot 中绘制虚线 95% 置信区间
How to plot a dashed line 95% confidence interval in ggplot
我是 R 的新手,所以这可能是一个简单的问题。
对于演示文稿,我需要制作一个带有置信区域的折线图。
数据代表化疗期间不同时间点的生活质量评分(时间点 0(基线)、时间点 4(4 个治疗周期后)和时间点 8(8 个周期后)。
这是我的数据:
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
这是我的数据结构(我把数据库从宽格式变成了长格式:
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
我想制作一个如下所示的情节:
到目前为止,我已经使用以下代码进行了此操作:
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"))
这给了我以下情节:
但我想用虚线更改这些误差线(一条在 X 轴上每个化疗周期点的平均值之间绘制的线上方和下方)
有人可以为我提供解决方案吗?另外,我应该使用什么设置来使 X 轴上的离散变量“0”向左移动(我想去掉行首和末尾的白色 space(这在视觉上更吸引我)
非常感谢!
您可以使用以下代码
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")
使用基本情节你可以这样做
#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)
我是 R 的新手,所以这可能是一个简单的问题。 对于演示文稿,我需要制作一个带有置信区域的折线图。
数据代表化疗期间不同时间点的生活质量评分(时间点 0(基线)、时间点 4(4 个治疗周期后)和时间点 8(8 个周期后)。
这是我的数据:
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
这是我的数据结构(我把数据库从宽格式变成了长格式:
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
我想制作一个如下所示的情节:
到目前为止,我已经使用以下代码进行了此操作:
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"))
这给了我以下情节:
但我想用虚线更改这些误差线(一条在 X 轴上每个化疗周期点的平均值之间绘制的线上方和下方)
有人可以为我提供解决方案吗?另外,我应该使用什么设置来使 X 轴上的离散变量“0”向左移动(我想去掉行首和末尾的白色 space(这在视觉上更吸引我)
非常感谢!
您可以使用以下代码
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")
使用基本情节你可以这样做
#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)