1

best wishes for the new year.

I have a question...I would like to create a loop to generate multiple plots from multiple dataframes for several designated factor names in r.

I have different dataset with the same fish species. There are more than 50 species (plaice, flounder etc.) For each of these species I have different datasets with different data for each set, but the species used are the same. For each of these species I want to create the same ggplots and other files. Now I made the scipt so that when I fill in a species name in:

###########################
USEDSPECIES <- "Plaice"
###########################

I can run the scripts all at once to generate the plots etc. I want. The only down side is that I have to write down each species and run the whole thing like 50 times... Is there a way for me to make a kind of loop?

In the past I created several loop constructions like:

MaxValue <- rep(FALSE, length(data$Column))
tags <- unique(data $FishID)

for(i in 1:length(tags)){
  t.ss <- which(data $FishID == tags[i])
  MaxValue[t.ss][which(data$Column[t.ss] == max(data $Column[t.ss], na.rm=T))] <- TRUE
}

Only this way only one dataframe is used, in stead of multiple without indicating a factor name (as species) used.

Thank you in advance!

Suusie
  • 149
  • 9
  • create a for-loop with the vector of `unique` species as variable-input? – Wimpel Jan 02 '23 at 12:54
  • Dear Wimpel, do you have an example code for this? – Suusie Jan 02 '23 at 12:55
  • 2
    No, I'm sorry. SO is not a 'write me some code'-service. You have to show some effort yourself. At the least you should provide some sample data (using `dput()`) for people to work with. See: https://stackoverflow.com/a/5963610/6356278 – Wimpel Jan 02 '23 at 12:58
  • 1
    It might probably save you a lot of effort later on to compile all your data into one *tidy* dataframe, e. g. one column for species (plaice, flounder, ...), another for property (body length, weight, ...), the third containing the actual value. That way you can take advantage not only of ggplot's `facet_wrap`/`facet_grid` but of `group`ing, `filter`ing, `summarize`ing and the rest of the `tidyverse`. Search stackoverflow for plenty of solutions to automate file import and dataframe combination. – I_O Jan 02 '23 at 13:14
  • @Wimpel, I know I created multiple loop constructions, with factors. However, these were from one 1 dataframe. I edited my question with examples – Suusie Jan 02 '23 at 13:38
  • I suggest going the path that @I_O suggests.. Put your data.frames in a list, bind them together into one tidy data.frame, and take it from there. – Wimpel Jan 02 '23 at 14:54
  • Thank you @I_O and Wimpel, I'll have a go with it! – Suusie Jan 02 '23 at 15:13

1 Answers1

1

An example of the advantages of combining the scattered dataframes and working on from there:

library(dplyr)

## example data frames for Plaice and Flounder:
df_plaice <- data.frame(FishID = rep('Plaice', 8), Weight = sample(500:800, 8))
df_flounder <- data.frame(FishID = rep('Flounder', 8), Weight = sample(500:800, 8))

## row-bind single data frames:
all_the_fish <- 
  list(df_plaice, df_flounder) |> Reduce(f = rbind)
> all_the_fish
     FishID Weight
1    Plaice    553
2    Plaice    776
## ...
15 Flounder    580
16 Flounder    794
## species-wise aggregates:
all_the_fish |>
  group_by(FishID) |>
  summarize(MaxWeight = max(Weight, na.rm = TRUE),
            AverageWeight = mean(Weight, na.rm = TRUE)
  )
# A tibble: 2 x 3
  FishID   MaxWeight AverageWeight
  <chr>        <int>         <dbl>
1 Flounder       794          674.
2 Plaice         776          620.

plotting:

all_the_fish |>
  ggplot() +
  geom_boxplot(aes(Weight)) +
  coord_flip() +
  facet_wrap(~ FishID)

enter image description here

I_O
  • 4,983
  • 2
  • 2
  • 15
  • 1
    Thank you very much for your effort and help! I did consider something like this in the beginning..but changed my plans. Now I see that it might be way better ^^'.. – Suusie Jan 02 '23 at 15:21
  • I learned it the hard way ... having to mill out diagnostic plots for dozens and dozens of factor combinations actually encouraged me to tackle the learning treshold for R back then. – I_O Jan 02 '23 at 15:30
  • Thats the way to do it :) – Suusie Jan 02 '23 at 17:56