繁体   English   中英

如何使用 ggplot2 重新排序 geom_boxplot() 中显示的单元组?

[英]how to i reorder cell groups displayed in geom_boxplot() using ggplot2?

我使用 ggplot() 和 geom_boxplot() 创建了一个箱线图来显示双向 anova 的结果(受试者之间为 2x2,两个因素都是分类的),但我无法将单元组重新排序为我首选的 aes() 映射。

这是我到目前为止所做的:

# libraries
library(tidyverse)
library(Rmisc)

# read data
DS = read.csv("....csv")
head(DS)
unique(DS$Binge)  
unique(DS$Gender)

# stripchart w box plots
df$f1f2 <- interaction(df$f1, df$f2)

ggplot(DS, aes(x=f1, y=ACC_L, fill=f2)) +

    geom_boxplot(position=position_dodge(0.7), width=.6, color='black',
                 outlier.alpha=FALSE) +
  
    geom_point(position=position_jitterdodge(.2),size=1) +

    stat_summary(fun="mean",geom = "point", size=5, 
               shape=18, position=position_dodge(0.7), color='lightcyan1') +

    theme_minimal() +
  
# Change the axis labels

#    ylab("FA") +
    xlab("Condition") +

# Make axis labels and tick labels larger
# and change to black

    theme( axis.title = element_text(size = 12, color = 'black'), 
       axis.text = element_text(size = 10, color = 'black')) 

箱形图

我想重新排序单元组,以便按顺序显示 LD-male、LD-female、BD-male、BD-female。

提前致谢,

如果f1代表您的单元组,那么您可以修改ggplot命令的第一行,如下所示:

ggplot(DS, aes(x=factor(f1,levels=c('LD-male','LD-female','BD-male','BD-female')), y=ACC_L, fill=f2)) +

或以您喜欢的任何其他顺序。

非常感谢您的建议。 我实际上发布了不正确的原始代码。

无论如何,我用它更新了它并且它有效!

# stripchart w box plots
#specify order of cell groups to be mapped in ggplot(); aes()
f1<-factor(DS$Binge,levels=c("LD","BD"))
f2<-factor(DS$Gender,levels=c("Male","Female"))

# ggplot () start
ggplot(DS, aes(x=f1, y=ACC_L, fill=f2)) +

    geom_boxplot(position=position_dodge(0.7), width=.6, color='black',
                 outlier.alpha=FALSE) +
  
    geom_point(position=position_jitterdodge(.3),size=1) +

    stat_summary(fun="mean",geom = "point", size=5, 
               shape=18, position=position_dodge(0.7), color='lightcyan1',
               show.legend=FALSE) +

    theme_minimal() +
  
# Change the colors
  
    scale_fill_manual( values = c('Female' = '#F8766D', 'Male' = '#00BFC4')) +
  
# Change the axis labels

#    ylab("FA") +
    xlab("Condition") 

更新的箱线图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM