0

I created the violin plot using below code, But on the x-axis, Models name are not in order as in the data set (i.e. "Observed", "SVM", "Grid_SVM", "MARS", "Grid_Mars", "RF", "Grid_RF"). Where the edit is required in the code?

ggplot(df1, aes(x = Model, y = Pb), cex.lab = 18, cex.axis=18) +
  geom_violin(trim=FALSE, fill = "palegreen") +
  geom_boxplot(width = .12, fill = "orange", outlier.color = "orange", outlier.size = 2) + 
  labs(title = "Pb Adsorption distribution by Models over testing phase") + xlab("Models") + ylab("Pb Adsorption distribution")+ theme_classic() +
  theme(
  plot.title = element_text(size = 12, colour = "black", face = "bold"), #for Main Title
  axis.title.x = element_text(size = 12, colour = "black", face = "bold"), #for axix title
  axis.title.y = element_text(size = 12, colour = "black", face = "bold"),
  axis.text.x = element_text(face="bold", color="darkblue", size=12, angle=0), #for axis tick
  axis.text.y = element_text(face="bold", color="black", size=12, angle=0))

1 Answers1

2

By default, ggplot2 plots character vectors in alphabetical order. To place a specified order to your plot, simply use dplyr and create the column as a factor() and specify the levels you desire. Then ggplot2 should plot it as you want.

Edit #1 One way is to modify the df1 data frame separately from your string of ggplot2 commands. You can do this as

df1 <- df1 %>%
  mutate( Model=factor(Model,levels=c("Observed", "SVM", "Grid_SVM", "MARS", "Grid_Mars", "RF", "Grid_RF")) )

And then call your string of ggplot2 commands as you have posted above.

Edit #2 (from comments below)

If you want to pipe everything through and do everything in one shot, try

df1 %>%
  mutate( Model=factor(Model,levels=c("Observed", "SVM", "Grid_SVM", "MARS", "Grid_Mars", "RF", "Grid_RF")) ) %>%
  ggplot( aes(x = Model, y = Pb), cex.lab = 18, cex.axis=18) +
  geom_violin(trim=FALSE, fill = "palegreen") +
  geom_boxplot(width = .12, fill = "orange", outlier.color = "orange", outlier.size = 2) + 
  labs(title = "Pb Adsorption distribution by Models over testing phase") + xlab("Models") + ylab("Pb Adsorption distribution")+ theme_classic() +
  theme(
  plot.title = element_text(size = 12, colour = "black", face = "bold"), #for Main Title
  axis.title.x = element_text(size = 12, colour = "black", face = "bold"), #for axix title
  axis.title.y = element_text(size = 12, colour = "black", face = "bold"),
  axis.text.x = element_text(face="bold", color="darkblue", size=12, angle=0), #for axis tick
  axis.text.y = element_text(face="bold", color="black", size=12, angle=0))

The original suggestion was to overwrite the data. Then generate the plot in a new chain.

df1 <- df1 %>%
  mutate( Model=factor(Model,levels=c("Observed", "SVM", "Grid_SVM", "MARS", "Grid_Mars", "RF", "Grid_RF")) )

ggplot( df1, aes(x = Model, y = Pb), cex.lab = 18, cex.axis=18) +
  geom_violin(trim=FALSE, fill = "palegreen") +
  geom_boxplot(width = .12, fill = "orange", outlier.color = "orange", outlier.size = 2) + 
  labs(title = "Pb Adsorption distribution by Models over testing phase") + xlab("Models") + ylab("Pb Adsorption distribution")+ theme_classic() +
  theme(
  plot.title = element_text(size = 12, colour = "black", face = "bold"), #for Main Title
  axis.title.x = element_text(size = 12, colour = "black", face = "bold"), #for axix title
  axis.title.y = element_text(size = 12, colour = "black", face = "bold"),
  axis.text.x = element_text(face="bold", color="darkblue", size=12, angle=0), #for axis tick
  axis.text.y = element_text(face="bold", color="black", size=12, angle=0))

Either of the above will work.

statstew
  • 301
  • 1
  • 6
  • 1
    There is also an `order` aesthetic with ggplot2. I have never used it, but you may want to try that as well. – statstew May 01 '20 at 11:59
  • thanks for the prompt response, for the first answer, how to perform, kindly describe or give a clue – Suraj Kumar Bhagat May 01 '20 at 12:05
  • I got the when used the latest suggestion: Error: Mapping should be created with `aes() or `aes_()`. Run `rlang::last_error()` to see where the error occurred. – Suraj Kumar Bhagat May 01 '20 at 12:35
  • First three line: df1 <- df1 %>% mutate(Model=factor(Model,levels=c("Observed", "SVM", "Grid_SVM", "MARS", "Grid_Mars", "RF", "Grid_RF"))) %>% ggplot(df1, aes(x = Model, y = Pb)) – Suraj Kumar Bhagat May 01 '20 at 12:37
  • Wow, thank you so much, I used the original suggestion and then call ggplot, this work perfectly. However, I will keep the second suggestion as well thank you so much. – Suraj Kumar Bhagat May 01 '20 at 12:59