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

Annotations work only if global aes() have been defined #1655

Closed
bleutner opened this issue Jun 24, 2016 · 3 comments · Fixed by #1690
Closed

Annotations work only if global aes() have been defined #1655

bleutner opened this issue Jun 24, 2016 · 3 comments · Fixed by #1690
Assignees
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@bleutner
Copy link

bleutner commented Jun 24, 2016

Hi,

adding an annotation works only if global aesthetics have been defined in ggplot().
If there are only "local" aesthetics in individual geoms the annotation will not be drawn (no warning though).

library(ggplot2)
library(grid)
df <- data.frame(x = 1:10, y= 1:10) 
anno <- annotation_custom(grid.rect(draw=FALSE), xmin = 7, xmax = 10,ymin = 1, ymax = 2)

## Global aesthetics: works fine
ggplot(df, aes(x = x, y = y)) + geom_point()+ 
        anno

## "Local" aesthetics: does not work
ggplot() + geom_point(data = df, aes(x = x, y = y))+
        anno 

selection_065

selection_066

I tried setting scales and/or coord limits explicitly, but this doesn't make it work either:

ggplot() + geom_point(data = df, aes(x = x, y = y))+
        anno +
        scale_x_continuous(limits = c(-10,10)) +
        scale_y_continuous(limits = c(-10,10)) +
        coord_equal(xlim = c(-20,20), ylim = c(-20,20))
@hadley
Copy link
Member

hadley commented Jul 28, 2016

The problem is that annotation currently sets data = NULL and clearly there's a bug somewhere because this causes draw_panel() to never be rendered.

Minimal reprex:

library(ggplot2)
df <- data.frame(x = 1:10, y = 1:10) 
ggplot() + 
  geom_point(aes(x, y), data = df) + 
  annotation_custom(grid::rectGrob(), xmin = 7, xmax = 10, ymin = 1, ymax = 2)

@hadley hadley added bug an unexpected problem or unintended behavior ready labels Jul 28, 2016
@hadley hadley added this to the v2.2.0 milestone Jul 28, 2016
@thomasp85
Copy link
Member

This is due to Layer checking for empty data before calling draw_layer. I'll add a switch to Layer to allow drawing with no data

@thomasp85
Copy link
Member

This is being fixed in #1690

thomasp85 added a commit to thomasp85/ggplot2 that referenced this issue Aug 3, 2016
Add to news

Fix inherit.aes so dummy data is not called out

Define and use dummy_data() to assign data to annotation layers

Add tests for dummy data and inherit.aes
@hadley hadley removed the in progress label Aug 3, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants