logger$info("Creating time-series plots")



Time-series of the selected type names, group codes and and spatial codes are given in the plots below, including trend line and smoother.

The lines and dots have the following meaning:



d_ltr %>%
    filter(type_name %in% unique(c("TC", TYPE_NAME, GROUP_CODE))) %>%
    filter(location_code %in% LOCATION_CODE) %>%
    left_join(
      d_stats %>%
        select(location_code, type_name, b0, b1), 
      by = c("location_code", "type_name")) %>%
    group_split(location_code, type_name) %>%
    map_df(function(x) {
        ymax <- CUTOFF_COUNT_AXIS / 100 * max(x$count)
        n_extremes <- sum(x$count > ymax)
        warn_message <- str_glue("NB: {n_extremes} largest counts are missing because only the lower {CUTOFF_COUNT_AXIS}% of the plot is given")
        g <- ggplot(data = x, mapping = aes(x = date, y = count)) +
                geom_point(na.rm = TRUE) +
                scale_x_date(name = "", limits = c(DATE_FROM, DATE_TO)) +
                coord_cartesian(ylim = c(NA, ymax)) +
                ggtitle(
                    label = str_c(x$location_code[1], x$type_name[1], sep = "  "),
                    subtitle = if (CUTOFF_COUNT_AXIS < 100) {warn_message} else {waiver()}) +
                theme(plot.subtitle = element_text(size = 8))
        if (nrow(x) >= 5L) {
            if (nrow(x) <= 24L) {
                g <- g +        
                    geom_path(size = 0.5, colour = "gray")
            }
            d <- tibble(date = range(x$date)) %>%
                mutate(count = x$b0[1] + x$b1[1] * as.numeric(date))
            g <- g + 
                geom_point() +
                geom_path(
                    data = d, 
                    mapping = aes(x = date, y = count),
                    colour = "red", size = 1.0)
            if (nrow(x) >= 25L) {
                g <- g +        
                    geom_smooth(method = "loess", formula = y ~ x, se = FALSE,
                                colour = "blue", size = 0.5)
            }
        }
        tibble(
            g = list(g), 
            location_code = x$location_code[1], 
            type_name = x$type_name[1])
    }) %>%
    arrange(location_code, type_name) %>%
    pull("g") %>%
    walk(print)


Try the litteR package in your browser

Any scripts or data that you put into this service are public.

litteR documentation built on Aug. 27, 2022, 1:05 a.m.