library("tidyverse")
library("lubridate")
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
# generate fake data
set.seed(1)
(data <- tibble(
date = seq(mdy("Jan 1, 2020"), today(), by = "day"),
val = round(exp(cumsum(rnorm(length(date), 0, .1))))
))
#> # A tibble: 392 x 2
#> date val
#> <date> <dbl>
#> 1 2020-01-01 1
#> 2 2020-01-02 1
#> 3 2020-01-03 1
#> 4 2020-01-04 1
#> 5 2020-01-05 1
#> 6 2020-01-06 1
#> 7 2020-01-07 1
#> 8 2020-01-08 1
#> 9 2020-01-09 1
#> 10 2020-01-10 1
#> # … with 382 more rows
# plot
data %>%
ggplot(aes(date, val)) +
geom_col()
# generate labels for time ranges
# note that this works because the cases are assigned sequentially
(data <- data %>% mutate(
status = case_when(
date < mdy("March 1, 2020") ~ "pre-shutdown",
date < mdy("June 1, 2020") ~ "shutdown",
date < mdy("Nov 1, 2020") ~ "open",
date <= today() ~ "shutdown"
)
))
#> # A tibble: 392 x 3
#> date val status
#> <date> <dbl> <chr>
#> 1 2020-01-01 1 pre-shutdown
#> 2 2020-01-02 1 pre-shutdown
#> 3 2020-01-03 1 pre-shutdown
#> 4 2020-01-04 1 pre-shutdown
#> 5 2020-01-05 1 pre-shutdown
#> 6 2020-01-06 1 pre-shutdown
#> 7 2020-01-07 1 pre-shutdown
#> 8 2020-01-08 1 pre-shutdown
#> 9 2020-01-09 1 pre-shutdown
#> 10 2020-01-10 1 pre-shutdown
#> # … with 382 more rows
# plot
data %>%
ggplot(aes(date, val)) +
geom_col(aes(fill = status))
# polish up a bit
data %>%
ggplot(aes(date, val)) +
geom_col(aes(fill = status)) +
scale_x_date(
date_breaks = "1 month", date_labels = "%b"
) +
scale_y_continuous(sec.axis = dup_axis()) +
ggtitle("Value over time") +
theme_minimal() +
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y.left = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
legend.position = c(.15, .75),
legend.background = element_rect(fill = "white", color = NA)
)
# if you want a rolling average, you can use the slider package
# https://davisvaughan.github.io/slider/
library("slider")
(data <- data %>% mutate(
avg_7day = slide_dbl(val, mean, .before = 6)
))
#> # A tibble: 392 x 4
#> date val status avg_7day
#> <date> <dbl> <chr> <dbl>
#> 1 2020-01-01 1 pre-shutdown 1
#> 2 2020-01-02 1 pre-shutdown 1
#> 3 2020-01-03 1 pre-shutdown 1
#> 4 2020-01-04 1 pre-shutdown 1
#> 5 2020-01-05 1 pre-shutdown 1
#> 6 2020-01-06 1 pre-shutdown 1
#> 7 2020-01-07 1 pre-shutdown 1
#> 8 2020-01-08 1 pre-shutdown 1
#> 9 2020-01-09 1 pre-shutdown 1
#> 10 2020-01-10 1 pre-shutdown 1
#> # … with 382 more rows
# add 7 day average to plot, polish a little
data %>%
ggplot(aes(date, val)) +
geom_col(aes(fill = status), alpha = .5) +
geom_line(aes(y = avg_7day)) +
geom_text(
aes(
x = date + days(1),
y = avg_7day,
label = round(avg_7day, digits = 2)
),
hjust = "left",
data = data %>% slice_max(date)
) +
scale_x_date(
date_breaks = "1 month", date_labels = "%b",
expand = expansion(c(.05, .10)) # add space to right
) +
scale_y_continuous(sec.axis = dup_axis()) +
ggtitle("Value over time") +
theme_minimal() +
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y.left = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
legend.position = c(.15, .75),
legend.background = element_rect(fill = "white", color = NA)
)
Created on 2021-01-26 by the reprex package (v0.3.0)
<my_graphic.jpeg># Example of graphic I want
<graphic_I_want.jpeg>--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2
---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ggplot2/ca45ee53-100d-4b78-9796-e6bad10fdcb5n%40googlegroups.com.
<my_graphic.jpeg><graphic_I_want.jpeg>
--