Plotting a box plot and line graph from two different dataframe in R

I have two dataframes
df1:

27-Apr-22 02-May-22 07-May-22 12-May-22 17-May-22 21-May-22 24-May-22 27-May-22 06-Jun-22
S1 0.069428571 0.044571429 0.086285714 0.077142857 0.028 0.032190476 0.039619048 0.06547619 0.109333333
S1 0.038333333 0.032857143 0.04 0.02 -0.02447619 -0.001380952 0.05 0.099047619 0.143619048
S2 0.183857143 0.13852381 0.079571429 0.104285714 0.089666667 0.108666667 0.094428571 0.090857143 0.053904762
S2 0.045571429 0.015619048 0.017190476 0.034761905 0.095619048 0.135904762 0.121809524 0.088047619 0.045761905

and df2

Date Rainfall(mm)
27-Apr-22 0.00012
02-May-22 0.00516
07-May-22 0.15439
12-May-22 0.1289
17-May-22 0.08721
21-May-22 0.01992
24-May-22 0.35019
27-May-22 0.10402
06-Jun-22 0.03086

Now i want to plot a box plot with df1 and line graph with df2 with the rainfall data on the secondary axis.

Hi @pgd
Welcome to the forum.
Multiple y-axes in the same graph frame is (often) regarded as bad form, so, try this:

suppressPackageStartupMessages(library(tidyverse))
library(patchwork)

df1 <- read.delim(header=TRUE, text=
"rows_df1   27-Apr-22   02-May-22   07-May-22   12-May-22   17-May-22   21-May-22   24-May-22   27-May-22   06-Jun-22
S1  0.069428571 0.044571429 0.086285714 0.077142857 0.028   0.032190476 0.039619048 0.06547619  0.109333333
S1  0.038333333 0.032857143 0.04    0.02    -0.02447619 -0.001380952    0.05    0.099047619 0.143619048
S2  0.183857143 0.13852381  0.079571429 0.104285714 0.089666667 0.108666667 0.094428571 0.090857143 0.053904762
S2  0.045571429 0.015619048 0.017190476 0.034761905 0.095619048 0.135904762 0.121809524 0.088047619 0.045761905
")

df1_long <- pivot_longer(df1, cols=starts_with("X"), names_to="date1")
df1_long$date1 <- as.Date(df1_long$date1, format="X%d.%b.%y")
df1_long
#> # A tibble: 36 × 3
#>    rows_df1 date1       value
#>    <chr>    <date>      <dbl>
#>  1 S1       2022-04-27 0.0694
#>  2 S1       2022-05-02 0.0446
#>  3 S1       2022-05-07 0.0863
#>  4 S1       2022-05-12 0.0771
#>  5 S1       2022-05-17 0.028 
#>  6 S1       2022-05-21 0.0322
#>  7 S1       2022-05-24 0.0396
#>  8 S1       2022-05-27 0.0655
#>  9 S1       2022-06-06 0.109 
#> 10 S1       2022-04-27 0.0383
#> # ℹ 26 more rows

my_breaks <- unique(df1_long$date1)


df2 <- read.delim(header=TRUE, text="
Date    Rainfall(mm)
27-Apr-22   0.00012
02-May-22   0.00516
07-May-22   0.15439
12-May-22   0.1289
17-May-22   0.08721
21-May-22   0.01992
24-May-22   0.35019
27-May-22   0.10402
06-Jun-22   0.03086
")


df2$date2 <- as.Date(df2$Date, format="%d-%b-%y")
df2
#>        Date Rainfall.mm.      date2
#> 1 27-Apr-22      0.00012 2022-04-27
#> 2 02-May-22      0.00516 2022-05-02
#> 3 07-May-22      0.15439 2022-05-07
#> 4 12-May-22      0.12890 2022-05-12
#> 5 17-May-22      0.08721 2022-05-17
#> 6 21-May-22      0.01992 2022-05-21
#> 7 24-May-22      0.35019 2022-05-24
#> 8 27-May-22      0.10402 2022-05-27
#> 9 06-Jun-22      0.03086 2022-06-06

p1 <- ggplot(df1_long) +
  aes(y=value, x=date1, group=date1) +
  geom_boxplot() +
  scale_x_date(breaks = my_breaks, date_labels = "%b %d")

p2 <- ggplot(df2) +
  aes(x=date2, y=Rainfall.mm.) +
  geom_point() +
  geom_line()

p1/p2

Created on 2023-05-27 with reprex v2.0.2

This topic was automatically closed 42 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.