GGplot multiple objects from list single plot

Hi Everyone,

New user here, still starting out with R. i have spent a few days searching and reading but am quite stuck.
I have some code that imports multiple csv files into a list;

    setwd("~/R/Projects/Train_Data/")

library(tidyverse)

# creates a list
Trains <- list() 

# creates the list of all the csv files in the directory
listcsv <- dir(pattern = "*.csv") 

#Reads in all the files
for (k in 1:length(listcsv)){
  Trains[[k]] <- read.csv(listcsv[k], header = TRUE, sep = ",", skip = 15)
}

#updates the list to just the Date & Time
for (k in 1:length(listcsv)){
  listcsv[k] <- substr(listcsv[[k]], 36, 51)
}

#create Wagons list from Trains excluding Locomotive's
Wagons <- list()
for (k in 1:length(Trains)){
  Wagons[[k]] <- subset(Trains[[k]], CARCLASS != 'QEL')
  Wagons[[k]] <- subset(Wagons[[k]], CARCLASS != 'QDEL')
}

#updates the lists with the new names
Trains <- set_names(Trains, listcsv)
Wagons <- set_names(Wagons, listcsv)

this creates two lists based off train data from our system, i can hard code the plots to show mutiple lines and facet into different classes.

ggplot(mapping = aes(x = WEIGHT)) + geom_density(data = Wagons[[1]], col = '1') + geom_density(data =  Wagons[[2]], col = '2') + geom_density(data =  Wagons[[3]], col = '3') + facet_grid(facets = vars(CARCLASS))

I am trying to display a single plot and place a line from each object in the list and possibly colour it based off the title of the list.
I have had a play with melt but am unable to get the id to be based off the title of the object from the list, otherwise if there is another option to plot all the lines no matter how may objects are in the list?

Thanks for all you help;
Josh (Intimidated Noob)

Hi!

A simpler approach would be to import your data into a dataframe instead of a list, to give you a working solution, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

not really sure what other info you want. this is what the data looks like, i have lots of .csv that look the same;

 SEQUENCENUMBERTRAIN AEITAG VEHICLETYPE          DATETIMEIN SPEED RAKENUMBER SEQUENCENUMBERRAKE CARCLASS
2                    2  50946     Wagon 4 2021/10/04-10:55:14   0.8          1                  2     QVSA
3                    3  50947     Wagon 4 2021/10/04-10:56:26   0.8          1                  3     QVSA
4                    4  50118     Wagon 4 2021/10/04-10:57:46   0.8          1                  4     QVSA
5                    5  50119     Wagon 4 2021/10/04-10:58:56   0.8          1                  5     QVSA
6                    6  50050     Wagon 4 2021/10/04-11:00:05   0.9          1                  6     QVSA
7                    7  50051     Wagon 4 2021/10/04-11:01:14   0.9          1                  7     QVSA
8                    8  51860     Wagon 4 2021/10/04-11:02:22   0.9          1                  8     QVEA
9                    9  51861     Wagon 4 2021/10/04-11:03:32   0.9          1                  9     QVEA
10                  10  53992     Wagon 4 2021/10/04-11:04:41   0.9          1                 10     QVEB
11                  11  53993     Wagon 4 2021/10/04-11:05:50   0.9          1                 11     QVEB
12                  12  53580     Wagon 4 2021/10/04-11:06:59   0.9          1                 12     QVCA
13                  13  53581     Wagon 4 2021/10/04-11:08:08   0.9          1                 13     QVCA
14                  14  50332     Wagon 4 2021/10/04-11:09:17   0.9          1                 14     QVSA
15                  15  50333     Wagon 4 2021/10/04-11:10:27   0.9          1                 15     QVSA

as far as my example code other than the import above and hard coding the plots from each iteam in the list, im not having any luck.

Hello.
Thanks for providing code and data, but you could take further steps to make it more convenient for other forum users to help you.

You have a attempted to supply data, but how easily can forum users load that data into their sessions ? Will they have to type out the data? Will they have to mess around savings files and reading them in in?

It is usually very straightforward to share data 'as code' in a very convenient way, and this is detailed explicitly in the linked FAQ above, but for re-emphasis..

Share data as code, rather than a table of data that forum users can not conveniently copy and paste into R. Rather , use tools such as the library datapasta, or the base function dput() to share a portion of data in code form, i.e. that can be copied from forum and pasted to R session.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.