Back to Programming coronaPlot

Download COVID-19 Data

First, download data for selected countries in Eastern Europe from Johns Hopkins COVID-19 tracker (See Source below):

library(coronavirus)
library(dplyr)
library(magrittr)
data("coronavirus")
coronaPlot <- coronavirus %>%
  filter(type %in% c("confirmed", "death", "recovered"), 
         country %in% c("Belarus", "Russia", "Ukraine", "Poland"))

Prepare Plot Data

Then, perform a few steps to prepare the data for plotting:

# Reassign country and type variables to factor type
coronaPlot$country <- as.factor(coronaPlot$country)
coronaPlot$type <- as.factor(coronaPlot$type)
# Assign countries to regional groups, as factor type
coronaPlot$group <- as.factor(ifelse(coronaPlot$country == "Poland", "EU", "Non-EU"))
# Aggregate cases by region
cases.region <- aggregate(cases ~ group+date+type, data = coronaPlot, FUN = sum)
# Merge aggregated regional case counts with country case counts
coronaPlotM <- merge(coronaPlot, cases.region, by = c("group", "date", "type"), all = TRUE)
# Clean up names from merge
names(coronaPlotM)[names(coronaPlotM)=="cases.x"] <- "cases.country"
names(coronaPlotM)[names(coronaPlotM)=="cases.y"] <- "cases.group"
# Sort data; this is necessary to prevent errors in plotting
attach(coronaPlotM)
coronaPlotM <- coronaPlotM[order(group, country, type, date),]
detach(coronaPlotM)
rownames(coronaPlotM) <- NULL

Generate Plots using Plotly

Finally, create two related plots showing cases by regional group in top chart and cases by country in bottom chart. Add a dropdown menu to select which kind of cases will be shown (confirmed, death, or recovered):

library(plotly)
fig1 <- coronaPlotM %>% plot_ly(
  type = 'scatter',
  mode = 'markers',
  x = ~date, 
  y = ~cases.group, 
  text = ~type,
  color = ~group, 
  legendgroup = ~group,
  transforms = list(
    list(
      type = 'filter',
      target = ~type,
      operation = '=',
      value = unique(coronaPlotM$type)[1]))
) %>% layout(
  title = "COVID-19 Cases in Eastern Europe",
  annotations = list(
    list(x = 0.5, y = 1.0, text = "Cases by Region", showarrow = F, xref='paper', yref='paper'),
    list(x = 0.0, y = 1.0, text = "Choose Case Type:", showarrow = F, xref='paper', yref='paper')),
  xaxis = list(
    showgrid = F
  ),
  yaxis = list(
    showgrid = F
  ),
  legend=list(itemclick="toggleothers"),
  updatemenus = list(
    list(
      type = 'dropdown', y = 0.95, x = 0.0,
      xanchor = "left", yanchor = "top",
      active = 0,
      buttons = list(
        list(method = "restyle",
             args = list("transforms[0].value", unique(coronaPlotM$type)[1]),
             label = unique(coronaPlotM$type)[1]),
        list(method = "restyle",
             args = list("transforms[0].value", unique(coronaPlotM$type)[2]),
             label = unique(coronaPlotM$type)[2]),
        list(method = "restyle",
             args = list("transforms[0].value", unique(coronaPlotM$type)[3]),
             label = unique(coronaPlotM$type)[3]))
    )
  )
)
fig2 <- coronaPlotM %>% plot_ly(  
  type = 'scatter',
  mode = 'markers',
  x = ~date, 
  y = ~cases.country, name = ~country, 
  text = ~type,
  color = ~country, 
  legendgroup = ~group,
  transforms = list(
    list(
      type = 'filter',
      target = ~type,
      operation = '=',
      value = list(unique(coronaPlotM$type)[1])))
) %>% layout(
  annotations = list(
    list(x = 0.5, y = 1.05, text = "Cases by Country", showarrow = F, xref='paper', yref='paper')),
  xaxis = list(
    showgrid = F
  ),
  yaxis = list(
    showgrid = F
  ),
  legend=list(itemclick="toggleothers")
)
fig <- subplot(fig1, fig2, nrows = 2, shareX = T)

fig
library(DT)
datatable(coronaPlotM)

Source: COVID-19 Data Repository by the Center for Systems Science and Engineering (CSSE) at Johns Hopkins University, https://github.com/CSSEGISandData/COVID-19