Skip to content

Instantly share code, notes, and snippets.

@RamiKrispin
Created February 28, 2020 14:57
Show Gist options
  • Save RamiKrispin/8f4034ec5672f3931ba7a0d05ea1339b to your computer and use it in GitHub Desktop.
Save RamiKrispin/8f4034ec5672f3931ba7a0d05ea1339b to your computer and use it in GitHub Desktop.
Creating bar chart with filter
library(coronavirus)
df <- coronavirus %>%
# dplyr::filter(date == max(date)) %>%
dplyr::group_by(Country.Region, type) %>%
dplyr::summarise(total = sum(cases)) %>%
tidyr::pivot_wider(names_from = type,
values_from = total) %>%
dplyr::arrange(-confirmed) %>%
dplyr::ungroup() %>%
dplyr::mutate(country = factor(Country.Region, levels = Country.Region))
df$flag <- NA
df$flag[1:10] <- "Top 10 Countries"
corona<- plotly::highlight_key(df)
widgets <- crosstalk::bscols(
widths = c(12, 12, 12),
crosstalk::filter_select("country", "Country", corona, ~country)
# crosstalk:: filter_checkbox("flag", "Top 10", corona, ~flag, inline = FALSE)
# filter_slider("confirmed", "Confirmed Cases", corona, ~confirmed),
# filter_slider("recovered", "Recovered Cases", corona, ~recovered)
)
crosstalk::bscols(
widths = c(4, 12), widgets,
plotly::plot_ly(data = corona, x = ~ country, y = ~ confirmed, type = "bar", name = "Confirmed") %>%
plotly::add_trace(y = ~ recovered, name = "Recovered") %>%
plotly::add_trace(y = ~ death, name = "Death") %>%
plotly::layout(barmode = 'stack',
yaxis = list(title = "Total Cases (log scaled)",
type = "log"),
xaxis = list(title = "Country"))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment