0

I have a time series data file which has the concentration of 4 metabolites A, B, AE and E over time. I have many data files of this type (around 100). I want to plot the time series for all the four metabolites in all the files in one graph. Each metabolite is assigned a specifc color.

I compiled the below code, however it plots data in only one file (the last one). I think that is because when ever I call ggplot() it creates a new plot. I've tried creating the plot outside of the four loop and it didn't work.

p = NULL

for(i in 1:length(filesToProcess)){
  fileName = filesToProcess[i]

  fileContent = read.csv(fileName)
  #fileContent$Time <- NULL

    p <- ggplot()+ 
    geom_line(data = fileContent, aes(x = Time, y = A, color = "A"), size =0.8) +
    geom_line(data = fileContent, aes(x = Time, y = B, color = "B"), size =0.8)  +
    geom_line(data = fileContent, aes(x = Time, y = AE, color = "AE"), size =0.8)  +
    geom_line(data = fileContent, aes(x = Time, y = E, color = "E"), size =0.8)  +
    xlab('Time') +
    ylab('Metabolite Concentration')+
    ggtitle('Step Scan') +
    labs(color="Metabolites")

}
plot(p)

Below is the graph enter image description here

Sample files can be found here

3 Answers 3

3

I usually take the following approach (untested, for lack of a reproducible example)

read_one <- function(f, ...){
  w <- read.csv(f, ...)
  m <- reshape2::melt(w, id = c("Time"))
  m$source <- tools::file_path_sans_ext(f) # keep track of filename
  m
}

plot_one <- function(d){
  ggplot(d, aes(x=Time, y=value)) + 
    geom_line(aes(colour=variable), size = 0.8) +
    ggtitle('Step Scan') +
    labs(x = 'Time', y = 'Metabolite Concentration', color="Metabolites")
}

## strategy 1 (multiple independent plots)

ml <- lapply(filesToProcess, read_one)
pl <- lapply(ml, plot_one)

gridExtra::grid.arrange(grobs = pl)

## strategy 2: facetting

m <- plyr::ldply(filesToProcess, read_one)
ggplot(m, aes(x=Time, y=value)) + 
  facet_wrap(~source) +
  geom_line(aes(colour=variable), size = 0.8) +
  ggtitle('Step Scan') +
  labs(x = 'Time', y = 'Metabolite Concentration', color="Metabolites")
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for the answer. I'm trying to wrap my head around your solution. It looks bit complicated for me. Also I've included few sample files.
0

Since plot(p) is outside the loop, it will only plot the last graph produced. Move plot(p) inside the loop.

Note: While the question is somewhat ambiguous, I'm assuming you want one graph per input file.

EDIT: To put all data in a single plot, assuming all your files have same columns in same order.

all_data <- lapply(filesToProcess, read.csv)
fileContent <- do.call(rbind, all_data)

Then you can run the ggplot code exactly as you did above (without a loop).

2 Comments

@Marchand I need one graph for data in all the files.
@Marchand thank you for the suggestion. Yes all the files have the same columns in the same order (Time, A, A|E, B, E). HOwere I tried your method and the plot doesn't look what it should look like. Also I've included few sample files.
0

I think I solved the problem. The answer is bit crude I admit. However if I could initialize the "p" variable outside the for loop it solves the problem.

filesToProcess = readLines("FilesToProcess.txt")

#initializing the variable with ggplot() object
p <- ggplot()

for(i in 1:length(filesToProcess)){
  fileName = filesToProcess[i]
  fileContent = read.csv(fileName)

  p <- p + 
  geom_line(data = fileContent, aes(x = Time, y = A, color = "A"), size =0.8) +
  geom_line(data = fileContent, aes(x = Time, y = B, color = "B"), size =0.8)  +
  geom_line(data = fileContent, aes(x = Time, y = AE, color = "AE"), size =0.8)  +
  geom_line(data = fileContent, aes(x = Time, y = E, color = "E"), size =0.8)

}

p <- p + theme_bw() + scale_x_continuous(breaks=1:20) + 
  xlab('Time') +
  ylab('Metabolite Concentration')+
  ggtitle('Step Scan') +
  labs(color="Legend text")
plot(p)

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.