0

I have a data frame like so:

Dataframe:

 df<- data.frame(station= as.factor(rep("a", 12)), 
            month= as.character(c("Apr", "Aug", "Dec", "Feb", "Jan", "Jul",
                                                                  "Jun", "Mar", "May", "Nov", "Oct", "Sep")), 
            mean= rnorm(12, 300, 10), sd= rnorm(12, 300, 1), season= c(rep("wet", 6), rep("dry", 6)))

I want to reorder the months in consecutive order so that when I input into a ggplot function data for each month occurs in the calendar order.

Failed attempt to order:

df$month<-factor(df$month, levels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))

I have previously got this to work and used the following code to get the subsequent output, however I erased something along the way and believe the source of error is with improperly ordering:

Previous outputs:

p<
  ggplot(df, aes(x = month, y=mean, fill=season)) + 
  geom_bar(stat="identity", color="black", 
       position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=.2,
             position=position_dodge(.9)) 
 p +labs(x="Month", y = "Precipitation (mm)") +
  theme_bw()+
  theme(legend.title = element_blank(),legend.text=element_text(size=15),legend.position="top",title= element_text(size=20), axis.text= element_text(size=10, face = "bold"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
   panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  coord_flip()

enter image description here

I want to ultimately produce this figure again (note: for simplification, my example code only includes two seasons rather than the three in the image).

Danielle
  • 785
  • 7
  • 15
  • 1
    Is it that the problem that the order now goes from bottom to top instead of top to bottom? In that case, you could add `scale_x_discrete(limits = rev(levels(df$month)))` as shown [here](https://stackoverflow.com/questions/34227967/reversed-order-after-coord-flip-in-r). Otherwise your code works for me to put things in order by date (from bottom to top of plot, though). – aosmith Nov 08 '18 at 19:16
  • FYI, you can use the built-in variable `month.abb` instead of typing out `"Jan", "Feb", ...`. – Gregor Thomas Nov 08 '18 at 21:11

1 Answers1

0

You were almost there. Something like this should work :

df<- data.frame(station= as.factor(rep("a", 12)), 
                month= as.character(c("Apr", "Aug", "Dec", "Feb", "Jan", "Jul",
                                      "Jun", "Mar", "May", "Nov", "Oct", "Sep")), 
                mean= rnorm(12, 300, 10), se= rnorm(12, 10, 1), season= c(rep("wet", 6), rep("dry", 6)))



mon <- rev(c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))

p<- ggplot(df, aes(x = factor(month, levels= mon, labels = mon), y=mean, fill=season)) + 
  geom_bar(stat="identity", color="black", position=position_dodge()) +
  geom_errorbar(aes(ymin=mean-se, ymax=mean+se), width=.2, position=position_dodge(.9)) 

p +labs(x="Month", y = "Precipitation (mm)") +
  theme_bw()+
  theme(legend.title = element_blank(),legend.text=element_text(size=15),legend.position="top",title= element_text(size=20), axis.text= element_text(size=10, face = "bold"), panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  coord_flip()

enter image description here

I am not sure why the behaviour is different if you only use level instead of level + label. Anyone?

Jrakru56
  • 1,211
  • 9
  • 16
  • Thank you, this did order them properly. Only issue now is, if I choose to use `coord_flip() the top starts as Dec and bottom goes to Jan. Any idea how to order in such a way so that the topmost value is Jan an it sequences to Dec which would become the bottommost value? – Danielle Nov 08 '18 at 19:16
  • I am not sure of the proper way but a quick fix would be reverse the list i.e `mon2 <- rev(mon)` – Jrakru56 Nov 08 '18 at 19:21
  • Aha! The proper way is to use `scale_x_reverse()` – Jrakru56 Nov 08 '18 at 19:23
  • @Jrakru56 if `scale_x_reverse()` works update your answer to include that . – Mike Nov 08 '18 at 20:48
  • Hmm. It seems that `scale_x_reverse` is not the proper way. It does not handle discrete cases. So the quick fix way it is then ... – Jrakru56 Nov 08 '18 at 21:05