doloresafwat
doloresafwat

Reputation: 53

Multiple line plot in R from a data frame

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):

Date.          Test1.       Test 2.      Test 3
2020-02-01.     1.            0.           1
2020-03-31.     1.            1.           1
2020-04-05.     5.            3.           3
2020-05-01.     20.           23.          17

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

Answers (1)

Seth
Seth

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

Related Questions