-
Notifications
You must be signed in to change notification settings - Fork 2.1k
when using annotation as first layer with x discrete and annotation xmin/xmax = -Inf/Inf error #3184
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
Comments
Here's a simpler reprex. This seems a possible bug (?) of guessing the type of scale. For a workaround, you can manually add 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) |
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. 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) |
For this purpose, I recommend using 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) |
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/ |
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
Created on 2019-03-13 by the reprex package (v0.2.1)
The text was updated successfully, but these errors were encountered: