【问题标题】:plot coefficient point estimate with standard errors for factors用因子的标准误差绘制系数点估计
【发布时间】:2020-10-21 01:00:12
【问题描述】:

假设我有一个数据框

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 绘制但按因素(组=1 或组=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

【问题讨论】:

    标签: r ggplot2 tidyverse boxplot


    【解决方案1】:

    您只是想让每个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')
    

    【讨论】:

      猜你喜欢
      • 2021-02-28
      • 1970-01-01
      • 2018-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多