簡體   English   中英

ggplot 2:兩個分組變量的均值和95%CI的點圖

[英]ggplot 2: dot plot with mean and 95% CI for two grouping variables

我正在嘗試生成點圖,並為兩個獨立的分組變量添加均值和95%CI(所以我總共有2 x 2組)。

這是我的數據:

GluNorm Gender  Treatment
1.317   Male    NAC
1.278   Male    SAL
1.302   Male    SAL
1.376   Male    NAC
1.279   Male    NAC
1.308   Male    SAL
1.451   Male    NAC
1.244   Male    NAC
1.411   Male    SAL
1.16    Male    NAC
1.159   Male    NAC
1.42    Male    SAL
1.407   Male    SAL
1.62    Male    SAL
1.167   Male    SAL
1.377   Male    NAC
1.393   Female  SAL
1.203   Female  NAC
1.191   Female  NAC
1.132   Female  SAL
1.191   Female  SAL
1.589   Female  SAL
1.169   Female  NAC
1.155   Female  SAL
1.249   Female  NAC
1.401   Female  NAC
1.455   Female  SAL
1.481   Female  SAL
1.293   Female  NAC
1.332   Female  NAC
1.462   Female  SAL

這是我希望情節的樣子:

圖。1

我做了以下嘗試:

g<- ggplot(Data, aes(x = Gender, y = GluNorm, fill=Group))+
  geom_dotplot(binaxis='y', stackdir='center',stackratio=1.2, dotsize=1.2, binwidth=0.02, position=position_dodge(0.8)) 


#function that outputs mean, lower limit and upper limit of 95% CI
data_summary <- function(x) {
  m <- mean(x, na.rm=TRUE)
  sem <-sd(x, na.rm=TRUE)/sqrt(sum(!is.na(x)))
  ymin<-m-1.96*sem
  ymax<-m+1.96*sem
  return(c(y=m,ymin=ymin,ymax=ymax))
}

g +  stat_summary(fun.data=data_summary, color="red")

並得到以下結果:

fig2

誰能建議每個性別x治療組如何獲得單獨的點和條?

謝謝!

如果以不同的數量避開折線圖和置信區間,則圖形將更易於閱讀,以免重疊:

pd1 = position_dodge(0.2)
pd2 = position_dodge(0.65)

ggplot(Data, aes(x = Gender, y = GluNorm, fill=Treatment, color=Treatment))+
  geom_dotplot(binaxis='y', stackdir='center',stackratio=1.2, 
               dotsize=0.8, binwidth=0.02, position=pd2) +
  stat_summary(fun.data=data_summary, position=pd1, geom="errorbar", width=0.05) +
  stat_summary(fun.data=data_summary, position=pd1, geom="point", size=2) +
  scale_fill_manual(values=hcl(c(15,195), 100, 60)) +
  scale_color_manual(values=hcl(c(15,195), 50, 40)) +
  theme_bw()

在此處輸入圖片說明

除了使用data_summary函數,您還可以使用內置函數。 下面我們使用引導程序獲得95%的CI:

ggplot(Data, aes(x = Gender, y = GluNorm, fill=Treatment, color=Treatment))+
  geom_dotplot(binaxis='y', stackdir='center',stackratio=1.2, 
               dotsize=0.8, binwidth=0.02, position=pd2) +
  stat_summary(fun.data=mean_cl_boot, position=pd1, geom="errorbar", width=0.05) +
  stat_summary(fun.y=mean, position=pd1, geom="point", size=2) +
  scale_fill_manual(values=hcl(c(15,195), 100, 60)) +
  scale_color_manual(values=hcl(c(15,195), 50, 40)) +
  theme_bw()

在此處輸入圖片說明

除了點陣圖,您還可以直接繪制點,添加抖動以避免重疊:

ggplot(Data, aes(x = Gender, y = GluNorm, fill=Treatment, color=Treatment))+
  geom_point(position=position_jitterdodge(dodge.width=0.65, jitter.height=0, jitter.width=0.25),
             alpha=0.7) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.05, position=pd1) +
  stat_summary(fun.y=mean, geom="point", size=2, position=pd1) +
  scale_fill_manual(values=hcl(c(15,195), 100, 60)) +
  scale_color_manual(values=hcl(c(15,195), 50, 40)) +
  theme_bw()

在此處輸入圖片說明

也許是箱線圖:

pd1 = position_dodge(0.4)

ggplot(Data, aes(x = Gender, y = GluNorm, fill=Treatment, color=Treatment))+
  geom_boxplot(position=pd1, width=0.3, alpha=0.2, color="#00000080", 
               lwd=0.4, fatten=1.5) +
  stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.1, position=pd1, size=0.7) +
  stat_summary(fun.y=mean, geom="point", size=2, position=pd1) +
  scale_fill_manual(values=hcl(c(15,195), 100, 80)) +
  scale_color_manual(values=hcl(c(15,195), 50, 30)) +
  theme_bw()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM