Home > Software engineering >  ggplot - Reorder Bars
ggplot - Reorder Bars

Time:04-13

Dear Members of the Coding Community,

As the title of my query suggests I'd like to change the order of the bars in the following plot

(i.e. having both "immediate" bars appear before the "delayed" ones) :

ggplot(propho.effect.frame, aes(session, fit, color=modality, fill=modality))   geom_bar(stat='identity',position="dodge")   geom_errorbar(aes(ymin=fit-se, ymax=fit se), width=0.4, position=position_dodge(width=0.9))   theme_bw(base_size=12)   labs(y="score", title="Phonological Production Task")

enter image description here

## INITIAL DATAFRAME ##############################################

PID   session group list modality    stim score
1  AL201 immediate     A    Y     NOLM FAHRRAD     0
2  AL201 immediate     A    Y     NOLM    BUCH     1
3  AL201 immediate     A    X      OLM   SCHAF     0
4  AL201 immediate     A    X      OLM   STUHL     0
5  AL201 immediate     A    Y     NOLM    AFFE     0
6  AL201 immediate     A    Y     NOLM   KERZE     0
7  AL201 immediate     A    X      OLM  KIRCHE     0
8  AL201 immediate     A    X      OLM   NAGEL     0
9  AL201 immediate     A    X      OLM  PFANNE     0
10 AL201 immediate     A    Y     NOLM   KLEID     0
11 AL201 immediate     A    X      OLM    HAHN     0
12 AL201 immediate     A    X      OLM    KAMM     0
13 AL201 immediate     A    Y     NOLM    PILZ     1
14 AL201 immediate     A    Y     NOLM   BIRNE     0
15 AL201 immediate     A    X      OLM   VOGEL     0
16 AL201 immediate     A    X      OLM  SCHALE     0
17 AL201 immediate     A    X      OLM   PFEIL     0
18 AL201 immediate     A    Y     NOLM TROMMEL     0
19 AL201 immediate     A    X      OLM  LEITER     0
20 AL201 immediate     A    Y     NOLM    KORB     0
> 

## propho.effect.frame (cf. ggplot code) ##########################

modality   session       fit         se     lower     upper
1     NOLM   delayed 0.3525117 0.08066153 0.2140540 0.5211437
2      OLM   delayed 0.6472552 0.07339121 0.4942436 0.7750446
3     NOLM immediate 0.3271910 0.07654911 0.1974255 0.4901584
4      OLM immediate 0.5659339 0.07947359 0.4088293 0.7108196

After trying to apply the suggestions of similar queries on the forum without success (e.g. propho$session <- factor(propho$session, levels = c("immediate","delayed")) , I come to you in hope of finding an answer.

Thank you all in advance for any insights you could share with me.

CodePudding user response:

You can use factor with ordered = T to change the way that ggplot interprets the ordering. Your original plotting code can remain the same:

propho.effect.frame$session <- factor(propho.effect.frame$session, c('immediate', 'delayed'), ordered = T)

ggplot(propho.effect.frame, aes(session, fit, color=modality, fill=modality))   
  geom_bar(stat='identity',position="dodge")   
  geom_errorbar(aes(ymin=fit-se, ymax=fit se), width=0.4, position=position_dodge(width=0.9))   
  theme_bw(base_size=12)   labs(y="score", title="Phonological Production Task")

enter image description here

  • Related