rplotcross-correlation

Add labels and title to a plot derived from an lapply function


I can't figure out how to add title and axis labels for this whole figure. (not just axis labels on each panel)

structure(list(Date = structure(c(16709, 15126, 14883, 16587, 
15095, 15492, 14549, 17410, 16314, 17348, 14761, 14457, 16375, 
17014, 15857, 17836, 15826, 15522, 15918, 15857), class = "Date"), 
    Commonname = c("Black Sea Bass", "Pinfish", "Silver Perch", 
    "Black Sea Bass", "Silver Perch", "Pinfish", "Pinfish", "Pigfish", 
    "Pinfish", "Black Sea Bass", "Silver Perch", "Pinfish", "Black Sea Bass", 
    "Pigfish", "Black Sea Bass", "Pinfish", "Black Sea Bass", 
    "Pigfish", "Black Sea Bass", "Silver Perch"), CPUE = c(1.07628582778677, 
    7.86657009496217, 1.85135216165977, 0.348590691952807, 0.397594400562561, 
    4.49617260026057, 9.8560683819882, 2.52182932296262, 4.66061566304416, 
    1.85247646459195, 1.09432152065719, 12.0038255707299, 0.145989032254548, 
    1.86725301741492, 1.07356515883229, 11.5172014403701, 0.296643539261688, 
    2.70394855189782, 1.14838123183663, 0.871334772441085), Discharge = c(3.02423424, 
    0.232938467873684, 2.141316416, 1.31224770666667, 1.03639488, 
    14.38635024, 1.2352637664, 8.27794453333333, 7.04587330461538, 
    6.47180464, 5.84804846222222, 2.37460535832896, 1.68201792, 
    19.73397792, 3.56857026461538, 4.89821025684211, 5.41174500571429, 
    33.25524992, 47.1935879314286, 3.88199730666667)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))
df.split <- split(df, df$Commonname, drop = TRUE)
par(mfrow=c(2,2))
lapply(seq_along(df.split), function(x) ccf(df.split[[x]]$CPUE,df.split[[x]]$Discharge, lag.max = 5,
                                              ylab = "", xlab = "", main= names(df.split)[x]))

enter image description here

I can't add it into the function or it applies to all panels.


Solution

  • I've just added title() function;

    df.split <- split(df, df$Commonname, drop = TRUE)
    par(mfrow=c(2,2))
    lapply(seq_along(df.split), function(x) ccf(df.split[[x]]$CPUE,df.split[[x]]$Discharge, lag.max = 5,
                                                  ylab = "", xlab = "", main= names(df.split)[x]))
    title("This is a main title", line = -1, outer = TRUE,xlab = 'This is x axis',ylab = 'This is y axis')
    

    enter image description here