英文:
Smooth confidence intervals and point estimated in ggplot
问题
以下是翻译好的代码部分:
我有以下数据:
structure(list(RR = c(0.89, 0.9, 0.92, 0.93, 0.94, 0.95, 0.96,
0.98, 0.99, 1, 1.01, 1.03, 1.04, 1.05, 1.06, 1.08, 1.09, 1.11,
1.12, 1.13, 1.15), CI.upper = c(1, 1, 1.01, 1.01, 1.01, 1.01,
1.02, 1.03, 1.04, 1.05, 1.06, 1.08, 1.1, 1.12, 1.13, 1.16, 1.18,
1.21, 1.23, 1.25, 1.29), CI.lower = c(0.78, 0.8, 0.83, 0.85,
0.87, 0.89, 0.9, 0.93, 0.94, 0.95, 0.96, 0.98, 0.98, 0.98, 0.99,
1, 1, 1.01, 1.01, 1.01, 1.01), quan_demands = c(0, 5, 10, 15,
20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95,
100)), class = "data.frame", row.names = c(NA, -21L))
我正在绘制RR和它们相应的置信区间,但似乎找不到使线条在点和置信区间之间更平滑的方法。
我尝试使用geom_smooth和stat_smooth都没有成功。
我的代码如下:
ggplot(data = data, aes(x= quan_demands, y = RR)) +
geom_point(size = 1, shape = 19, color = "darkblue") +
geom_line(size = 0.5, colour = "darkblue") +
geom_ribbon(aes(ymin = CI.lower, ymax = CI.upper), linetype = 2, alpha = 0.4, fill =
"deepskyblue3") +
theme_bw() +
scale_x_continuous(limits = c(0, 100),
breaks = seq(0, 100, 25)) +
scale_y_continuous(limits = c(0.75, 1.4),
breaks = seq(0, 1.5, 0.2)) +
geom_vline(xintercept = 45, color = "black", linetype = "dashed") +
geom_hline(yintercept = 1, color = "black", linetype = "dashed") +
theme(plot.background = element_rect(fill = "white"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_rect(color = "black", size = 1),
axis.ticks = element_line(color = "black", size = 1),
axis.text = element_text(color = "black", size = 12),
plot.margin = unit(c(1, 1, 1, 1), "mm"),
strip.background = element_rect(fill = "deepskyblue3", size = 1),
strip.text.x = element_text(colour = "white", size = 13)) +
facet_grid(. ~ "title")
希望有所帮助!
英文:
I have the following data:
structure(list(RR = c(0.89, 0.9, 0.92, 0.93, 0.94, 0.95, 0.96,
0.98, 0.99, 1, 1.01, 1.03, 1.04, 1.05, 1.06, 1.08, 1.09, 1.11,
1.12, 1.13, 1.15), CI.upper = c(1, 1, 1.01, 1.01, 1.01, 1.01,
1.02, 1.03, 1.04, 1.05, 1.06, 1.08, 1.1, 1.12, 1.13, 1.16, 1.18,
1.21, 1.23, 1.25, 1.29), CI.lower = c(0.78, 0.8, 0.83, 0.85,
0.87, 0.89, 0.9, 0.93, 0.94, 0.95, 0.96, 0.98, 0.98, 0.98, 0.99,
1, 1, 1.01, 1.01, 1.01, 1.01), quan_demands = c(0, 5, 10, 15,
20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95,
100)), class = "data.frame", row.names = c(NA, -21L))
I am plotting RR and their respective confidence intervals but I cannot seem to find a way to make the lines that runs across the points and the confidence intervals smoother.
I have tried to use geom_smooth and stat_smooth with no luck.
My code is this one:
ggplot(data = data, aes(x= quan_demands, y = RR)) +
geom_point(size = 1, shape = 19, color = "darkblue") +
geom_line(size = 0.5, colour = "darkblue") +
geom_ribbon(aes(ymin = CI.lower, ymax = CI.upper), linetype = 2, alpha = 0.4, fill =
"deepskyblue3") +
theme_bw() +
scale_x_continuous(limits = c(0, 100),
breaks = seq(0, 100, 25)) +
scale_y_continuous(limits = c(0.75, 1.4),
breaks = seq(0, 1.5, 0.2)) +
geom_vline(xintercept = 45, color = "black", linetype = "dashed") +
geom_hline(yintercept = 1, color = "black", linetype = "dashed") +
theme(plot.background = element_rect(fill = "white"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_rect(color = "black", size = 1),
axis.ticks = element_line(color = "black", size = 1),
axis.text = element_text(color = "black", size = 12),
plot.margin = unit(c(1, 1, 1, 1), "mm"),
strip.background = element_rect(fill = "deepskyblue3", size = 1),
strip.text.x = element_text(colour = "white", size = 13)) +
facet_grid(. ~ "title")
Any help?
答案1
得分: 2
以下是代码的翻译部分:
主要数据系列大致呈线性关系,置信区间似乎遵循二次形状,因此您可以执行以下操作:
data$upper <- predict(nls(CI.upper ~ a * (b + quan_demands)^2 + c, data,
start = list(a = 1, b = 1, c = 1)))
data$lower <- predict(nls(CI.lower ~ a * (b + quan_demands)^2 + c, data,
start = list(a = 1, b = 1, c = 1)))
因此,绘图将如下所示:
ggplot(data = data, aes(x= quan_demands, y = RR)) +
geom_point(size = 1, shape = 19, color = "darkblue") +
geom_smooth(method = 'lm', size = 0.5, colour = "darkblue") +
geom_ribbon(aes(ymax = upper, ymin = lower),
fill = 'deepskyblue4', alpha = 0.5) +
theme_bw() +
scale_x_continuous(limits = c(0, 100),
breaks = seq(0, 100, 25)) +
scale_y_continuous(limits = c(0.75, 1.4),
breaks = seq(0, 1.5, 0.2)) +
geom_vline(xintercept = 45, color = "black", linetype = "dashed") +
geom_hline(yintercept = 1, color = "black", linetype = "dashed") +
theme(plot.background = element_rect(fill = "white"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_rect(color = "black", size = 1),
axis.ticks = element_line(color = "black", size = 1),
axis.text = element_text(color = "black", size = 12),
plot.margin = unit(c(1, 1, 1, 1), "mm"),
strip.background = element_rect(fill = "deepskyblue3", size = 1),
strip.text.x = element_text(colour = "white", size = 13)) +
facet_grid(. ~ "title")
如果我们添加点来显示实际的95%置信区间,我们会看到这是一个出色的拟合。
英文:
The main series is approximately linear, and the confidence intervals seem to be following a quadratic shape, so you could do:
data$upper <- predict(nls(CI.upper ~ a * (b + quan_demands)^2 + c, data,
start = list(a = 1, b = 1, c = 1)))
data$lower <- predict(nls(CI.lower ~ a * (b + quan_demands)^2 + c, data,
start = list(a = 1, b = 1, c = 1)))
So the plot would just be something like:
ggplot(data = data, aes(x= quan_demands, y = RR)) +
geom_point(size = 1, shape = 19, color = "darkblue") +
geom_smooth(method = 'lm', size = 0.5, colour = "darkblue") +
geom_ribbon(aes(ymax = upper, ymin = lower),
fill = 'deepskyblue4', alpha = 0.5) +
theme_bw() +
scale_x_continuous(limits = c(0, 100),
breaks = seq(0, 100, 25)) +
scale_y_continuous(limits = c(0.75, 1.4),
breaks = seq(0, 1.5, 0.2)) +
geom_vline(xintercept = 45, color = "black", linetype = "dashed") +
geom_hline(yintercept = 1, color = "black", linetype = "dashed") +
theme(plot.background = element_rect(fill = "white"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_rect(color = "black", size = 1),
axis.ticks = element_line(color = "black", size = 1),
axis.text = element_text(color = "black", size = 12),
plot.margin = unit(c(1, 1, 1, 1), "mm"),
strip.background = element_rect(fill = "deepskyblue3", size = 1),
strip.text.x = element_text(colour = "white", size = 13)) +
facet_grid(. ~ "title")
If we add points in to show where the actual 95% confidence intervals are, we see this is an excellent fit:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论