-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Split geom_hline into geom and annotation (fixes #426) #482
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
Conversation
It also looks like there's a lot of code in the Also, stat_abline gives default values for slope and intercept. Is that necessary? |
The code for the line stats is a bit simpler with the duplicate stuff removed. It might be a good idea to print a message when using a geom with fixed intercept values, though, because it's easy to do accidentally. I also removed the default values for Here's some code that tests what should be expected when the duplicate line prevention code is removed: p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p + geom_abline(aes(intercept = mpg, slope = mpg/5), alpha = .2) # Lots of lines
p + geom_abline(intercept = 20, slope = 1, alpha = .2) # Should be dark
p + geom_abline(aes(intercept = 20, slope = 1), alpha = .2) # Should be dark
p + annotate_abline(intercept = 20, slope = 1, alpha = .2) # Should be light
p + geom_vline(aes(xintercept = wt), alpha=.2) # Lines through each point
p + geom_vline(xintercept = 3, alpha=.2) # Should be dark
p + geom_vline(aes(xintercept = 3), alpha=.2) # Should be dark
p + annotate_vline(xintercept = 3, alpha=.2) # Should be light
p + geom_hline(aes(yintercept = mpg), alpha=.2) # Lines through each point
p + geom_hline(yintercept = 20, alpha=.2) # Should be dark
p + geom_hline(aes(yintercept = 20), alpha=.2) # Should be dark
p + annotate_hline(yintercept = 20, alpha=.2) # Should be light
|
Looks good! I wonder now if the stat functions could be removed altogether and just moved into the geoms? I'm trying to remember why I had separate stat functions - it's probably something to do with either correctly setting scale limits, or with non-Cartesian coordinates. If you have tests for those and they work fine, I think we could go ahead and remove the stat functions. |
And we might not want to include this in 0.9.1 since it breaks backward compatability |
@@ -0,0 +1,41 @@ | |||
#' @rdname geom_hline | |||
#' @export | |||
annotate_hline <- function(yintercept = NULL, ...) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To reduce duplication (esp. with annotate
) maybe the the layer construction should be extracted out into its own function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or should annotate have special cases for vline
, hline,
and abline
so that you can just use annotate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking that one way to go would be to simply call annotation()
, but I think it would require some modification. Is there a reason why annotation()
looks specifically for x, xmin, and xmax, but not other values, like xintercept?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I probably just forgot xintercept
and yintercept
- the other problem is that they don't currently work with stat_identity
, but maybe that can go away as per my other comment. The reason that position aesthetics are treated differently is that they should be scaled (i.e. you specify position in data coordinates), but all other aesthetics should be treated as set parameter values (e.g. colour = "red"
shouldn't be scaled).
That said, there were a couple of bug reports on the mailing list about annotations not working with various scales.
Regarding removing the stat: it apparently also can work if passed a function for |
I now think that should be removed - it's not elegant, and it's not inline with the way that the rest of ggplot2 works. |
OK, I removed the stat and the examples I posted above still work. The code is much simpler now. What remains to (possibly) be done:
|
Another thought: as long as we're breaking compatibility, all these geoms could probably be collapsed into a single one, like It could take |
I deliberately made it three separate functions because I think it makes more sense to keep behaviours separate, especially when you start having combinations of arguments that don't make sense. |
ranges <- coord_range(coordinates, scales) | ||
|
||
if (!is.null(yintercept)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer data$y <- yintercept %||% data$yintercept
So a horizontal line would be specified with |
@BrianDiggs I was thinking of having Come to think of it, you can always specify a line with two of the three: Here's what combinations would be valid:
|
Yes, this is the way that |
I've consolidated the docs into There's one inconsistency between # Works
p + annotate("hline", yintercept = seq(10, 30, by = 5))
p + annotate("vline", xintercept = 1:5)
# Doesn't work
p + annotate("abline", intercept = c(17, 22), slope = c(0.5, 1)) I think this is because In general, I like the idea of being able to pass vectors as aesthetic properties using |
Oh hmmm, good catch. The problem in general is that putting other aesthetics in the data frame will cause them to be mapped - maybe we need some way to turn that off? (But that seems like it would add a lot of complexity for little gain) |
Could we just |
But how would you stop them from being mapped? i.e. if you do |
The bug with |
Regarding stopping vars from being mapped, I can't see a way to avoid it right now. What if there were an option to pass a "raw" (unmapped) data frame to |
I think that's something I explored a bit in the layers repo - lets not worry about it for now. |
Could you please rebase/merge against master, re-document with the development version of roxygen2 ( |
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
This fixes #426. As discussed, it splits
geom_hline
into a geom and annotation.If this looks like the right approach, I can do the same for vline and abline. If so, should I put
annotate_hline
,annotate_vline
,annotate_abline
all in one file since they're quite simple? Or they could be added to their respectivegeom-Xline.r
files.Also, some of the visual tests will have to be updated.