0

How can I manually change the order of the facets while using the function gather? The figures in the grid are not presented in the order I defined in myvars.

Here's the code:

myvars <- c("1","2","3","4","5"
            ,"6","7","8","9","10",
            "11","12","13","14")
DataVars<- Data[myvars]

DataVars%>%
  gather(-1, key = "var", value = "value") %>% 
  ggplot(aes(x = value, y = 1)) +
  facet_wrap(~ var, scales = "free") +
  geom_jitter(size = 0.5, shape = 16, aes(colour = var),width = 0.5, height = 0.5) +
  geom_smooth(method="lm", size = 1, color = "black", level=0.95)

Thanks in advance!

AmitKai
  • 1
  • 2

1 Answers1

0

To use the accepted anser to Fixing the order of facets in ggplot in a pipe, you need the mutate verb:

DataVars%>%
  gather(-1, key = "var", value = "value") %>% 
  mutate(var = factor(var, levels = ...) %>% 
  ggplot(aes(x = value, y = 1)) +
  facet_wrap(~ var, scales = "free") +
  geom_jitter(size = 0.5, shape = 16, aes(colour = var),width = 0.5, height = 0.5) +
  geom_smooth(method="lm", size = 1, color = "black", level=0.95)

In the above example, replace the ... with a vector containing the desired order of the facet labels.

Ben Norris
  • 5,226
  • 2
  • 7
  • 14