Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for mapping the width and height values #25

Open
huftis opened this issue Sep 26, 2020 · 0 comments
Open

Support for mapping the width and height values #25

huftis opened this issue Sep 26, 2020 · 0 comments

Comments

@huftis
Copy link

huftis commented Sep 26, 2020

Thank you for this extremely useful package!

I have a feature request – support for mapping the width and height values, not just setting them. This is needed when one uses geom_tile() instead of geom_rect() to draw the boxes that the geom_fit_text() text is supposed to be placed into. And geom_tile() is needed when one of the axis is discrete, i.e. a factor. Here’s a small reproducible example.

library(tidyverse)
library(ggfittext)
d = tibble(
  cat = factor(c("R1", "R2")),
  boxstart = c(3, 4),
  boxend = c(5, 10),
  boxwidth = boxend - boxstart,
  boxtext = c(strrep("R1 ", 150), strrep("R2 ", 150))
)

# Plotting boxes using geom_rect()
const_boxheight = .75
p_rect = ggplot(d, aes(
    x = (boxstart + boxend) / 2,
    y = cat,
    xmin = boxstart,
    xmax = boxend,
    ymin = as.numeric(cat) - const_boxheight / 2,
    ymax = as.numeric(cat) + const_boxheight / 2,
    label = boxtext)) +
  geom_rect() +
  geom_fit_text(reflow = TRUE, colour = "orange")
p_rect

This looks fine. But using as.numeric() on the cat factor inside aes() is an ugly hack, which causes problems later. A better solution is to use geom_tile():

# Plotting boxes using geom_tile()
d$boxheight = const_boxheight
p_tile = ggplot(d, aes(
    x = (boxstart + boxend) / 2,
    y = cat,
    width = boxwidth,
    height = boxheight,
    label = boxtext)) +
  geom_tile(height = const_boxheight) +
  geom_fit_text(reflow = TRUE, colour = "orange")
p_tile

The boxes are laid out the same, but the width and height aesthetics are not recognised by geom_fit_text(). Of course, I can set the width and height, but then it would be the same for both (all) boxes, so there would either be overflowing text or too much space inside the boxes.

Now for the reason I need to use geom_tile() instead of geom_rect(). The as.numeric() hack doesn’t work when one uses faceting with free scales and not all levels of the factor are present in all panels. Here’s a simple example:

p_rect + facet_wrap(~cat, scales = "free_y")

Note the wrong vertical placement of the box in panel R2 / the wrong y-axis labelling.

When one uses geom_tile() instead, there’s no problem, since the internals of ggplot2 handle the vertical placement and takes care of any missing levels:

# Correct placement and y axis
p_tile + facet_wrap(~cat, scales = "free_y")

But then geom_fit_text() doesn’t work properly, as it doesn’t recognise the width and height aesthestics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants