0
votes

i created a boxplot using ggplot() and geom_boxplot() to display the results of a two-way anova (2x2 between subjects, both factors categorical), but im having trouble reordering the cell groups to my preferred aes() mapping.

here's what ive done so far:

# 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')) 

boxplot

I want to reorder the cell groups so that LD-male, LD-female, BD-male, BD-female are displayed in that order.

Thanks in advance,

2

2 Answers

0
votes

if f1 represents your cell groups, then you can modify the first line of your ggplot command as follows:

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

or in any other ordering you prefer.

0
votes

Many thanks for the suggestion. I actually posted the incorrect original code.

Regardless, I updated it with this and it worked!

# 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") 

updated boxplot