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

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

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")

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

英文:

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? 平滑置信区间和点估计在ggplot中

答案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 &lt;- predict(nls(CI.upper ~ a * (b + quan_demands)^2 + c, data, 
                          start = list(a = 1, b = 1, c = 1)))

data$lower &lt;- 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 = &quot;darkblue&quot;) + 
  geom_smooth(method = &#39;lm&#39;, size = 0.5, colour = &quot;darkblue&quot;) +
  geom_ribbon(aes(ymax = upper, ymin = lower),
              fill = &#39;deepskyblue4&#39;, 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 = &quot;black&quot;, linetype = &quot;dashed&quot;) +
  geom_hline(yintercept = 1, color = &quot;black&quot;, linetype = &quot;dashed&quot;) +
  theme(plot.background = element_rect(fill = &quot;white&quot;),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_rect(color = &quot;black&quot;, size = 1),
        axis.ticks = element_line(color = &quot;black&quot;, size = 1),
        axis.text = element_text(color = &quot;black&quot;, size = 12),
        plot.margin = unit(c(1, 1, 1, 1), &quot;mm&quot;),
        strip.background = element_rect(fill = &quot;deepskyblue3&quot;, size = 1),
        strip.text.x = element_text(colour = &quot;white&quot;, size = 13)) +
  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:

确定