0

I'm trying to plot multiple layers (around 100) on the same plot using ggplot2. To do that I create a dataframe which contains the data assigned according to 'i'.

rm(list = ls())
# file directory
setwd("E:/stage/Flight_Data/Flight_Data/0200")

library(ggplot2)

# to read txt files
liste_fichier <- list.files("./", pattern=".txt")
n <- length(liste_fichier)

times = list();
altitudes = list();
dataframes = list();

# loop to read the files
for (i in 1:n) {
  fichier = liste_fichier[i]
  smp <- read.table(fichier, header=FALSE, sep="", skip=3, fill=TRUE)
  names(smp) <- c("Date","Time", "Latitude", "Longitude", "Baro.Altitude", 
                  "Radio.Altitude", "Pressure", "A340.static.T", "A340.air.speed",
                  "A340.ground.speed", "zonal.wind", "meridian.wind", "ozone.vmr",
                  "H2O.static.T", "relative.humidity", "RH.validity", "RH.accuracy",
                  "H2O.mmr","CO.vmr", "NOy.vmr", "NO.vmr", "NOx.vmr",
                  "NOy.uncertainty", "NOy.validity")
  smp$ultratime <- strptime(paste(smp$Date, sprintf("%06d",strtoi(smp$Time)), sep=" "), 
                            format="%Y%m%d %H%M%S")

  dataframes[[i]] <- data.frame(smp$ultratime,smp$Baro.Altitude)

  # Here I want to plot all the files in the same graph, first i want to try only 2

  print(ggplot(NULL, aes(x=smp$ultratime, y=smp$Baro.Altitude)) +
     geom_line(data=dataframes[[1]])+geom_line(data=dataframes[[2]]))
}

I'm able to plot a single layer for dataframes[[1]], but when I add more, I get the following error:

Error in dataframes[[2]] : subscript out of bounds
gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
AAa
  • 1
  • 1
  • 1
    This is not really is easy with no reproducible example. But here, in your first loop, dataframes[[2]] doesn't exist – Bambs Jul 31 '17 at 12:48
  • Subscript out of happens exactly if you access an area of your list, which is not defined, as in your case dataframes[[2]] is in the first loop. – tanktoo Jul 31 '17 at 12:57

0 Answers0