slowkow / ggrepel

:round_pushpin: Repel overlapping text labels away from each other in your ggplot2 figures.
https://ggrepel.slowkow.com
GNU General Public License v3.0
1.21k stars 95 forks source link

Feature request / offer: `element_text_repel()` #248

Open teunbrand opened 8 months ago

teunbrand commented 8 months ago

Hi Kamil and others,

I have been musing for a while that it'd be great if we could have axis text repelled. This could be great for e.g. heatmaps that highlight only some rows or columns. For example, in the plot below there is ample spacing for 'Texas', 'New York' and 'California' but the states starting with 'M' are a little bit cramped.

library(ggplot2)

df <- reshape2::melt(as.matrix(USArrests))

ggplot(df, aes(Var2, Var1, fill = value)) +
  geom_tile() +
  scale_y_discrete(
    breaks = c("Missouri", "Mississippi", "Minnesota", "Michigan", 
               "Massachusetts", "New York", "California", "Texas")
  )

Created on 2024-01-13 with reprex v2.0.2

I'm reasonably confident that it should be possible to capture that axis repelling into an element_text_repel() theme setting that you can feed to the axis text elements. If this is something you think might be a good fit for {ggrepel}, I'll be happy to prepare a PR and discuss further. Thanks for reading!