I have two dataframes, each having one hundred 'x' and 'y' coordinates I wish to plot together on a single line plot. I can get the lines to plot, but am running into trouble trying to colour the lines and add a legend to the graph.
From my understanding it might be better to combine the two dataframes into one and add a column with the two series names, but for now it's best if the two are separate. I can replicate the problem using the mtcars datasets.
It seems strange that the original code I used would not work, since my understanding is ggplot simply needs descriptors inside aes() to tell it what to plot.
The image you presented looks like a solution but the code does not work; when running I get the following error:
Inside the aes() function you map variables to aesthetic elements, in your code you were mapping the colour aesthetic to a character vector called "blue". This is different than assigning the blue color to the line geom, which you could do outside the aes() function.
In order to help you with your issue, we would need a reprex that actually shows your problem maybe with a sample of your data instead of mtcars
So just to circle back, I tried the above solution on my original data with some different values and got it to plot a yellow and green line successfully (instead of red and blue). I also noticed:
when defining the colours vector in my code, I first used colors=c(...) instead of COLORS=c(...) and got errors; I'm assuming it's because colors (in lowercase) is a protected command in R
The label of the two series are still the y-values in the plot. Is there a way to rename these "Initial" and "Offset" in the plot legend only, or would renaming the series in my dataframe header be the easiest way?
Would it be easier to combine the two dataframes into one, then add a column (named 'condition') so that each x/y has a named condition (either inital or offset) and then have ggplot use the single dataframe and simply differ the colors by condition.