Search code examples
rggplot2data.tableaxes

ggplot2: how to fix a "squashed" y-axis of categorical variables?


I'm trying to increase the spacing of the labels on the y-axis in this barplot:

library(data.table)    
ggplot(data, aes(y=values, x=categories)) + 
             geom_bar(stats="identity") +coord_flip()

enter image description here

The problem of course is that there are ~1500 y-axis categorical labels. At the moment, these are so squashed together one cannot see any trends in the data.

How can I increase spacing between these y-axis labels? Could I make the plot larger vertically?

Naturally one could decrease the text size or decrease the width of the bars, but this only works to a point...

I have tried the following solution with ?discrete_scale:

ggplot(data, aes(y=values, x=categories))
           +geom_bar(stats="identity") +coord_flip() + scale_x_discrete(expand = c(0,0.01))

However, fiddling with expand doesn't appear to actually create space between these labels.


Solution

  • There isn't really a trick answer here. If you have an 8 inch tall image, and 1500 rows of text, the text is either going to be heavily overlapped or tiny. Either way it will not be readable. One option is to just turn off the y labels. theme(axis.ticks.y = element_blank(), axis.text.y = element_blank()). Note you may need to change those to axis.ticks.x and axis.text.x depending on the coord_flip. Another option is to sample your dataset. Instead of plotting everything, just plot a randomly selected 100 rows. ggplot(data[sample(dim(data)[1], 100)], aes(....

    If you really want every label, and every row of data, the third option is to just make a very tall image.

    pdf(file = "image.pdf", width = 8, height = 120)
    g <- ggplot(data, aes(y = values, x = categories)) + 
      geom_bar(stats="identity") + 
      coord_flip()
    print(g)
    dev.off()