Re-ordering facet_wrap

I want to change order of this facet_wrap (see below).

image

Instead of 'Midtre Ost', 'Osterbotn', 'Porsangnes East' (from left to right), I would like 'Porsanges East, Midtre Ost, Osterbotn'

Is there a way to do this without modifying my data frame?

labels <- c('por' = "Porsangnes East", 'mid' = "Midtre Ost", 'ost' = "Osterbotn")

ggplot(data, aes(x = depth, y = temp, colour = month))+
theme_bw() +
geom_line(size=1, linetype=1) +
labs(x = "Depth/ m", y = "Temperature/ °C", colour = "Month") +
scale_colour_manual(values = c("#FF6666", "#FFCC00", "#66CCFF", "#00CC33"), labels=c("August", "May", "November", "October")) +
geom_point(size=0)+
scale_x_reverse()+
scale_y_continuous(position="right")+
coord_flip()+
facet_wrap(vars(location), labeller = as_labeller(labels))

You want to turn your categorical column you're faceting by, "location", into a factor, like in the below code. You may be better adding a mutate() step first to turn location into a factor rather than doing it within the facet function, however.

library(tidyverse)

# original order
palmerpenguins::penguins %>% 
  ggplot(aes(x = body_mass_g, color = island)) +
  geom_density() +
  facet_wrap(~species)
#> Warning: Removed 2 rows containing non-finite values (stat_density).

# new order
palmerpenguins::penguins %>%
  mutate() %>% 
  ggplot(aes(x = body_mass_g, color = island)) +
  geom_density() +
  facet_wrap(~factor(species, c("Chinstrap", "Adelie", "Gentoo")))
#> Warning: Removed 2 rows containing non-finite values (stat_density).

Created on 2022-07-12 by the reprex package (v2.0.1)

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.