使用 plotly 在日期轴中为周末设置不同的背景颜色
Set a different background color for weekends in a date axis using plotly
我在 r
中使用 plot.ly
来显示 X 轴上有日期的时间序列(请参阅下面的代码)。我还想突出显示周末,很可能使用较暗的背景。我知道我可以使用 layout
设置背景颜色,但 AFAIK 这适用于所有绘图区域,而不是特定区域。
有没有办法在横轴上使用不同的周末背景颜色,如下图所示?
library(plotly)
df <- read.csv("https://raw.githubusercontent.com/datadista/datasets/master/COVID%2019/nacional_covid19.csv")
plot_ly(data = df) %>%
add_trace(x = ~ fecha,
y = ~ casos,
type = "scatter",
mode = "lines+markers",
name = "Contagios detectados") %>%
layout(title = "My title",
legend = list(x = 0.1, y = 0.9),
hovermode = "compare")
这是数据框:
> tail(df)
fecha casos altas fallecimientos ingresos_uci hospitalizados
32 2020-03-27 64059 9357 4858 4165 36293
33 2020-03-28 72248 12285 5690 4575 40630
34 2020-03-29 78797 14709 6528 4907 43397
35 2020-03-30 85195 16780 7340 5231 46617
36 2020-03-31 94417 19259 8189 5607 49243
37 2020-04-01 102136 22647 9053 5872 51418
> dput(df)
structure(list(fecha = structure(1:37, .Label = c("2020-02-25",
"2020-02-26", "2020-02-27", "2020-02-28", "2020-02-29", "2020-03-01",
"2020-03-02", "2020-03-03", "2020-03-04", "2020-03-05", "2020-03-06",
"2020-03-07", "2020-03-08", "2020-03-09", "2020-03-10", "2020-03-11",
"2020-03-12", "2020-03-13", "2020-03-14", "2020-03-15", "2020-03-16",
"2020-03-17", "2020-03-18", "2020-03-19", "2020-03-20", "2020-03-21",
"2020-03-22", "2020-03-23", "2020-03-24", "2020-03-25", "2020-03-26",
"2020-03-27", "2020-03-28", "2020-03-29", "2020-03-30", "2020-03-31",
"2020-04-01"), class = "factor"), casos = c(3L, 10L, 16L, 32L,
44L, 66L, 114L, 135L, 198L, 237L, 365L, 430L, 589L, 999L, 1622L,
2128L, 2950L, 4209L, 5753L, 7753L, 9191L, 11178L, 13716L, 17147L,
19980L, 24926L, 28572L, 33089L, 39673L, 47610L, 56188L, 64059L,
72248L, 78797L, 85195L, 94417L, 102136L), altas = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 23L, 135L, 183L, 189L,
189L, 517L, 517L, 530L, 1028L, 1081L, 1107L, 1585L, 2125L, 2575L,
3355L, 3794L, 5367L, 7015L, 9357L, 12285L, 14709L, 16780L, 19259L,
22647L), fallecimientos = c(NA, NA, NA, NA, NA, NA, NA, NA, 1L,
3L, 5L, 8L, 17L, 17L, 35L, 47L, 84L, 120L, 136L, 288L, 309L,
491L, 598L, 767L, 1002L, 1326L, 1720L, 2182L, 2696L, 3434L, 4089L,
4858L, 5690L, 6528L, 7340L, 8189L, 9053L), ingresos_uci = c(NA,
NA, NA, NA, NA, NA, NA, NA, 7L, 9L, 11L, NA, NA, 68L, 100L, 142L,
190L, 272L, 293L, 382L, 432L, 563L, 774L, 939L, 1141L, 1612L,
1785L, 2355L, 2636L, 3166L, 3679L, 4165L, 4575L, 4907L, 5231L,
5607L, 5872L), hospitalizados = c(NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 3215L,
5717L, NA, 10542L, 13282L, 15554L, 18374L, 22762L, 26960L, 31912L,
36293L, 40630L, 43397L, 46617L, 49243L, 51418L)), .Names = c("fecha",
"casos", "altas", "fallecimientos", "ingresos_uci", "hospitalizados"
), class = "data.frame", row.names = c(NA, -37L))
这是输出图:
我的想法是为周末添加多边形,这类似于周末的不同背景。
library(plotly)
library(dplyr)
library(tidyr)
#Creating a dataset for polygons with weekend dates
data.frame(fecha=as.Date(df1$fecha), day=weekdays(as.Date(df1$fecha))) %>%
filter(day %in% c("Saturday", "Sunday")) %>%
pivot_wider(names_from = day, values_from = fecha) %>%
unnest() -> df2
df1 %>%
mutate(fecha = as.Date(fecha)) %>%
plot_ly(data = .) %>%
add_trace(x = ~ fecha,
y = ~ casos,
type = "scatter",
mode = "lines+markers",
name = "Contagios detectados") %>%
layout(title = "My title",
legend = list(x = 0.1, y = 0.9),
hovermode = "compare") -> p
for (i in 1:nrow(df2)) {
p <- add_polygons(p, x = c(df2[[i, "Saturday"]], df2[[i, "Saturday"]],
df2[[i, "Sunday"]], df2[[i, "Sunday"]]),
y = c(0, max(df1$casos), max(df1$casos), 0),
hoverinfo = "none", color = I("blue"), showlegend = F,
hoveron = "points", opacity = 0.5)
}
p
我在 r
中使用 plot.ly
来显示 X 轴上有日期的时间序列(请参阅下面的代码)。我还想突出显示周末,很可能使用较暗的背景。我知道我可以使用 layout
设置背景颜色,但 AFAIK 这适用于所有绘图区域,而不是特定区域。
有没有办法在横轴上使用不同的周末背景颜色,如下图所示?
library(plotly)
df <- read.csv("https://raw.githubusercontent.com/datadista/datasets/master/COVID%2019/nacional_covid19.csv")
plot_ly(data = df) %>%
add_trace(x = ~ fecha,
y = ~ casos,
type = "scatter",
mode = "lines+markers",
name = "Contagios detectados") %>%
layout(title = "My title",
legend = list(x = 0.1, y = 0.9),
hovermode = "compare")
这是数据框:
> tail(df)
fecha casos altas fallecimientos ingresos_uci hospitalizados
32 2020-03-27 64059 9357 4858 4165 36293
33 2020-03-28 72248 12285 5690 4575 40630
34 2020-03-29 78797 14709 6528 4907 43397
35 2020-03-30 85195 16780 7340 5231 46617
36 2020-03-31 94417 19259 8189 5607 49243
37 2020-04-01 102136 22647 9053 5872 51418
> dput(df)
structure(list(fecha = structure(1:37, .Label = c("2020-02-25",
"2020-02-26", "2020-02-27", "2020-02-28", "2020-02-29", "2020-03-01",
"2020-03-02", "2020-03-03", "2020-03-04", "2020-03-05", "2020-03-06",
"2020-03-07", "2020-03-08", "2020-03-09", "2020-03-10", "2020-03-11",
"2020-03-12", "2020-03-13", "2020-03-14", "2020-03-15", "2020-03-16",
"2020-03-17", "2020-03-18", "2020-03-19", "2020-03-20", "2020-03-21",
"2020-03-22", "2020-03-23", "2020-03-24", "2020-03-25", "2020-03-26",
"2020-03-27", "2020-03-28", "2020-03-29", "2020-03-30", "2020-03-31",
"2020-04-01"), class = "factor"), casos = c(3L, 10L, 16L, 32L,
44L, 66L, 114L, 135L, 198L, 237L, 365L, 430L, 589L, 999L, 1622L,
2128L, 2950L, 4209L, 5753L, 7753L, 9191L, 11178L, 13716L, 17147L,
19980L, 24926L, 28572L, 33089L, 39673L, 47610L, 56188L, 64059L,
72248L, 78797L, 85195L, 94417L, 102136L), altas = c(NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 23L, 135L, 183L, 189L,
189L, 517L, 517L, 530L, 1028L, 1081L, 1107L, 1585L, 2125L, 2575L,
3355L, 3794L, 5367L, 7015L, 9357L, 12285L, 14709L, 16780L, 19259L,
22647L), fallecimientos = c(NA, NA, NA, NA, NA, NA, NA, NA, 1L,
3L, 5L, 8L, 17L, 17L, 35L, 47L, 84L, 120L, 136L, 288L, 309L,
491L, 598L, 767L, 1002L, 1326L, 1720L, 2182L, 2696L, 3434L, 4089L,
4858L, 5690L, 6528L, 7340L, 8189L, 9053L), ingresos_uci = c(NA,
NA, NA, NA, NA, NA, NA, NA, 7L, 9L, 11L, NA, NA, 68L, 100L, 142L,
190L, 272L, 293L, 382L, 432L, 563L, 774L, 939L, 1141L, 1612L,
1785L, 2355L, 2636L, 3166L, 3679L, 4165L, 4575L, 4907L, 5231L,
5607L, 5872L), hospitalizados = c(NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 3215L,
5717L, NA, 10542L, 13282L, 15554L, 18374L, 22762L, 26960L, 31912L,
36293L, 40630L, 43397L, 46617L, 49243L, 51418L)), .Names = c("fecha",
"casos", "altas", "fallecimientos", "ingresos_uci", "hospitalizados"
), class = "data.frame", row.names = c(NA, -37L))
这是输出图:
我的想法是为周末添加多边形,这类似于周末的不同背景。
library(plotly)
library(dplyr)
library(tidyr)
#Creating a dataset for polygons with weekend dates
data.frame(fecha=as.Date(df1$fecha), day=weekdays(as.Date(df1$fecha))) %>%
filter(day %in% c("Saturday", "Sunday")) %>%
pivot_wider(names_from = day, values_from = fecha) %>%
unnest() -> df2
df1 %>%
mutate(fecha = as.Date(fecha)) %>%
plot_ly(data = .) %>%
add_trace(x = ~ fecha,
y = ~ casos,
type = "scatter",
mode = "lines+markers",
name = "Contagios detectados") %>%
layout(title = "My title",
legend = list(x = 0.1, y = 0.9),
hovermode = "compare") -> p
for (i in 1:nrow(df2)) {
p <- add_polygons(p, x = c(df2[[i, "Saturday"]], df2[[i, "Saturday"]],
df2[[i, "Sunday"]], df2[[i, "Sunday"]]),
y = c(0, max(df1$casos), max(df1$casos), 0),
hoverinfo = "none", color = I("blue"), showlegend = F,
hoveron = "points", opacity = 0.5)
}
p