0

I tried to create a relatively simple boxplot plot in R's ggplot2: One value on the x axis and several variables on the y axis. I'm using a code similar to this one:

 ggplot() + 
  # Boxplot 1
  geom_boxplot(df[which(df$Xvalue=="Boxplot1"),], 
               mapping = aes(X, "Y")) +
  # Boxplot 2
  geom_boxplot(df[which(df$Xvalue=="Boxplot2"),], 
               mapping = aes(X, "Y")) +
  # Boxplot 3
  geom_boxplot(df[which(df$Xvalue=="Boxplot3"),], 
               mapping = aes(X, "Y")) +

The boxplots in my real code are ordered alphabetically, however, I need them to be in a customized, categorial order.

I'm aware I could restructure my data frame so that I don't use a subset and a new geom_boxplot command for each boxplot, but I've structured the data that way for other reasons and that's not the solution I'm looking for right now.

Maybe there is an easy way using the scale_Y_manual or else? Any help is appreciated!

Sophia
  • 3
  • 1
  • 2
    Please draw the expected output. The canonical way to arrange subset groups in a plot is using factor levels e.g. `df$Xvalue <- factor(df$Xvalue, levels = c("Boxplot2", "Boxplot1", "Boxplot3")"` to put the second boxplot fist. Depending on your need, you also might want to use the R package patchwork here. – danlooo Oct 13 '22 at 09:07
  • Does this answer your question? [How to change order of boxplots when using ggplot2?](https://stackoverflow.com/questions/6867393/how-to-change-order-of-boxplots-when-using-ggplot2) – Paul Oct 13 '22 at 09:10
  • Greetings! Usually it is helpful to provide a minimally reproducible dataset for questions here so people can troubleshoot your problems. One way of doing this is by using the `dput` function. You can find out how to use it here: https://youtu.be/3EID3P1oisg – Shawn Hemelstrand Oct 14 '22 at 00:06

0 Answers0