0

I have a series of csv files in one directory. The csv files share the same format.

I wish to iterate through all of the csv files and plot a graph per csv file.

The (tested) function to plot the graph is as follows:

ggplot(aes(x = Count_norm, y = duration_in_traffic), data = tmp) + geom_point(aes(color = id)) + geom_smooth(aes(color = id), method= "lm", se = F, formula=y ~ poly(x, 3, raw=TRUE))

I have attempted to iterate over the csv files and then plot each by:

setwd("/Users/testdata/")
filenames = dir(pattern="*.csv")
for (i in 1:length(filenames)) { tmp <-read.csv(files[i]) ggplot(aes(x = Count_norm, y = duration_in_traffic), data = tmp) + geom_point(aes(color = id)) + geom_smooth(aes(color = id), method= "lm", se = F, formula=y ~ poly(x, 3, raw=TRUE))}

I have used tmp as the data source, is this incorrect?

aosmith
  • 34,856
  • 9
  • 84
  • 118
LearningSlowly
  • 8,641
  • 19
  • 55
  • 78
  • 1
    That should be fine. Are you having a problem? The way this is printed, you need a semi colon after the `read.csv()` function. Also, I often wrap my `ggplot()` function in the `print()` function within a loop, though I am not sure this is necessary. – lmo Apr 11 '16 at 18:26
  • Good catch on the semicolon post read.csv()! It does indeed run but I don't get any plots. I am using RStudio – LearningSlowly Apr 11 '16 at 18:30

1 Answers1

1

Put your ggplot inside a "print()". This is a requirement when plotting inside a for loop.