1

I've made a series of scatter plots and put them together using 'facet.by', so they are grouped by distance. However, I'd like them to appear in the order 'Margin', '10 m', '50 m', instead of the order that they are in currently (see attached photo). Is there any way I can change the order of the faceted graphs?

ggscatter(floral_data, x = "Spp Flowering", y = "Parasitoids", facet.by = "Distance",
          add = "reg.line",
          conf.int = TRUE,
          cor.coef = TRUE, # Add correlation coefficient
          cor.coeff.args = list(method = "spearman", label.y = 37, label.x = 5.7, label.sep = "\n"),
          xlab = "Average number of flowering species in margin (May-June)",
          ylab = "Parasitoid Abundance") 

1

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Bethany
  • 11
  • 3
  • 1
    Welcome to SO. Sorting in R/ ggplot2 always follows the same idea - usually alphabetically, except if it is a factor with levels in specified order. How to do that, see for example https://stackoverflow.com/questions/12774210/how-do-you-specifically-order-ggplot2-x-axis-instead-of-alphabetical-order/48998414#48998414 – tjebo Oct 21 '21 at 10:04
  • 1
    Does this answer your question? [Fixing the order of facets in ggplot](https://stackoverflow.com/questions/14262497/fixing-the-order-of-facets-in-ggplot) – tjebo Oct 21 '21 at 10:06

1 Answers1

0

I'll expand on what tjebo has correctly suggested with a quick example. Ill use the example data 'mtcars' that is included in R. To get this I do:

df <- datasets::mtcars

Take a look at the data frame so you understand the data. Here Ill plot the data faceting by the 'gear' column, but then turn the gear column into a factor with a specified order given by levels. The resulting facets are reordered by this level order when I plot again.

# Plot initially
ggscatter(df, x = "mpg", y = "cyl",facet.by = "gear")

# Turn the gear column from a numeric in a factor with a certain order
df$gear <- factor(df$gear,levels = c("5","3","4"))

# plot again
ggscatter(df, x = "mpg", y = "cyl",facet.by = "gear")

enter image description here

enter image description here

Dharman
  • 30,962
  • 25
  • 85
  • 135
Henry Holm
  • 495
  • 3
  • 13