I'm using the grid
and grid.Extra
packages to have multiple ggplots on one grid, however, I need to make 28 ggplots in total, each one with a different y variable. Obviously the loop to make and save them as a variable is simple enough, but I need to save each plot as a different object. Currently I just have 28 of the same plot code with only the axis changed, but I know that there's a better way.
My minimum code example:
dattn <- ggplot()+
geom_boxplot(
data = dat,
mapping = aes(x = site, y = tn)
)
My current loop heading (not sure if it makes any difference):
for (i in dat[, 10:12])
I need to switch the y value to tp
and tss
and save those ggplots as dattp
and datss
. The columns of the variables are 10, 11, and 12 in that order.
While doing research for this problem, I came across this question Dynamically naming variables in a loop to create 30 ggplots which is very similar to my problem, however it uses pipe functions and I have no idea how to use them whereas I have some idea of using the for loop. If anyone thinks that would work better I'd be grateful to be told so.
I have also tried the code paste("dat", i, sep="") <- ggplot...
specifically from the question Save ggplot objects in loop but that gives me an error message target of assignment expands to non-language object
and again uses pipe functions.
I'll update my question with any more necessary info as it's needed. Thanks in advance.
The reason I requested help using for loops is because I also need to subset my data based on location ("farm" in the data) and I figured once I could change the text of a loop I would be able to change how I subset the data and use nested loops.
Data using dput(head(dat))
:
structure(list(year = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("2018",
"2019"), class = "factor"), month = structure(c(4L, 5L, 5L, 6L,
7L, 7L), .Label = c("1", "2", "3", "4", "5", "6", "7", "9", "10",
"11", "12"), class = "factor"), day = c(24L, 18L, 30L, 25L, 6L,
19L), farm = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("ARR",
"Car", "CAR", "Mur", "Muz", "PBR", "Pre", "PRE", "Sch", "SCH",
"Sim", "SIM", "STU"), class = "factor"), treat = structure(c(1L,
1L, 1L, 1L, 1L, 1L), .Label = c("CC", "Con"), class = "factor"),
tss = c(1955, 3540, 4893.3, 410, 3357.5, 1836), tn = c(17,
32.8, 7.26, 5.91, 16.1, 16.7), tp = c(4.35, 10, 49.5, 3.57,
9.79, 11.1), dis = c(8178, 184232, 401364, 1113947, 10728,
21869), tss.1 = c(1.576347171, 64.30227415, 193.6414217,
45.03046056, 3.551344392, 3.958763937), tn.1 = c(0.013707367,
0.595795082, 0.28729829, 0.649097614, 0.017029529, 0.036008365
), tp.1 = c(0.003507473, 0.181644842, 1.958851976, 0.392094498,
0.010355223, 0.023933704), site = structure(c(1L, 1L, 1L,
1L, 1L, 1L), .Label = c("Car CC", "Mur CC", "Muz CC", "Pre CC",
"Sch CC", "Sim CC", "CAR CC", "ARR CC", "PBR CC", "PRE CC",
"STU CC", "Car Con", "Mur Con", "Muz Con", "Pre Con", "Sch Con",
"Sim Con", "SCH Con", "PRE Con", "ARR Con", "PBR Con", "SIM Con",
"STU Con"), class = "factor")), row.names = c(NA, 6L), class = "data.frame")
And some formatting code:
# Set variables as factors
cols <- c("year", "month")
dat[cols] <- lapply(dat[cols], as.factor)
# Set dat$site to combine farm and treat
dat$site <- paste(dat$farm, dat$treat)
# Sets the sites in order instead of aphabetically.
# unique() is needed else Error: duplicates
dat$site <- factor(dat$site, levels =unique(dat$site))
I'm not sure if that helps but it was what was suggested to me.