平滑置信区间和点估计在ggplot中

huangapple go评论99阅读模式
英文:

Smooth confidence intervals and point estimated in ggplot

问题

以下是翻译好的代码部分:

  1. 我有以下数据:
  2. structure(list(RR = c(0.89, 0.9, 0.92, 0.93, 0.94, 0.95, 0.96,
  3. 0.98, 0.99, 1, 1.01, 1.03, 1.04, 1.05, 1.06, 1.08, 1.09, 1.11,
  4. 1.12, 1.13, 1.15), CI.upper = c(1, 1, 1.01, 1.01, 1.01, 1.01,
  5. 1.02, 1.03, 1.04, 1.05, 1.06, 1.08, 1.1, 1.12, 1.13, 1.16, 1.18,
  6. 1.21, 1.23, 1.25, 1.29), CI.lower = c(0.78, 0.8, 0.83, 0.85,
  7. 0.87, 0.89, 0.9, 0.93, 0.94, 0.95, 0.96, 0.98, 0.98, 0.98, 0.99,
  8. 1, 1, 1.01, 1.01, 1.01, 1.01), quan_demands = c(0, 5, 10, 15,
  9. 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95,
  10. 100)), class = "data.frame", row.names = c(NA, -21L))
  11. 我正在绘制RR和它们相应的置信区间,但似乎找不到使线条在点和置信区间之间更平滑的方法。
  12. 我尝试使用geom_smoothstat_smooth都没有成功。
  13. 我的代码如下:
  14. ggplot(data = data, aes(x= quan_demands, y = RR)) +
  15. geom_point(size = 1, shape = 19, color = "darkblue") +
  16. geom_line(size = 0.5, colour = "darkblue") +
  17. geom_ribbon(aes(ymin = CI.lower, ymax = CI.upper), linetype = 2, alpha = 0.4, fill =
  18. "deepskyblue3") +
  19. theme_bw() +
  20. scale_x_continuous(limits = c(0, 100),
  21. breaks = seq(0, 100, 25)) +
  22. scale_y_continuous(limits = c(0.75, 1.4),
  23. breaks = seq(0, 1.5, 0.2)) +
  24. geom_vline(xintercept = 45, color = "black", linetype = "dashed") +
  25. geom_hline(yintercept = 1, color = "black", linetype = "dashed") +
  26. theme(plot.background = element_rect(fill = "white"),
  27. panel.grid.major = element_blank(),
  28. panel.grid.minor = element_blank(),
  29. panel.border = element_rect(color = "black", size = 1),
  30. axis.ticks = element_line(color = "black", size = 1),
  31. axis.text = element_text(color = "black", size = 12),
  32. plot.margin = unit(c(1, 1, 1, 1), "mm"),
  33. strip.background = element_rect(fill = "deepskyblue3", size = 1),
  34. strip.text.x = element_text(colour = "white", size = 13)) +
  35. facet_grid(. ~ "title")

希望有所帮助! 平滑置信区间和点估计在ggplot中

英文:

I have the following data:

  1. structure(list(RR = c(0.89, 0.9, 0.92, 0.93, 0.94, 0.95, 0.96,
  2. 0.98, 0.99, 1, 1.01, 1.03, 1.04, 1.05, 1.06, 1.08, 1.09, 1.11,
  3. 1.12, 1.13, 1.15), CI.upper = c(1, 1, 1.01, 1.01, 1.01, 1.01,
  4. 1.02, 1.03, 1.04, 1.05, 1.06, 1.08, 1.1, 1.12, 1.13, 1.16, 1.18,
  5. 1.21, 1.23, 1.25, 1.29), CI.lower = c(0.78, 0.8, 0.83, 0.85,
  6. 0.87, 0.89, 0.9, 0.93, 0.94, 0.95, 0.96, 0.98, 0.98, 0.98, 0.99,
  7. 1, 1, 1.01, 1.01, 1.01, 1.01), quan_demands = c(0, 5, 10, 15,
  8. 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95,
  9. 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:

  1. ggplot(data = data, aes(x= quan_demands, y = RR)) +
  2. geom_point(size = 1, shape = 19, color = "darkblue") +
  3. geom_line(size = 0.5, colour = "darkblue") +
  4. geom_ribbon(aes(ymin = CI.lower, ymax = CI.upper), linetype = 2, alpha = 0.4, fill =
  5. "deepskyblue3") +
  6. theme_bw() +
  7. scale_x_continuous(limits = c(0, 100),
  8. breaks = seq(0, 100, 25)) +
  9. scale_y_continuous(limits = c(0.75, 1.4),
  10. breaks = seq(0, 1.5, 0.2)) +
  11. geom_vline(xintercept = 45, color = "black", linetype = "dashed") +
  12. geom_hline(yintercept = 1, color = "black", linetype = "dashed") +
  13. theme(plot.background = element_rect(fill = "white"),
  14. panel.grid.major = element_blank(),
  15. panel.grid.minor = element_blank(),
  16. panel.border = element_rect(color = "black", size = 1),
  17. axis.ticks = element_line(color = "black", size = 1),
  18. axis.text = element_text(color = "black", size = 12),
  19. plot.margin = unit(c(1, 1, 1, 1), "mm"),
  20. strip.background = element_rect(fill = "deepskyblue3", size = 1),
  21. strip.text.x = element_text(colour = "white", size = 13)) +
  22. facet_grid(. ~ "title")

Any help? 平滑置信区间和点估计在ggplot中

答案1

得分: 2

以下是代码的翻译部分:

主要数据系列大致呈线性关系,置信区间似乎遵循二次形状,因此您可以执行以下操作:

  1. data$upper <- predict(nls(CI.upper ~ a * (b + quan_demands)^2 + c, data,
  2. start = list(a = 1, b = 1, c = 1)))
  3. data$lower <- predict(nls(CI.lower ~ a * (b + quan_demands)^2 + c, data,
  4. start = list(a = 1, b = 1, c = 1)))

因此,绘图将如下所示:

  1. ggplot(data = data, aes(x= quan_demands, y = RR)) +
  2. geom_point(size = 1, shape = 19, color = "darkblue") +
  3. geom_smooth(method = 'lm', size = 0.5, colour = "darkblue") +
  4. geom_ribbon(aes(ymax = upper, ymin = lower),
  5. fill = 'deepskyblue4', alpha = 0.5) +
  6. theme_bw() +
  7. scale_x_continuous(limits = c(0, 100),
  8. breaks = seq(0, 100, 25)) +
  9. scale_y_continuous(limits = c(0.75, 1.4),
  10. breaks = seq(0, 1.5, 0.2)) +
  11. geom_vline(xintercept = 45, color = "black", linetype = "dashed") +
  12. geom_hline(yintercept = 1, color = "black", linetype = "dashed") +
  13. theme(plot.background = element_rect(fill = "white"),
  14. panel.grid.major = element_blank(),
  15. panel.grid.minor = element_blank(),
  16. panel.border = element_rect(color = "black", size = 1),
  17. axis.ticks = element_line(color = "black", size = 1),
  18. axis.text = element_text(color = "black", size = 12),
  19. plot.margin = unit(c(1, 1, 1, 1), "mm"),
  20. strip.background = element_rect(fill = "deepskyblue3", size = 1),
  21. strip.text.x = element_text(colour = "white", size = 13)) +
  22. 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:

  1. data$upper &lt;- predict(nls(CI.upper ~ a * (b + quan_demands)^2 + c, data,
  2. start = list(a = 1, b = 1, c = 1)))
  3. data$lower &lt;- predict(nls(CI.lower ~ a * (b + quan_demands)^2 + c, data,
  4. start = list(a = 1, b = 1, c = 1)))

So the plot would just be something like:

  1. ggplot(data = data, aes(x= quan_demands, y = RR)) +
  2. geom_point(size = 1, shape = 19, color = &quot;darkblue&quot;) +
  3. geom_smooth(method = &#39;lm&#39;, size = 0.5, colour = &quot;darkblue&quot;) +
  4. geom_ribbon(aes(ymax = upper, ymin = lower),
  5. fill = &#39;deepskyblue4&#39;, alpha = 0.5) +
  6. theme_bw() +
  7. scale_x_continuous(limits = c(0, 100),
  8. breaks = seq(0, 100, 25)) +
  9. scale_y_continuous(limits = c(0.75, 1.4),
  10. breaks = seq(0, 1.5, 0.2)) +
  11. geom_vline(xintercept = 45, color = &quot;black&quot;, linetype = &quot;dashed&quot;) +
  12. geom_hline(yintercept = 1, color = &quot;black&quot;, linetype = &quot;dashed&quot;) +
  13. theme(plot.background = element_rect(fill = &quot;white&quot;),
  14. panel.grid.major = element_blank(),
  15. panel.grid.minor = element_blank(),
  16. panel.border = element_rect(color = &quot;black&quot;, size = 1),
  17. axis.ticks = element_line(color = &quot;black&quot;, size = 1),
  18. axis.text = element_text(color = &quot;black&quot;, size = 12),
  19. plot.margin = unit(c(1, 1, 1, 1), &quot;mm&quot;),
  20. strip.background = element_rect(fill = &quot;deepskyblue3&quot;, size = 1),
  21. strip.text.x = element_text(colour = &quot;white&quot;, size = 13)) +
  22. facet_grid(. ~ &quot;title&quot;)

平滑置信区间和点估计在ggplot中

If we add points in to show where the actual 95% confidence intervals are, we see this is an excellent fit:

平滑置信区间和点估计在ggplot中

huangapple
  • 本文由 发表于 2023年3月10日 01:46:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75688273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定