I'm trying to use geom_rect
to colour parts of my time series (values
), based on the state
.
idx state value
1 1 S 0.00000
2 2 S 0.00001
3 3 S 0.00016
4 4 S 0.00003
5 5 S 0.00047
6 6 S 0.00002
7 7 J 0.00048
8 8 J 0.00011
9 9 J 0.00001
10 10 J 0.00753
'data.frame': 245 obs. of 3 variables:
$ idx : chr "1" "2" "3" "4" ...
$ state: chr "S" "S" "S" "S" ...
$ value: num 0 0.00001 0.00016 0.00003 ...
The code:
ggplot(new, aes(x = idx, y = value)) +
geom_rect(aes(xmin = idx, xmax = dplyr::lead(idx), ymin = -Inf, ymax = Inf, fill = factor(state)),
alpha = .3) +
geom_line(aes(x = idx, y = value)) +
theme(axis.title.y = element_blank())
Unfortunately, I get an error:
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
What am I doing wrong?