1

I have a dataset where each row is an object, that is grouped into different alphabetical groups. This data set also has the price and the time the price was set at.

I want to create a line graph where I have time on the x-axis, price on the y-axis, and different colored lines for groups. All in one graph.

  1. Ideally I would want a data set that has different times as columns, groups as rows, and each case would be a price.

  2. How would I account for the missing data? Some groups didn't have their prices calculated on certain days.

Thank you

Sample Data:

structure(list(price = c(540.16, 539.76, 538.82, 539.79, 539.42, 
539.73, 538.57, 539.05, 542.13, 537.85, 540.36, 539.13, 536.19, 
539.43, 537.49, 539.32, 537.37, 539.64, 539.37, 539.09, 537.68, 
539.19, 540.13, 539.29, 539.29, 538.93, 539.32, 538.96, 537.61, 
541.03, 538.29, 538.95, 538.33, 537.46, 538.48, 537.17, 538.87, 
540.32, 539.65, 537.48, 540.5, 539.11, 539.37, 538.78, 539.17, 
537.91, 537.97, 538.79, 538.03, 538.67), Date_YM = c("2020-01", 
"2020-01", "2019-01", "2019-01", "2020-04", "2019-01", "2019-03", 
"2020-01", "2019-04", "2019-03", "2019-03", "2019-02", "2019-05", 
"2020-01", "2020-02", "2019-02", "2020-03", "2019-05", "2019-03", 
"2019-03", "2019-02", "2019-03", "2020-03", "2020-03", "2020-01", 
"2019-01", "2020-06", "2019-06", "2020-05", "2019-02", "2019-03", 
"2019-06", "2020-04", "2020-02", "2020-06", "2019-03", "2019-01", 
"2020-02", "2020-06", "2019-04", "2019-04", "2020-06", "2019-03", 
"2019-01", "2019-02", "2019-01", "2020-01", "2020-02", "2019-01", 
"2019-01"), group = c("A", "A", "A", "A", "A", "A", "A", "A", 
"A", "B", "B", "B", "C", "C", "C", "D", "D", "E", "E", "E", "E", 
"E", "E", "F", "F", "F", "G", "G", "G", "G", "G", "G", "G", "G", 
"G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", "G", 
"H", "H", "H")), row.names = c(NA, -50L), class = "data.frame")
imdinos
  • 49
  • 5

1 Answers1

0

Maybe this can help:

library(ggplot2)
#Plot
ggplot(df, aes(x = factor(Date_YM), y=price,color=group,
               group=group))+
  geom_line()+
  xlab('Date')+
  theme(axis.text.x = element_text(angle = 90))

Output:

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84