1

I am using a loop to perform faceting with ggplot and facet_wrap; I used stat_smooth and stat_cor for statistical analysis and I need to visualise the R coefficient (spearman coefficient) on the plot. In the resulting facets, I would like to have the panels ordered based on those statistical results I am calculating in the plot function itself (from the highest to the lowest R coefficient). e.g.: in facet a: I need to turn panel 1: R = 0.1, panel 2: R= 0.01; panel 3: R= 2 (from left to right on the plot) into panel 3, panel 1, panel 2 (as panel 3's the highest R coefficient resulted from the test and panel 2 the lowest). N.B.: the table I am using for faceting doesn't contain these stats data, I am calculating it in the ggplot function itself in the loop.

Thank you!

Something like this but adding the correlation coefficient as a label and ordering the panels by increasing value of the correlation coefficient:

library(ggplot2)

ggplot(mtcars, aes(disp, mpg)) +
  geom_point() +
  geom_smooth(method = "lm", formula = y ~ x) +
  facet_wrap(~cyl)

Created on 2021-10-19 by the reprex package (v2.0.1)

Pedro J. Aphalo
  • 5,796
  • 1
  • 22
  • 23
Beth
  • 99
  • 1
  • 1
  • 3
  • 2
    Put the data you want to facet by in the data itself. Make it a `factor` data type with the levels in the order you wan the facets to appear. If you need more help, please share a [minimal reproducible example](https://stackoverflow.com/q/5963269/903061). – Gregor Thomas Oct 18 '21 at 17:46
  • 1
    The easiest way to share a reproducible example is to use built-in data, such as `mtcars`. If that doesn't work in your case, please either share code to simulate a little bit of fake data or use `dput()` on an illustrative sample of your real data and share that to give us a copy/pasteable version of data to work on. Also please share minimal code to demonstrate the issue on the shared data. For more help on making reproducible examples in R, see the link in my first comment. – Gregor Thomas Oct 18 '21 at 17:50
  • A reproducible code example would help. As suggested you can use `mtcars`, just a plot with facets and the smooth lines is enough for us to build upon. The tricky part is the reordering of the panels within the ggplot code, as @GregorThomas suggests above, precomputation of R and creating a factor is likely the simplest approach. You can use `reorder()` on the factor to set the order of the levels based on the R coefficient values. – Pedro J. Aphalo Oct 18 '21 at 20:53
  • @Beth Hi! You are new here. Your question is an interesting one and it is worthwhile to answer it. A reprex is most important for errors and specific problems. You are asking how to do a certain type of plot. In this case sometimes a figure done in a graphic editor is added. Your explanation is clear, and no such drawing is needed. Code enough to be used as a starting point for an answer would make it easier to write an answer. – Pedro J. Aphalo Oct 18 '21 at 21:06
  • Thank you very much, @PedroJ.Aphalo – Beth Oct 25 '21 at 11:12
  • I'll manage to make a reproducible example and share it here – Beth Oct 25 '21 at 11:14

0 Answers0