Is it possible to extend geom_ribbon to xlimits?

I have the following code (as an example) that I would like to adapt in such a way that the tape spreads to the whole xrange, as it does geom_hline(). The tape indicates which values ​​are within acceptable limits. In my real application, sometimes there is no upper or lower bound, so the hline itself is not enough to determine if the values ​​are within the bounds.

library(ggplot2)
set.seed(2016-12-19)
dates <- seq(as.Date('2016-01-01'), as.Date('2016-12-31'), by = 1)
values <- rexp(length(dates), 1)
groups <- rpois(length(dates), 5)
temp <- data.frame(
    date = dates,
    value = values,
    group = groups,
    value.min = 0,
    value.max = 2
)
ggplot(temp, aes(date, value)) +
    geom_ribbon(aes(ymin = value.min, ymax = value.max), fill = '#00cc33', alpha = 0.6) +
    geom_hline(aes(yintercept = value.min)) +
    geom_hline(aes(yintercept = value.max)) +
    geom_point() +
    facet_wrap(~group)

I tried to set xto geom_ribbonon dates, but then only a fraction of the range is filled. Also I tried this:

geom_ribbon(aes(ymin = -Inf, ymax = 2, x = dates), data = data.frame(), fill = '#00cc33', alpha = 0.6)

but then the data is likely to be overwritten for the entire graph, and I get an error Error in eval(expr, envir, enclos) : object 'value' not found. Even if this works, the range is still actually too narrow as the xlimits expand.

+4
2

:

ggplot(temp, aes(as.numeric(date), value)) +
  geom_rect(aes(xmin=-Inf, xmax=Inf, ymin = value.min, ymax = value.max), temp[!duplicated(temp$group),], fill = '#00cc33', alpha = 0.6) + 
  geom_hline(aes(yintercept = value.min)) +
  geom_hline(aes(yintercept = value.max)) +
  geom_point() +
  scale_x_continuous(labels = function(x) format(as.Date(x, origin = "1970-01-01"), "%b %y")) + 
  facet_wrap(~group)

enter image description here

, as.numeric(date), Inf -Inf

: : date_trans

, scale_x_continuous. ( . , / , , , breaks , , seq.Date.)

, temp[!duplicated(temp$group),], , , -.

+3

lukeA , , , :

library(ggplot2)
set.seed(2016-12-19)
dates <- seq(as.Date('2016-01-01'), as.Date('2016-12-31'), by = 1)
values <- rexp(length(dates), 1)
groups <- rpois(length(dates), 5)
temp <- data.frame(
    date = dates,
    value = values,
    group = groups,
    value.min = 1,
    value.max = 2
)
bounds <- data.frame(
    xmin = -Inf,
    xmax = Inf,
    ymin = temp$value.min[1],
    ymax = temp$value.max[1]
)
ggplot(temp, aes(date, value)) +
    geom_rect(
        aes(
            xmin = as.Date(xmin, origin = '1970-01-01'),
            xmax = as.Date(xmax, origin = '1970-01-01'),
            ymin = ymin,
            ymax = ymax
        ),
        data = bounds,
        fill = '#00cc33',
        alpha = 0.3,
        inherit.aes = FALSE
    ) +
    geom_point() +
    facet_wrap(~group)

, , inherit.aes = FALSE, , -, temp data.frame(- ). -Inf Inf , ( POSIXt as.POSIXct/lt, ).

+1

Source: https://habr.com/ru/post/1664149/


All Articles