I have this working loop with the basic plot function
#my_data is a data.frame defined before
day = c("2016-12-24","2016-12-25", "2016-12-31")
par(mfrow = c(length(day),1))
for (i in 8:length(my_data)){
for (j in 1:length(day)){
plot(x=my_data$Zeit[which(my_data$Datum == day[j])], y=my_data[which(my_data$Datum == day[j]),i], type="l",
main=colnames(my_data[i]), xlab=day[j], ylab=my_units[1,i])
}
}
But instead of the basic plot function, I want to use ggplot. With the search function on this board I found de command "grid.arrange", but it somehow does not work.
Edit: This is my new code with "do.call". Thank you already for this hint.
Edit2: Now it works. I don't understand why, but with the "#Added" lines it does not print out the same plot three times.
for (i in 8:length(my_data)){
for (j in 1:length(day)){
local({ #Added
i <- i #Added
t <- which(my_data$Datum == day[j])
p[[j]] <<- ggplot(my_data[t,], aes(my_data$Zeit[t], my_data[t,i]))+ #Changed to <<-
geom_line()+
labs(x = "", y = my_units[[i]], title = colnames(my_units[i]))+
theme_own()
}) #Added
}
Edit: What I want to do is plot the data for several days (in this example three) in the same "picture". I want to repeat this for all columns (8:length(my_data)).
With this new code and "do.call" I do get three plots like I want, but these plots are all for the same (the last) day. Why are the first two dates overwritten?
Edit2: I found the hint here: for loop with ggplots produces graphs with identical values but different headings
Thank you all for your help :)