R/plot_verlauf_bl.R

Defines functions get_bl_ogd_demo plot_verlauf_bl get_bez_ogd_demo plot_verlauf_bez

Documented in plot_verlauf_bez plot_verlauf_bl

globalVariables(c(
  "faelle", "Bundesland", "neu_positive_tests", "bezirk", "Bezirk"
))

get_bl_ogd_demo <- function() {
  url <- "https://github.com/botic/ogd-demo-covid19-at/blob/master/data/2020-03-18/covid19-2020-03-18-verlauf-bundeslaender.csv?raw=true"
  tmpf <- tempfile(fileext = ".csv")
  on.exit(file.remove(tmpf))  
  utils::download.file(url, tmpf, quiet = TRUE)
  utils::read.csv(tmpf) %>% 
    dplyr::group_by(bundesland) %>% 
    dplyr::mutate(faelle = cumsum(neu_positive_tests)) %>% 
    dplyr::mutate(datum = as.Date(datum)) %>% 
    dplyr::ungroup() %>% 
    dplyr::rename(Bundesland = bundesland)
}

#' Zeitlicher Verlauf pro Bundesland
#' @param dat Der Datensatz, der als Grundlage für die Visualisierung
#'   verwendet werden soll
#' @examples 
#' plot_verlauf_bl()
#' @export
plot_verlauf_bl <- function(dat = get_bl_ogd_demo()) {
  dat %>% 
    ggplot(aes(datum, faelle, color = Bundesland)) +
    geom_line() +
    scale_y_continuous(trans = "log", breaks = c(2, 5, 10, 20, 50, 100, 200, 500)) +
    theme_minimal() +
    xlab("Datum") +
    ylab("Best\u00e4tigte F\u00e4lle")
}

get_bez_ogd_demo <- function(bezirk_ids = 701) {
  url <- "https://github.com/botic/ogd-demo-covid19-at/blob/master/data/2020-03-18/covid19-2020-03-18-verlauf-bezirke.csv?raw=true"
  tmpf <- tempfile(fileext = ".csv")
  on.exit(file.remove(tmpf))  
  utils::download.file(url, tmpf, quiet = TRUE)
  utils::read.csv(tmpf) %>% 
    dplyr::filter(gkz %in% bezirk_ids) %>% 
    dplyr::group_by(gkz) %>% 
    dplyr::mutate(faelle = cumsum(neu_positive_tests)) %>% 
    dplyr::mutate(datum = as.Date(datum)) %>% 
    dplyr::ungroup() %>% 
    dplyr::mutate(gkz = as.factor(gkz))
}

globalVariables("gkz")

#' Zeitlicher Verlauf pro Bezirk
#' 
#' @param bezirk_ids Bezirkskennzahlen für alle Bezirke, die angezeigt werden
#'   sollen
#' @examples 
#' plot_verlauf_bez(c(701, 901, 921))
#' @export
plot_verlauf_bez <- function(bezirk_ids = 701) {
  get_bez_ogd_demo(bezirk_ids) %>% 
    dplyr::rename(id = gkz) %>% 
    merge(coronaAT::geo_bez) %>% 
    dplyr::rename(Bezirk = bezirk) %>% 
    ggplot(aes(datum, faelle/population * 100000, color = Bezirk)) +
    geom_line() +
    scale_y_continuous(trans = "log", breaks = c(1, 2, 5, 10, 20, 50, 100, 200, 500)) +
    theme_minimal() +
    xlab("Datum") +
    ylab("Best\u00e4tigte  F\u00e4lle pro 100 000 Einwohner")
}
statistikat/coronar documentation built on April 6, 2020, 6:25 p.m.