具有因子标准误差的图系数点估计

plot coefficient point estimate with standard errors for factors

假设我有一个数据框

rn = c('disease1', 'disease1', 'disease2', 'disease2') 
coef = c(0.1, 0.2, 0.3, 0.4) 
std = c(0.003, 0.003, 0.002, 0.0004) 
group = c(1,0,1,0)
df = data.frame(rn, coef, std)  

我想要做的是绘制一个图,在其中我将获得每个给定 rn 的系数和标准误差,但按因素(group=1 或 group=0)

现在我的代码是

library('ggplot2')
plot_heterogenous <- ggplot(df, aes(x=factor(rn), y=coef)) +
  geom_hline(yintercept = 0, color='firebrick') +
  geom_point(aes(fill=as.factor(group))) +
  geom_errorbar(aes(ymin=wave1_coef-wave1_std, ymax=wave1_coef+wave1_std), width=0, color="steelblue4") + 
  coord_flip() + scale_y_continuous(limits = c(-0.2, 0.2)) +
  theme_bw()+
  theme(panel.grid.major.x = element_blank(), 
        panel.grid.minor.x = element_blank(),
        panel.border = element_blank())+
  labs(y= "Treatment effect (standard deviations)", x="")

但它并没有像我想要的那样将其显示为分组箱线图。相反,它只是绘制关于组的一个点和另一个点,而我想要这样的东西 example

您是否只想让每个 rn 中的点数根据他们的组移动?在那种情况下,你想要的是使用位置“闪避”:

df = data.frame(rn = factor(c('disease1', 'disease1', 'disease2', 'disease2') )
                coef = c(0.1, 0.2, 0.3, 0.4) 
                std = c(0.003, 0.003, 0.002, 0.0004) 
                group = factor(c(1,0,1,0)))  

ggplot(df, aes(x=rn, y=coef)) +
  geom_point(aes(color=group), position = position_dodge(.9)) +
  geom_hline(yintercept = 0, color='firebrick')