I am trying to layer multiple data frames in one line plot, with x = index
, y = values
. The 8 data.frames I work with come in this format (index and value) and are several hundred rows long:
Values
2306 0.000000
2307 1.004711
Because the data frames don't all have the same size, I am also trying to resize the data sets by converting them into percent (index/total number of values)*100, should I place this in the plotting code or should I better convert the data sets before plotting?
Hope the hivemind of StackOverflow can help an R newbie
No hive mind required:
If you want them all in a single plot, it would be easiest if you "stack" the data frames first and include a column that identifies which original data frame the data came from.
First create fake data. The code below creates a list containing eight data frames. We'll assume this is where we start after we've read in the data. If you're reading in your data frames from separate files (csv files, for example), just read them all into a single list and then use
bind_rows
to stack them:Plot using ggplot. We use
source
(the name of the original data frame) as thecolour
aesthetic:Or, if you want to normalize
index
to span the same range for each data frame:UPDATE: In response to your comment, if you have the data frame already, you could do this to get a single data frame:
But you must have read the data into R at some point and you can take care of this type of processing when you read the data files into R.