0

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.

Quinten
  • 35,235
  • 5
  • 20
  • 53
asderad
  • 3
  • 2
  • Hi @asderad, could you please share your data using `dput(df)`? So we can help you better. – Quinten Apr 11 '22 at 17:02
  • Does this answer your question? [Order Bars in ggplot2 bar graph](https://stackoverflow.com/questions/5208679/order-bars-in-ggplot2-bar-graph) – jdobres Apr 11 '22 at 18:02
  • @Quinten, thank you for your reply. As the "dput" output exceeds my console backlog, it seems too exhaustive to post it here as is. Can I give you another precision to help you help me ? – asderad Apr 12 '22 at 13:59
  • @jdobres, and thank you too. I've seen the reorder command suggested but am unsure how to implement it into my ggplot. In the "reorder(Position,Position, function(x)-length(x)" structure what do the individual terms correspond to in my case ? – asderad Apr 12 '22 at 14:01
  • You could share a sample of your data using head(dput(), 20)? – Quinten Apr 12 '22 at 14:48
  • @Quinten; There you go, a sample of the initial dataframe and the dataframe used to create the ggplot (knowing that I've followed this tutorial in order to adapt results of a mixed model : https://www.r-bloggers.com/2014/08/plotting-mixed-effects-model-results-with-effects-package/) – asderad Apr 12 '22 at 15:50

1 Answers1

0

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

jdobres
  • 11,339
  • 1
  • 17
  • 37
  • You sir are a godsend, my deepest gratitude for your insight; That did the trick perfectly. I completely overlooked the fact that I have to change the order for the dataframe that's actually specified in the ggplot - I falsely assumed that changing the order in the initial dataframe was sufficient. Thanks a million and have a very nice evening ! – asderad Apr 12 '22 at 18:20