0

I possess time series data within an Excel file named "validation.xlsx." This dataset comprises three variables: time, observed, and simulated, spanning across 132 rows. I have successfully generated individual box plots for both the observed and simulated variables. However, I am currently facing challenges in creating a unified box plot canvas for both variables.

data file look line - first 10 rows

I am seeking assistance or guidance to overcome these obstacles. Your help in resolving this matter would be greatly appreciated. Thank you.

Boxplot

Here is the relevant code:

# packages
library(ggplot2)
library(tidyverse)
library(hydroTSM)

# Read Excel data file into a data frame
df <- read_excel("validation.xlsx")

# Convert the 'time' column to a Date format
df$time <- as.Date(df$time)
# Define seasons based on months (customize as needed)
seasons <- df %>%
  mutate(Season = case_when(
    Month %in% c("12", "01", "02") ~ "Winter",
    Month %in% c("03", "04", "05") ~ "Spring",
    Month %in% c("06", "07", "08") ~ "Summer",
    Month %in% c("09", "10", "11") ~ "Fall"
  ))

# Create a box plot
monthly_observed_boxplot <- ggplot(seasons, aes(x = Month)) +
  geom_boxplot(aes( y = observed, fill=Month),show.legend = F, alpha=0.5)  +
  labs(x = "Month", 
       y = expression(paste("Streamflow, ", m^3/s)), 
       title = "Monthly Box Plot - Observed") 
monthly_simulated_boxplot <- ggplot(seasons, aes(x = Month)) +
  geom_boxplot(aes( y = simulated, fill=Month),show.legend = F, alpha=0.5)  +
  labs(x = "Month", 
       y = expression(paste("Streamflow, ", m^3/s)), 
       title = "Simulated") 
monthly_observed_boxplot+monthly_simulated_boxplot


head(df)

  time                observed simulated
  <dttm>                 <dbl>     <dbl>
1 2011-01-01 00:00:00     40.3      24.8
2 2011-02-01 00:00:00     17.6      19.8
3 2011-03-01 00:00:00     63.9      17.2
4 2011-04-01 00:00:00    114.       75.5
5 2011-05-01 00:00:00    266.      269. 
6 2011-06-01 00:00:00    120.       87.2


head(seasons)

  time       observed simulated season Time1      Month Year  Season
  <date>        <dbl>     <dbl> <chr>  <date>     <chr> <chr> <chr> 
1 2011-01-01     40.3      24.8 Winter 2011-01-01 01    2011  Winter
2 2011-02-01     17.6      19.8 Winter 2011-02-01 02    2011  Winter
3 2011-03-01     63.9      17.2 Winter 2011-03-01 03    2011  Spring
4 2011-04-01    114.       75.5 Winter 2011-04-01 04    2011  Spring
5 2011-05-01    266.      269.  Winter 2011-05-01 05    2011  Spring
6 2011-06-01    120.       87.2 Winter 2011-06-01 06    2011  Summer

  • 1
    You could pivot your data into long format e.g. `season <- pivot_longer(season, -Month)`. Then you can plot with `ggplot(season, aes(Month, value, fill = name)) + geom_boxplot()` – Allan Cameron Aug 16 '23 at 18:56
  • `monthly_observed_boxplot + monthly_simulated_boxplot` looks like you're using the `patchwork` library, is that correct? (Otherwise, not sure another package supports addition of grobs.) – r2evans Aug 16 '23 at 19:24
  • 1
    Welcome to SO, Kamal Ziaul! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (you have this, great!), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Aug 16 '23 at 20:21

0 Answers0