Iterate over a data frame to produce multiple plots

Hello,

I have a large dataframe of groups that are distinguished by SITE_ID and DATE. I want to create a function or loop or just simple code that will allow me to group them and then generate a plot for each group. I tried the following:

dd %>%
  group_by(SITE_ID,DATE) %>%
  ggplot() +
  geom_line(aes(x=TEMP_C, y=DEPTH_m)) +
  scale_y_reverse()

But I got a plot of all the data on a single plot. Anyone have an idea of how to resolve this?

Here's a subset of my data:

dd <- structure(list(SITE_ID = c("BI22_01", "BI22_01", "BI22_01", "BI22_01", 
"BI22_01", "BI22_01", "BI22_01", "BI22_01", "BI22_01", "BI22_01", 
"BI22_01", "BI22_01", "BI22_01", "BI22_01", "BI22_01", "BI22_01", 
"BI22_01", "BI22_01", "BI22_01", "BI22_01"), DATE = structure(c(19138, 
19138, 19138, 19145, 19145, 19145, 19145, 19145, 19145, 19145, 
19145, 19145, 19145, 19145, 19145, 19151, 19151, 19151, 19151, 
19151), class = "Date"), DEPTH_m = c(0.1, 0.5, 1, 0.1, 0.5, 1, 
1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 0.1, 0.2, 0.4, 0.6, 0.8), 
    TEMP_C = c(16.115, 15.87, 13.86, 15, 14.9, 14.65, 14.425, 
    14.3, 14.005, 13.995, 13.91, 13.885, 13.84, 13.785, 13.665, 
    22.805, 22.605, 22.04, 21.565, 21.085), COND_US = c(0.108, 
    0.1075, 0.107, 0.119, 0.119, 0.1195, 0.1195, 0.1195, 0.1195, 
    0.1195, 0.1195, 0.1195, 0.12, 0.12, 0.12, 0.1315, 0.1315, 
    0.7115, 0.1315, 0.13), DO_MG_L = c(9.815, 9.245, 8.985, 8.885, 
    8.875, 8.895, 8.875, 8.835, 8.87, 8.845, 8.875, 8.83, 8.805, 
    8.835, 8.765, 9.415, 9.26, 9.08, 8.95, 8.76), PH = c(7.825, 
    7.725, 7.73, 7.24, 7.58, 7.56, 7.545, 7.545, 7.535, 7.53, 
    7.53, 7.52, 7.51, 7.51, 7.515, 7.34, 7.335, 7.325, 7.32, 
    7.315), TURB_NU = c(7.05, 7.8, 7.35, 9.6, 9.15, 9.95, 9.55, 
    10.25, 12.15, 12.3, 12.55, 11.2, 11.35, 11.95, 11.85, 12.95, 
    13.7, 14.1, 15.55, 15.6)), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -20L), groups = structure(list(
    SITE_ID = c("BI22_01", "BI22_01", "BI22_01"), DATE = structure(c(19138, 
    19145, 19151), class = "Date"), .rows = structure(list(1:3, 
        4:15, 16:20), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -3L), .drop = TRUE))

Thanks a million!

Is this what you mean?

library(tidyverse)

dd <- structure(list(SITE_ID = c("BI22_01", "BI22_01", "BI22_01", "BI22_01", 
                                 "BI22_01", "BI22_01", "BI22_01", "BI22_01", "BI22_01", "BI22_01", 
                                 "BI22_01", "BI22_01", "BI22_01", "BI22_01", "BI22_01", "BI22_01", 
                                 "BI22_01", "BI22_01", "BI22_01", "BI22_01"), DATE = structure(c(19138, 
                                                                                                 19138, 19138, 19145, 19145, 19145, 19145, 19145, 19145, 19145, 
                                                                                                 19145, 19145, 19145, 19145, 19145, 19151, 19151, 19151, 19151, 
                                                                                                 19151), class = "Date"), DEPTH_m = c(0.1, 0.5, 1, 0.1, 0.5, 1, 
                                                                                                                                      1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 0.1, 0.2, 0.4, 0.6, 0.8), 
                     TEMP_C = c(16.115, 15.87, 13.86, 15, 14.9, 14.65, 14.425, 
                                14.3, 14.005, 13.995, 13.91, 13.885, 13.84, 13.785, 13.665, 
                                22.805, 22.605, 22.04, 21.565, 21.085), COND_US = c(0.108, 
                                                                                    0.1075, 0.107, 0.119, 0.119, 0.1195, 0.1195, 0.1195, 0.1195, 
                                                                                    0.1195, 0.1195, 0.1195, 0.12, 0.12, 0.12, 0.1315, 0.1315, 
                                                                                    0.7115, 0.1315, 0.13), DO_MG_L = c(9.815, 9.245, 8.985, 8.885, 
                                                                                                                       8.875, 8.895, 8.875, 8.835, 8.87, 8.845, 8.875, 8.83, 8.805, 
                                                                                                                       8.835, 8.765, 9.415, 9.26, 9.08, 8.95, 8.76), PH = c(7.825, 
                                                                                                                                                                            7.725, 7.73, 7.24, 7.58, 7.56, 7.545, 7.545, 7.535, 7.53, 
                                                                                                                                                                            7.53, 7.52, 7.51, 7.51, 7.515, 7.34, 7.335, 7.325, 7.32, 
                                                                                                                                                                            7.315), TURB_NU = c(7.05, 7.8, 7.35, 9.6, 9.15, 9.95, 9.55, 
                                                                                                                                                                                                10.25, 12.15, 12.3, 12.55, 11.2, 11.35, 11.95, 11.85, 12.95, 
                                                                                                                                                                                                13.7, 14.1, 15.55, 15.6)), class = c("grouped_df", "tbl_df", 
                                                                                                                                                                                                                                     "tbl", "data.frame"), row.names = c(NA, -20L), groups = structure(list(
                                                                                                                                                                                                                                         SITE_ID = c("BI22_01", "BI22_01", "BI22_01"), DATE = structure(c(19138, 
                                                                                                                                                                                                                                                                                                          19145, 19151), class = "Date"), .rows = structure(list(1:3, 
                                                                                                                                                                                                                                                                                                                                                                 4:15, 16:20), ptype = integer(0), class = c("vctrs_list_of", 
                                                                                                                                                                                                                                                                                                                                                                                                             "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
                                                                                                                                                                                                                                                                                                                                                                                                             ), row.names = c(NA, -3L), .drop = TRUE))
dd %>%
    group_by(SITE_ID, DATE) %>% 
    group_nest() %>% 
    mutate(plot = map(data, ~ {
        ggplot(.x) +
            geom_line(aes(x=TEMP_C, y=DEPTH_m)) +
            scale_y_reverse()
    })) %>% 
    pull(plot)
#> [[1]]

#> 
#> [[2]]

#> 
#> [[3]]

Created on 2022-08-29 with reprex v2.0.2

2 Likes

Thanks so much for your help! Is there a way to add the SITE ID and Date to the title of the graph?

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