0
votes

I would like to order a ggplot on Rstudio based on the highest value of two columns (Ex. M1 and M2 below). In the example below, the correct order would be Chemical C, B, A, E, D.

Name    Parent  M1  M2 
Chemical A  0.088   0.237   0.310
Chemical B  0.004   0.315   0.238
Chemical C  0.026   0.387   0.285
Chemical D  0.015   0.235   0.272
Chemical E  0.086   0.288   0.272

The code I have been using is:

ggplot(datafile, aes (reorder (x=Name, M1), M1)) + geom_point(aes (y= Parent, color = "black")) + geom_point(aes(y=M1, color = "blue")) + geom_point(aes(y=M2, color = "orange"))

This code successfully orders my data based on M1 values only, but I would like it to order it based on both M1 and M2. How can I modify this code to achieve that?

Thank you!

1
Replace reorder(x = Name, M1) with reorder(x = Name, pmax(M1, M2)).Gregor Thomas

1 Answers

1
votes

Assuming from your question that you want an ordered plot without editing the underlying data.frame itself, you can produce such a plot with the following code:

ggplot(datafile[order(-pmax(datafile$M1, datafile$M2)),], aes (x=factor(Name, levels=Name), M1)) +  
  geom_point(aes (y= Parent), color = "black") + 
  geom_point(aes(y=M1), color = "blue") + 
  geom_point(aes(y=M2), color = "orange")

enter image description here

datafile[order(-pmax(datafile$M1, datafile$M2)),] supplies your data.frame ordered by the maximum value across both M1 and M2.

aes(factor(x=Name, levels=Name), M1) tells R to plot the chemical names in the order they are presented as a result of the reordered datafile.