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

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

Closed
smouksassi opened this issue Mar 13, 2019 · 4 comments

Comments

@smouksassi
Copy link

@smouksassi smouksassi commented Mar 13, 2019

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)

@yutannihilation
Copy link
Member

@yutannihilation yutannihilation commented Mar 14, 2019

Here's a simpler reprex. This seems a possible bug (?) of guessing the type of scale. For a workaround, you can manually add scale_x_discrete() to explicitly specify the type of the X scale.

library(ggplot2)

d <- data.frame(x = letters[1:10], y = 1:10, stringsAsFactors = FALSE)

ggplot() +
  geom_point(aes(x, y), d) +
  geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
            data.frame(xmin = -Inf, xmax = Inf, ymin = 4, ymax = 6))

ggplot() +
  geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
            data.frame(xmin = -Inf, xmax = Inf, ymin = 4, ymax = 6)) +
  geom_point(aes(x, y), d)
#> Error: Discrete value supplied to continuous scale

ggplot() +
  geom_rect(aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax),
            data.frame(xmin = -Inf, xmax = Inf, ymin = 4, ymax = 6)) +
  geom_point(aes(x, y), d) +
  scale_x_discrete()

Created on 2019-03-14 by the reprex package (v0.2.1.9000)

@smouksassi
Copy link
Author

@smouksassi smouksassi commented Mar 14, 2019

I am using annotation as first layer in order for it to be first i.e. in the bacground, repeated across facets and to avoid affecting scales.
here is few examples.
p1 workaround with geom_blank make scales reflect original data ( can be desirable in some settings)
p2 works as expected after adding scale_x_discrete() ( thanks @yutannihilation )
p3 explicitly stating xmin = "Fair" and xmax = "Ideal" make the scale ignore the order of the cut (unexepected output)
p4 uses numerical values 0/6 but this is affecting the scales maybe due to latest annotation modifications

library(ggplot2)
library(egg)
#> Loading required package: gridExtra

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

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


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

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

egg::ggarrange(p1,p2,p3,p4,ncol=2)

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

@yutannihilation
Copy link
Member

@yutannihilation yutannihilation commented Mar 14, 2019

to avoid affecting scales.

For this purpose, I recommend using annotation_raster().

library(ggplot2)

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

Created on 2019-03-14 by the reprex package (v0.2.1.9000)

@lock
Copy link

@lock lock bot commented Sep 16, 2019

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/

@lock lock bot locked and limited conversation to collaborators Sep 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants