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?
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:
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.