Altering colours of plots outputted by fasstr package

Hi,

I use a package called fasstr(), which performs various calculations and outputs a list of ggplot2 objects for plotting. I would like to be able to change the colours of the plots which this function outputs, but I am having a hard time understanding what the function is doing internally and how I can alter the colour of the outputs.

Example:

library(fasstr)
library(tidyhydat)

plot_missing_dates(station_number = "07GJ001") 

Outputs three plots that all look similar to this:

All I want to do is change the colours used in these plots so that I can create a brand-consistent report, but I'm completely stumped. Any help is greatly appreciated!

You might be able to adjust the colors with scale_fill_manual, as in the examples below.

DF <- data.frame(Season = c("A","A","A", "B", "B","B"),
                 Year = c(2000,2001,2002,2000,2001,2002),
                 Value = c(6,5,2,4,7,3))
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.3.3
Plt <- ggplot(DF, aes(x = Year, y = Value, fill = Season)) +
  geom_col()
Plt


#assign colors by the order of the groups
Plt2 <- Plt + scale_fill_manual(values = c("blue", "red"))
Plt2


#assign colors by the name of the group, if you know the names
Plt3 <- Plt + scale_fill_manual(values = c(B = "blue", A = "red")) 
Plt3

Created on 2024-04-26 with reprex v2.0.2

It think it's more than that. @ beeju is using canned functions.

The function they mention, plot_missing_dates() looks like this:

 plot_missing_dates
function (data, dates = Date, values = Value, groups = STATION_NUMBER, 
    station_number, roll_days = 1, roll_align = "right", water_year_start = 1, 
    start_year, end_year, months = 1:12, include_title = FALSE, 
    plot_type = "tile") 
{
    if (missing(data)) {
        data <- NULL
    }
    if (missing(station_number)) {
        station_number <- NULL
    }
    if (missing(start_year)) {
        start_year <- 0
    }
    if (missing(end_year)) {
        end_year <- 9999
    }
    plot_type <- plot_type[1]
    if (!any(c("tile", "bar") %in% plot_type)) 
        stop("plot_type must be one of 'tile' or 'bar' plots.", 
            call. = FALSE)
    logical_arg_check(include_title)
    flow_data <- flowdata_import(data = data, station_number = station_number)
    flow_data <- format_all_cols(data = flow_data, dates = as.character(substitute(dates)), 
        values = as.character(substitute(values)), groups = as.character(substitute(groups)), 
        rm_other_cols = TRUE)
    flow_summary <- screen_flow_data(data = flow_data, roll_days = roll_days, 
        roll_align = roll_align, water_year_start = water_year_start, 
        start_year = start_year, end_year = end_year, months = months, 
        include_symbols = FALSE)
    if (plot_type == "bar") {
        missing_plotdata <- flow_summary[, c(1, 2, 11:ncol(flow_summary))]
        missing_plotdata <- tidyr::gather(missing_plotdata, Month, 
            Value, 3:ncol(missing_plotdata))
        missing_plotdata <- dplyr::mutate(missing_plotdata, Month = substr(Month, 
            1, 3))
        missing_plotdata$Month <- factor(missing_plotdata$Month, 
            levels = month.abb[c(water_year_start:12, 1:water_year_start - 
                1)])
        miss_plots <- dplyr::group_by(missing_plotdata, STATION_NUMBER)
        miss_plots <- tidyr::nest(miss_plots)
        miss_plots <- dplyr::mutate(miss_plots, plot = purrr::map2(data, 
            STATION_NUMBER, ~ggplot2::ggplot(data = ., ggplot2::aes(x = Year, 
                y = Value)) + ggplot2::geom_bar(colour = "cornflowerblue", 
                fill = "cornflowerblue", na.rm = TRUE, stat = "identity") + 
                ggplot2::facet_wrap(~Month, ncol = 3, scales = "fixed", 
                  strip.position = "top") + ggplot2::ylab("Missing Days") + 
                ggplot2::xlab(ifelse(water_year_start == 1, "Year", 
                  "Water Year")) + ggplot2::theme_bw() + ggplot2::scale_y_continuous(limits = c(0, 
                32)) + {
                if (include_title & .y != "XXXXXXX") 
                  ggplot2::ggtitle(paste(.y))
            } + ggplot2::theme(panel.border = ggplot2::element_rect(colour = "black", 
                fill = NA, size = 1), panel.grid = ggplot2::element_line(size = 0.2), 
                axis.title = ggplot2::element_text(size = 12), 
                axis.text = ggplot2::element_text(size = 10), 
                plot.title = ggplot2::element_text(hjust = 1, 
                  size = 9, colour = "grey25"), strip.background = ggplot2::element_blank(), 
                strip.text = ggplot2::element_text(hjust = 0, 
                  face = "bold", size = 10))))
    }
    else if (plot_type == "tile") {
        missing_plotdata <- dplyr::select(flow_summary, c(1:2, 
            11:ncol(flow_summary)))
        missing_plotdata <- tidyr::pivot_longer(missing_plotdata, 
            -(1:2), names_to = "Month", values_to = "Missing")
        missing_plotdata <- dplyr::mutate(missing_plotdata, Month = factor(substr(Month, 
            1, 3), levels = rev(month.abb[c(water_year_start:12, 
            1:water_year_start - 1)])), Days = lubridate::days_in_month(paste(Year, 
            match(Month, month.abb), 1, sep = "-")), Percent_Missing = Missing/Days * 
            100)
        miss_plots <- dplyr::group_by(missing_plotdata, STATION_NUMBER)
        miss_plots <- tidyr::nest(miss_plots)
        miss_plots <- dplyr::mutate(miss_plots, plot = purrr::map2(data, 
            STATION_NUMBER, ~ggplot2::ggplot(data = ., ggplot2::aes(x = Year, 
                y = Month)) + ggplot2::geom_tile(ggplot2::aes(fill = Percent_Missing), 
                colour = "black") + ggplot2::scale_x_continuous(expand = c(0, 
                0)) + ggplot2::scale_y_discrete(expand = c(0, 
                0), limits = rev(levels(month.abb))) + ggplot2::scale_fill_viridis_c(direction = -1, 
                name = "Missing\nDays (%)") + ggplot2::ylab("Month") + 
                ggplot2::xlab(ifelse(water_year_start == 1, "Year", 
                  "Water Year")) + ggplot2::theme(axis.title.y = ggplot2::element_blank())))
    }
    plots <- miss_plots$plot
    if (nrow(miss_plots) == 1) {
        names(plots) <- "Missing_Dates"
    }
    else {
        names(plots) <- paste0(miss_plots$STATION_NUMBER, "_Missing_Dates")
    }
    plots
}
<bytecode: 0x56c5eacf8d30>
<environment: namespace:fasstr>

I understand the problem of figuring out what that function is doing.

assign the output of plot_missing_dates to a (list) variable, as in

p <- plot_missing_dates(station_number = "07GJ001")

then use

p$Missing_Dates + scale_fill_*

to use any scale you want, like

p$Missing_Dates + scale_fill_viridis_c(option = "cividis")

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.