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.

2 answers

  • answered 2021-09-11 18:50 Samet Sökel

    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

  • answered 2021-09-11 21:15 Johnny5ish

    I got it to where I need for the actual plot and data using this code and with the help of the previous answers.

    d.select.ccf <- BD %>% 
      group_by(Zone, Date, Commonname) %>%
      filter(Zone == "D") %>% 
      summarise(Number = sum(Number), CPUE = mean((CPUE)), Discharge = mean (Discharge)) %>% 
      ungroup()
    
    d.spp <- subset(d.select.ccf,  Commonname == "Pigfish" | Commonname == "Pinfish"
                    | Commonname == "Silver Perch"
                    | Commonname == 'Spottail Pinfish' | Commonname == "Dusky Pipefish"
                    | Commonname == "Black Sea Bass"
                    | Commonname == "Pink Shrimp" | Commonname == "Scallops"
                    | Commonname == 'Grass Porgy' | Commonname == 'Lane Snapper'
                    | Commonname == 'White Grunt'| Commonname == "Spotted Seatrout")
    d.split <- split(d.spp, d.spp$Commonname, drop = TRUE)
    par(mfrow=c(4,3),omi=c(0.1,0.1,0.1,0.2), mai=c(0.55,0.6,0.7,0.5))
    d.ccf.plot <- lapply(seq_along(d.split), function(x) ccf(d.split[[x]]$CPUE,d.split[[x]]$Discharge, lag.max = 5,
                                                             xlab = "", ylab = "", sub = "", main= names(d.split)[x]))
    title("Steinhatchee Lag Correlations", sub = " ", line = -1, outer = TRUE,
          xlab = 'Lag (months)',ylab = 'Correlation Coefficient', cex.main = 1.5, cex.lab = 1.5)
    

    Of course you would need the full data set to achieve this image which is way too large to post here. But I hope this alternate method can help someone.

    enter image description here

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum