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
Closed

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

bleutner opened this issue Jun 24, 2016 · 3 comments
Assignees
Labels
Milestone

Comments

@bleutner
Copy link

@bleutner 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 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 this to the v2.2.0 milestone Jul 28, 2016
@thomasp85
Copy link
Member

@thomasp85 thomasp85 commented Aug 1, 2016

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

@thomasp85 thomasp85 commented Aug 1, 2016

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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants