我正在尝试通过使用在文档中获得交互性。rmarkdown
crosstalk
问题:当我使用串扰进行绘图时,它不会在图表中显示线条,但它会在将鼠标悬停在绘图上时给出值。有没有办法在过滤器中有默认选项?
代码:
library(tidyverse)
library(plotly)
library(crosstalk)
library(glue)
library(scales)
library(tidytext)
数据加载:
file_url <- "https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/ts_all_long2.csv"
ts_all_long <- read.csv(url(file_url))
共享数据
confirm_col = "#32a4ba"
death_col = "#f08080"
Country_selected = c("United Kingdom")
dual_axis_plt_data <- ts_all_long %>%
group_by(Country.Region) %>%
mutate(scaleFactor = max(Confirmed_daily) / max(Death_daily)) %>%
ungroup() %>%
SharedData$new()
ggplotly()
dual_axis_plt <- ggplotly(
ggplot(data = dual_axis_plt_data, aes(x = date)) +
geom_area(aes(y = Confirmed_daily), fill = confirm_col, alpha = .7) +
geom_line(aes(y = Death_daily * scaleFactor), col = death_col,
size = 0.8, alpha = 0.8) +
scale_y_continuous(name = "Daily Cases", sec.axis = sec_axis(~./scaleFactor, name = "Daily Deaths"),
labels = scales::comma_format()) +
scale_x_date(date_breaks = "1 month", date_labels = "%b") +
theme_excel_new() +
theme(
axis.title.y.left=element_text(color=confirm_col),
axis.text.y.left=element_text(color=confirm_col),
axis.title.y.right=element_text(color=death_col),
axis.text.y.right=element_text(color=death_col),
plot.title = element_markdown(face = "plain", family = "serif", size = 14),
panel.grid.major = element_blank()
) +
labs(title = glue("<i>{Country_selected}</i>: Daily confirmed & death cases as of: {max(ts_all_long$date)}"),
caption = "Data source: covid19.analytics)
)
bscols(widths = c(3, 9),
list(
filter_select(id = "country", label = "Country",
sharedData = dual_axis_plt_data, group = ~Country.Region)
),
dual_axis_plt)
期望的结果:
没有串扰交互,它看起来像这样(它是一个双轴图):