r - Creating ggplots in a loop -
i struggling create loop make load of different plots variables in file imported r.
some data in dummydata.csv :
time,a1,a2,a3,a4 1,0.1,0.2,0.1,0.1 2,0.2,0.2,0.2,0.3 3,0.4,0.5,0.3,0.4 4,0.6,0.8,0.4,0.6 5,0.8,0.9,0.6,0.7
basically real data in larger file this, , interested in plotting "time" against each other variable in separate plots, , thought trying loop through more sensible individually writing out each plot!
what tried do:
library("ggplot2") dummydata <- read.csv("dummydata.csv", header = t) columns <- colnames(dummydata[2:5]) for(i in columns){ title <- paste("graph_", i, ".pdf") pdf(title) ggplot(data = dummydata, aes(x=time, y=i)) + geom_point()} dev.off()
clearly doesn't work. i've made few different attempts ggplot (or normal plot function in r) take 1 of variables plotted loop, seemingly can't it.
any advice on try appreciated!
you can (it default plot
function) :
(i in 1:(dim(dummydata)[2]-1)){ title<-paste0("graph_", i, ".pdf") # use paste0 remove blanks pdf(title) plot(x=df[,1],y=df[,i],pch=20,type='l') # add options such axis titles, color, ... dev.off()}
if want use ggplot()
:
for (i in 1:(dim(dummydata)[2]-1)){ title<-paste0("graph_", i, ".pdf") pdf(title) print(ggplot(data = dummydata, aes(x=time, y=dummydata[,i+1]))+geom_point()) dev.off()}
Comments
Post a Comment