0
votes

I have a three column data source from a csv. I have used read.csv to import it into R. The data set looks like the following.

      LRV Variance      Date
   1: 101    4.541  9/1/2020
   2: 101    0.000  9/2/2020
   3: 101    8.992  9/3/2020
   4: 101   23.233  9/4/2020
   5: 101    6.347  9/5/2020
  ---                       
1696: 150    4.516 9/30/2020
1697: 150    0.000 10/1/2020
1698: 150    0.000 10/2/2020

I am trying to generate a line graph with the x axis as Date, Y axis as Variance, and the plot points/line representing each LRV.

I tried using ggplot to do this.

ggplot(mydata, aes(Date, Variance, colour = LRV)) + geom_point() + geom_path()

But this ends up plotting a line within each day instead of each LRV. Here's the output

1

1 Answers

0
votes

Try this approach using geom_line() instead (I have used your data so there are empty dates). Here the code using ggplot2:

library(ggplot2)
#Format date
df$Date <- as.Date(df$Date,'%m/%d/%Y')
#Code
ggplot(df, aes(Date,Variance, color=factor(LRV),group=factor(LRV)))+
  geom_line()+
  scale_x_date(date_labels = '%Y-%m_%d')

Output:

enter image description here

Some data used:

#Data
df <- structure(list(LRV = c(101, 101, 101, 101, 101, 150, 150, 150, 
151, 151, 151), Variance = c(4.541, 0, 8.992, 23.233, 6.347, 
4.516, 0, 0, 6, 9, 15), Date = c("09/01/2020", "09/02/2020", 
"09/03/2020", "09/04/2020", "09/05/2020", "09/30/2020", "10/01/2020", 
"10/02/2020", "09/30/2020", "10/01/2020", "10/02/2020")), row.names = c(NA, 
-11L), class = "data.frame")