R/plot_linedate.R

Defines functions plot_linedate

Documented in plot_linedate

#' plot_linedate
#'
#' @export
#'
plot_linedate <- function(data, legend, n) {
  plot.this <- data %>%
    tibble::rownames_to_column("variable") %>%
    reshape2::melt(id.vars = "variable", variable.name = "date") %>%
    dplyr::mutate(date = as.Date(date))

  if(!missing(n)) {
    ind <- plot.this %>%
      dplyr::group_by(variable) %>%
      dplyr::summarise(total = sum(value), .groups = "drop") %>%
      dplyr::arrange(total) %$%
      variable

    len <- length(ind)

    if(n == "Top 5") {
      topn <- min(5, len)
    } else if(n == "Top 10") {
      topn <- min(10, len)
    } else if(n == "All") {
      topn <- len
    }

    plot.this %<>% dplyr::filter(variable %in% head(ind, topn)) %>%
      dplyr::mutate(variable = factor(variable, levels = ind))
  }

  pal <- RColorBrewer::brewer.pal(n = 7, name = 'Dark2')

  plotly::plot_ly(plot.this, x = ~date, y = ~value) %>%
    plotly::add_trace(type = "scatter", mode = "markers+lines",
                      color = ~variable, colors = pal) %>%
    # plotly::add_markers(color = ~variable) %>%
    plotly::layout(xaxis = list(title = "Week commencing",
                                type = "date",
                                tickformat = "%d. %b"),
                   yaxis = list(title = "Number of deaths"),
                   legend = list(title = list(text = paste0("<b>", legend,
                                                            "</b>"))),
                   autosize = TRUE)

}
soniamitchell/SCRCshinyApp documentation built on July 7, 2020, 5:36 a.m.