I have a dataframe like this:
date_list = seq(ymd('2000-01-01'),ymd('2000-12-31'),by='day')
testframe = data.frame(Date = date_list)
testframe$ABC = rnorm(366)
testframe$DEF = rnorm(366)
testframe$GHI = seq(from = 10, to = 25, length.out = 366)
testframe$JKL = seq(from = 5, to = 45, length.out = 366)
I want to automatize the thing I am doing below. I want to plot each column from 2:4 against the time (Date). The plots should be saved in a form like p_columnname.
p_ABC = ggplot(data = testframe, aes(x = Date, y = ABC)) +
geom_line(color = "grey", size = 1)
p_DEF = ggplot(data = testframe, aes(x = Date, y = DEF)) +
geom_line(color = "grey", size = 1)
p_GHI = ggplot(data = testframe, aes(x = Date, y = GHI)) +
geom_line(color = "grey", size = 1)
p_JKL = ggplot(data = testframe, aes(x = Date, y = JKL)) +
geom_line(color = "grey", size = 1)
I tried to create a loop:
library(ggplot2)
theme_set(theme_gray())
for (i in colnames(testframe[2:ncol(testframe)])) {
paste("p", i, sep = "_") = ggplot(data = testframe, aes(x = Date, y = i)) +
geom_line(color = "grey", size = 1)
}
That does not work! Any suggestions?