Reputation: 53
I am trying to plot a multiple line graph, with different colors and the legend in R, without much success... I have a data frame named DatProv <- (DatProv$Date, DatProv$Test1, DatProv$Test2, DatProv$Test3) as follows (simplified):
I would like to plot in x-axis the Date (which is in date format %Y-%m-%d, but if possible get the months (feb, mar, apr, may)) against Test1, Test 2 and Test 3 (they are integers).
I tried the ggplot but didn't get at all what I expected which is basically 3 time series showing the evolution of the three tests against the date.
Any help would be much appreciated! Thanks!
Upvotes: 0
Views: 51
Reputation: 3864
Please don't share images of data tables as part of the question. In the future you can use the dput() function to generate a sharable version of your data. For example: dput(DatProv)
Does this code below produce what you're looking for?
library(tidyverse)
DatProv %>%
pivot_longer(cols = starts_with('Test'),
names_to = 'Test',
values_to = 'Value') %>%
ggplot(aes(lubridate::date(date), Value, color = Test, group = Test)) +
geom_line() +
scale_x_date(date_breaks = '1 month',
date_labels = '%b')
Upvotes: 1