Coder Social home page Coder Social logo

Comments (4)

wilkox avatar wilkox commented on May 29, 2024

Thanks for reporting this Alastair! The error happens because when geom_fit_text() is called with only a y aesthetic (as opposed to either a y aesthetic plus a height argument, or ymin/ymax aesthetics), it automatically guesses upper and lower limits for the box based on the data. If these limits fall outside of the limits set by ylim() (as in your second and third examples), it results in the error you saw.

I think the best way to handle this would be to not draw any labels when the bounding box falls outside explicitly set plot limits, perhaps emitting a warning that they had been skipped. However, I'm curious about the use case explored in your examples – specifically, a continuous variable on the y axis but no explicit ymin/ymax or height, and y limits fixed with ylim(). It seems like plain old geom_text() would be better for this kind of situation:

library(tidyverse)
z = data.frame(x = letters[1:5], y = 0:4, lb = 3)
z %>%
  ggplot(aes(x = x, y = y, label = lb)) + 
  geom_bar(stat = "identity", position = "dodge") + 
  ylim(-0.1, 6) + 
  geom_text()

Created on 2019-07-21 by the reprex package (v0.3.0)

If I changed geom_fit_text() to skip drawing the text labels as I described above, would that work for your use case?

from ggfittext.

alastairrushworth avatar alastairrushworth commented on May 29, 2024

Thanks for the quick response! I think my use case is fairly simple here - so I might have misunderstood the gg_fit_text() parameters. My not specifying ymin/ymax or height is really convenience - I like that I can just let gg_fit_text() guess from x and y what to do. Is there a way these arguments could be added to the example with ylim() that avoids the error?

This isn't a huge deal for me and I don't necessarily think anything needs to be fixed - and as you say, maybe geom_text() is the way to go in the case, though I'd really like to keep the text scaling if possible. Perhaps a more general argument for your suggestion to skip the labels with a warning is that I think if the main ggplot() part works then maybe we should not expect the annotation geom to cause an error.

Thanks again, it's much appreciated.

from ggfittext.

wilkox avatar wilkox commented on May 29, 2024

You're definitely right that if ggplot() accepts the data and aesthetics then geom_fit_text() shouldn't stop with an error. I've just pushed a commit to the development version (df19c27) that fixes this and emits a warning instead.

I can't really think of an ideal behaviour that would work for your use case though. Any automatic method that tries to guess a more appropriate set of y limits for the box could still end up generating limits outside the plot limits, since ylim() by design can chew into the available plot area regardless of whether there is data there or not. I guess in defence of the 'skip-and-warn' behaviour is that this is also how ggplot2 treats geom_bar() if it falls outside the limits, so at least the plot is consistent:

library(tidyverse)
library(ggfittext)
z = data.frame(x = letters[1:5], y = 0:4, lb = 3)

# this works
z %>%
  ggplot(aes(x = x, y = y, label = lb)) + 
  geom_bar(stat = "identity", position = "dodge") + 
  ylim(1, 8) + 
  geom_fit_text()
#> Warning: Removed 5 rows containing missing values (geom_bar).
#> Warning: Removed 2 rows where box limits were outside plot limits
#> (geom_fit_text)

Created on 2019-07-21 by the reprex package (v0.3.0)

from ggfittext.

alastairrushworth avatar alastairrushworth commented on May 29, 2024

That makes total sense. I like the warn and skip approach - that does help with some of the things I was doing. I need to think a bit harder about my own application but I don't think it's a ggfittext issue. Thanks again for looking into this!

from ggfittext.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.