I want to make this kind of graph (here from Our World In data ) but where the line colour varies by categorical variable. Since the calculated threshold is seasonal, I would need to use a categorical variable (see the Alert variable). Based on Stephan's answer, I made this script, and it's true that the document about the e_visual_map is quite scare.
x <- seq(
from = 1,
to = 100,
b = 1
)
y <- sin(x)
df <- data.frame(x = x, y = y)
df1<- df %>%
mutate(rM = rollmean(
y,
3,
na.pad = TRUE,
align = "center"
)) %>% mutate(
Threshold = round(rM, 2)
) %>% mutate (Alert = y > Threshold) %>%mutate(
Alert = case_when(
Alert == "FALSE" ~ 0,
TRUE ~ 1
)
)
df1 |>
e_charts(x) |>
e_line(y) |>
e_visual_map(
Alert,
type = "continuous",
inRange = list(color = c("#f6efa6","#bf444c")),
bottom = 300 # padding to avoid visual maps overlap
)
One option would be to use
e_visual_map
. Unfortunately the docs are quite scare but based on [this example] we could usepieces
to define the ranges for youry
column, i.e. there is no need to create a categorical variable: