4

I am trying to get interactivity in the rmarkdown document by using crosstalk.

Issue: When I plot using cross talk it doesn't display lines in the graph but it gives values on hovering over the plot. And is there a way to have default option in filter ?

enter image description here

Code:

library(tidyverse)
library(plotly)
library(crosstalk)
library(glue)
library(scales)
library(tidytext)

data loading:

file_url <- "https://raw.githubusercontent.com/johnsnow09/covid19-df_stack-code/main/ts_all_long2.csv"

ts_all_long <- read.csv(url(file_url))

shared data

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)

Desired result:

Without crosstalk interactivity it looks like this (Its a dual axis plot):

enter image description here

ViSa
  • 1,563
  • 8
  • 30

0 Answers0