Plotting multiple columns against one column in ggplot2

Yes, you can use pivot_longer (or gather) and facets to achieve this.

One issue is that by default the labels will not be in the order X1 – X12, so you will need to specify the factor levels.

Try this:

data %>% 
  pivot_longer(cols = 1:12) %>% 
  mutate(name = factor(name, levels = paste0("X", 1:12))) %>% 
  ggplot(aes(x, value)) + 
  geom_line() + 
  facet_wrap(~name) +
  theme_bw()

Result:

enter image description here

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top