Home > Enterprise >  R: How to ggplot two time series dataframes, one of which contains non-numerical labels and slightly
R: How to ggplot two time series dataframes, one of which contains non-numerical labels and slightly

Time:09-16

I have 2 data frames:

df1 <- setNames(data.frame(c(as.POSIXct("2022-07-29 00:00:00","2022-07-29 00:05:00","2022-07-29 00:10:00","2022-07-29 00:15:00","2022-07-29 00:20:00")), c(1,2,3,4,5)), c("timeStamp", "value"))
df2 <- setNames(data.frame(c(as.POSIXct("2022-07-29 00:00:05","2022-07-29 00:05:05","2022-07-29 00:20:05")), c("a","b","c")), c("timeStamp", "text"))

I want to plot them, so as to to have the main graph be a numerical y scale geom_point, and then collate in the second dataframe with the labels (a,b,c) at the correct timeStamps on the continuous time series x axis.

ggplot()   
  geom_point(data=df1, aes(x=timeStamp, y= value))  
  geom_text(data=df2, aes(x=timeStamp, y= text))

The difficulty I think lies in the fact that the timeStamps do not perfectly match up, and I keep getting returned with "Error: Discrete value supplied to continuous scale". Can anybody please offer some advice here?

The end result should look something like this (this an example from a much larger dataframe) enter image description here

  • Related