I have a function to plot one fixed column against 49 columns in a data frame (gbTSS), each column has 3713 observations. To do that, I built a function:
Tplots = function(BoR,name){
pl = ggplot(
gbTSS,
aes(x = {{BoR}}, y= TSS)
) +
geom_point(color = 'deepskyblue4',size = 0.4 ,alpha = 0.15) +
#scale_x_continuous(trans = "log10") +
facet_grid(.~ Water_body_type + paste(name,"band")) +
theme(legend.position = "none") +
labs(
x = "Reflectance",
y = "TSS (mg/L)"
) +
stat_cor(r.digits = 2, method = "spearman",aes(label = paste(..r.label..,
..p.label.., sep = "~`,`~")),cor.coef.name = c("rho"), position = "identity",
color = 'deepskyblue4', size = 4.3)
ggsave(filename = paste(name,".png") ,device = 'png', path = "./Figures", dpi=500)
return(pl)
}
where "BoR" is the 3713 elements from each column and "name" is the name of each column. The problem with this function is that I have to call it 49 times (the number of columns). The function displays a scatter plot and saves it to my files in each call.
I'm trying to modify the function; I want the same result without calling the function each time I want to change the column.
I tried use do.call, so I created two lists with the names of the columns ("name" in the function), and with elements of each column ("BoR in the function)
band_names = list(colnames(gbTSS[12:ncol(gbTSS)]))
k = nrow(gbTSS)
nr = list(1:49)
n = 49
bn = function(band){
rept = list(rep(band_names[[1]][band], each = k))
}
bns = do.call(bn,nr)
bns = lapply(bns, function(x) split(unlist(x), cut(seq_along(unlist(x)), n, labels = F)))
xd=c(bns, band_names)
where xd is the list to call in the do.call, but I'm having this error:
Error in `recycle_columns()`:
! Tibble columns must have compatible sizes.
• Size 49: Column `paste(BoR, "band")`.
• Size 3713: Column `Water_body_type`.
ℹ Only values of size one are recycled.
The error is pretty clear, and I know there is a mismatch in the length of the lists. The thing is that I don't know how to modify the function in order to get what I want.
I'll appreciate any help.