Break ggplot2 into Windows/facets with labels in alphabetical order

Viewed 36

I want to generate a point plot which shows equal number of rows in each frame (can be facets if nothing else works) & in alphabetical order (A1,A2,B2,B2 etc.) since the plot length is too high to see the axis labels clearly. I want to break this plot into 4 windows with the same number of rows i.e. 13 each. (preferably tidyverse & not hard coded # of rows)

library(tidyverse)
df <- data.frame(names=c(paste0(LETTERS,1),paste0(LETTERS,2)),value=1:52)
  
df %>% 
  arrange(desc(names)) %>% 
  ggplot(aes(y=names,x=value))+
  geom_point()+
  scale_y_discrete(limits=rev)
1 Answers

We can create a grouping column with gl and use facet_wrap

library(dplyr)
library(ggplot2)
df %>% 
   arrange(desc(names)) %>% 
   mutate(grp = as.integer(gl(n(), ceiling(n()/4), n()))) %>%  
   ggplot(aes(y=names,x=value))+
    geom_point() +
    facet_wrap(~ grp, scales = 'free_y')

-output

enter image description here

Related