首页 > 解决方案 > 在 r 中使用 ggplot2 的两个因素的可信区间

问题描述

我在绘制这样的可信区间时遇到问题:在此处输入图像描述

我的数据结构如下,L1,L2,M,U1,U2 分别代表 0.025quant,0.25quant,0.5quant,0.75quant,0.975quant。

`

structure(list(approach = structure(c(1L, 2L, 1L, 2L, 1L, 2L), class = "factor", .Label = c("INLA", 
"rjags")), param = structure(c(1L, 2L, 3L, 1L, 2L, 3L), class = "factor", .Label = c("alpha", 
"beta", "sig2")), L1 = c(0.0844546867936143, 1.79242348175439, 
0.163143886545317, 0.0754165380733685, 1.79067991488052, 3.66675821267498
), L2 = c(0.60090835904286, 1.95337968870806, 0.898159977552433, 
0.606017177641373, 1.95260448314298, 4.07080184844179), M = c(0.870204161297956, 
2.03768437879748, 2.20651061559405, 0.87408237273113, 2.03725552264872, 
4.32531027636171), U2 = c(1.13905085248391, 2.12210930874551, 
4.26836270504725, 1.66260576926063, 2.28900567640091, 5.10063756831338
), U1 = c(1.65214011950274, 2.28396345192398, 4.9109804477583, 
1.1450384685802, 2.12117799328209, 4.55657971279654), AP = structure(c(1L, 
4L, 5L, 2L, 3L, 6L), .Label = c("INLA.alpha", "rjags.alpha", 
"INLA.beta", "rjags.beta", "INLA.sig2", "rjags.sig2"), class = "factor")), .Names = c("approach", 
"param", "L1", "L2", "M", "U2", "U1", "AP"), row.names = c(NA, 
-6L), class = "data.frame")`

我参考了这个答案在此处输入链接描述,但“填充”似乎仅适用于箱线图案例。到目前为止我尝试的代码是:

CI$AP=interaction(CI$approach,CI$param)
p=ggplot(CI,aes(y=AP))+geom_point(aes(x=M))
p=p+geom_segment(aes(x=L1,xend=U1,y=AP,yend=AP))
p=p+geom_segment(aes(x=L2,xend=U2,y=AP,yend=AP),size=1.5)

在此处输入图像描述

离我想要的还很远。

非常感谢!

标签: rggplot2

解决方案


以下情况如何:

ggplot(df, aes(x = param, y = M, colour = approach)) +
    geom_point(position = position_dodge2(width = 0.3), size = 3) +
    geom_linerange(
        aes(ymin = L2, ymax = U2, x = param),
        position = position_dodge2(width = 0.3),
        size = 2) +
    geom_linerange(
        aes(ymin = L1, ymax = U1, x = param),
        position = position_dodge2(width = 0.3),
        size = 1) +
    coord_flip() +
    labs(x = "Parameter", y = "Estimate")

在此处输入图像描述


样本数据

df <- structure(list(approach = structure(c(1L, 2L, 1L, 2L, 1L, 2L), class = "factor", .Label = c("INLA",
"rjags")), param = structure(c(1L, 2L, 3L, 1L, 2L, 3L), class = "factor", .Label = c("alpha",
"beta", "sig2")), L1 = c(0.0844546867936143, 1.79242348175439,
0.163143886545317, 0.0754165380733685, 1.79067991488052, 3.66675821267498
), L2 = c(0.60090835904286, 1.95337968870806, 0.898159977552433,
0.606017177641373, 1.95260448314298, 4.07080184844179), M = c(0.870204161297956,
2.03768437879748, 2.20651061559405, 0.87408237273113, 2.03725552264872,
4.32531027636171), U2 = c(1.13905085248391, 2.12210930874551,
4.26836270504725, 1.66260576926063, 2.28900567640091, 5.10063756831338
), U1 = c(1.65214011950274, 2.28396345192398, 4.9109804477583,
1.1450384685802, 2.12117799328209, 4.55657971279654), AP = structure(c(1L,
4L, 5L, 2L, 3L, 6L), .Label = c("INLA.alpha", "rjags.alpha",
"INLA.beta", "rjags.beta", "INLA.sig2", "rjags.sig2"), class = "factor")), .Names = c("approach",
"param", "L1", "L2", "M", "U2", "U1", "AP"), row.names = c(NA,
-6L), class = "data.frame")

推荐阅读