Skip to content

when using annotation as first layer with x discrete and annotation xmin/xmax = -Inf/Inf error  #3184

Closed
@smouksassi

Description

@smouksassi

Hi developpers !
I posted first on Rstudio Community
https://community.rstudio.com/t/understanding-when-and-why-discrete-value-supplied-to-continuous-scale-happens/26011/5
but then it was thought of as a potential bug that is maybe related to
#3120
Basically when we want to annotate a plot with discrete x scale edge to edge it errors out unless we have a fake first geom blank
when we specify the levels as xmin or xmax the order get changed
I am on ggplot2_3.1.0.9000

require(ggplot2)
#> Loading required package: ggplot2
# geom blank make the scales wider than what we want but it works

ggplot(diamonds, aes(x = cut, y = price)) +
  geom_blank()+
  annotate("rect",
           xmin = -Inf, xmax = Inf,
           ymin = 15, ymax = 15000, fill = "lightblue",
           alpha = .2
  )+
  stat_summary(data=diamonds,fun.y="median",geom="line",mapping = aes(group=1L))

ggplot(diamonds, aes(x = cut, y = price)) +
  annotate("rect",
           xmin = -Inf, xmax = Inf,
           ymin = 15, ymax = 15000, fill = "lightblue",
           alpha = .2
  )+
  stat_summary(data=diamonds,fun.y="median",geom="line",mapping = aes(group=1L))
#> Error: Discrete value supplied to continuous scale

#  cut order changed annotation does not start from the edge of plot
ggplot(diamonds, aes(x = cut, y = price)) +
  annotate("rect",
           xmin = "Fair", xmax = "Ideal",
           ymin = 15, ymax = 15000, fill = "lightblue",
           alpha = .2
  )+
  stat_summary(data=diamonds,fun.y="median",geom="line",mapping = aes(group=1L))

Created on 2019-03-13 by the reprex package (v0.2.1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions