0

I'm trying to develop a loop to plot every column from a dataset called chr, with the code I'm using, a for loop, conditioned by some criteria, to save plots in a list

desired_length <- 8
plot_list <- vector(mode = "list", length = desired_length)
for(i in 3:10) {
  {lot_list[[i-2]] <- ggplot(chr) +
    geom_point(data = chr, aes(x = chr[,1], y = chr[,i], color = ponto),
               size = 2) +
    geom_hline(yintercept = as.double(cla[1,i-1]), size = 2, linetype = "dashed", color = "black") +
    ylim(0,max(chr[,i], na.rm = TRUE)) +
    labs(title = glue("Resultados analíticos no corpo receptor do empto para {(colnames(chr))[i]}"),
         subtitle = glue("Padrão de c3 ={cla[,i-1]} mg/L"), y = glue("{(colnames(chr))[i]} (mg/L)"), x = "Período de análise", caption = glue("Número de amostras = {sum(!is.na(chr[,i]))}")) +
    theme_minimal()
  }
}

if I save if manually, like, for example:


plot_list[[1]] <- ggplot(chr) +
  geom_point(data = chr, aes(x = chr[,1], y = chr[,3], color = ponto),
             size = 2) + ...

it plots perfectly when i run

plot_list[[1]]

plot as expected

but when i run the plot after running the loop, also with plot_list[1], it displays that all values are missing and only shows this plot, with a this Warning message: Removed 142 rows containing missing values (geom_point).

plot with missing values

What could've gone wrong? thanks in advance for helping me

Tung
  • 26,371
  • 7
  • 91
  • 115
  • 1
    I assume it is not due to the typo in line 3? "{lot_list[[i-2]]" – stefanH Jul 10 '20 at 19:17
  • 1
    Welcome to stack overflow. It’s easier to help if you make your question reproducible: include a minimal dataset in the form of an object for example if a data frame as df <- data.frame(…) where … is your variables and values or use dput(head(df)). These links should be of help: [mre] and [ask] – Peter Jul 10 '20 at 19:18
  • Does this answer your question? https://stackoverflow.com/a/31994539/1968 – Konrad Rudolph Jul 10 '20 at 20:39

0 Answers0