1

I have a time series of surface contacts (in df1) made by a person, as well as the temperature in the room (measured every few seconds and stored in df2). I'd like to plot temperature over Dev.Date.Time and below the y-axis (or in a facet plot), the Surface they touched with respect to Dev.Date.Time. The problem I have is that temperature is recorded every ten seconds whereas the movements happen more often.

head(df1)
  ActivityID CareType HCWType Orientation    Surface       Date     Time       Dev.Date.Time SurfaceCategories
1         01       IV    RN01  leftFacing AlcOutside 2019-08-03 11:08:01 2019-08-03 11:08:01       HygieneArea
2         01       IV    RN01  leftFacing         In 2019-08-03 11:08:12 2019-08-03 11:08:12                In
3         01       IV    RN01  leftFacing       Door 2019-08-03 11:08:12 2019-08-03 11:08:12        FarPatient
4         01       IV    RN01  leftFacing       Door 2019-08-03 11:08:18 2019-08-03 11:08:18        FarPatient
5         01       IV    RN01  leftFacing      Other 2019-08-03 11:08:22 2019-08-03 11:08:22        FarPatient
6         01       IV    RN01  leftFacing      Table 2019-08-03 11:10:26 2019-08-03 11:10:26       NearPatient

df2<-data.frame(sample(32:35,100,replace=T),Dev.Date.Time=seq(
     from=as.POSIXct("2012-1-1 0","%Y-%m-%d %H", tz="UTC"),
     to=as.POSIXct("2012-1-3 23", "%Y-%m-%d %H", tz="UTC"),
     by="10 seconds")
   )

I can plot the temperature but can't work out how to plot a string of characters over time. Any thoughts are much appreciated!

ggplot(df, aes(x=Dev.Date.Time, y=Temperature)) +
    geom_line() + 
    geom_text()

EDIT:

using:

ggplot(df2, aes(x=Dev.Date.Time, y=Temperature)) +
    geom_line() + 
    geom_text(data=df1, aes(label=Surface))

enter image description here

HCAI
  • 2,213
  • 8
  • 33
  • 65

2 Answers2

4
ggplot(df, aes(x = Dev.Date.Time, y = Temperature)) + 
  geom_line() + 
  geom_text(aes(label = Surface))

check ?geom_text for other arguments

teunbrand
  • 33,645
  • 4
  • 37
  • 63
Yazid
  • 101
  • 1
  • 4
  • Thank you for your help. I had forgotten I had measured the temperature at a faster rate than the movements so they are two different data frames. I apologise for this. – HCAI Aug 12 '19 at 20:00
2

Please try the following:

ggplot(df, aes(x=Dev.Date.Time, y=Temperature, label=Surface)) +
    geom_line() + 
    geom_text()

You can also add check_overlap = TRUE to geom_text so that your labels don't overlap.

EDIT:

If you have two data frames with same variables, it should be enough to do the following:

ggplot(df2, aes(x=Dev.Date.Time, y=Temperature)) +
    geom_line() + 
    geom_text(data=df1, aes(label=Surface))

If your df1 is missing temperature, create a dummy Temperature variable. If you want to show the labels a bit above x-axis, set the Temperature to min of df2$Temperature.

df1$Temperature <- min(df2$Temperature)
Arienrhod
  • 2,451
  • 1
  • 11
  • 19
  • Thank you for your help. I had forgotten I had measured the temperature at a faster rate than the movements so they are two different data frames. I apologise for this. – HCAI Aug 12 '19 at 20:00
  • The new edit isn't working for me saying Error in FUN(X[[i]], ...) : object 'Surface' not found. So even if I create a dummy variable in df2 called Surface, it still doesn't show the labels. I'm trying instead to merge the data frames: https://stackoverflow.com/questions/14102498/merge-dataframes-different-lengths. I'll post an update if this works. Thanks again for your help. – HCAI Aug 13 '19 at 12:28
  • oh, my bad. i've edited my edit to fix that. i've just flipped the data frames. – Arienrhod Aug 13 '19 at 12:31
  • It still gives the same errors even if I switch them around, hmmm. – HCAI Aug 13 '19 at 12:36
  • That's weird. Check your data frames. This works for me: `df2<-data.frame(Temperature=sample(32:35,208,replace=T),Dev.Date.Time=seq( from=as.POSIXct("2012-1-1 0","%Y-%m-%d %H", tz="UTC"), to=as.POSIXct("2012-1-1 23", "%Y-%m-%d %H", tz="UTC"), by="400 sec") )` `df1<-data.frame(Temperature=sample(32:35,83,replace=T),Dev.Date.Time=seq( from=as.POSIXct("2012-1-1 0","%Y-%m-%d %H", tz="UTC"), to=as.POSIXct("2012-1-1 23", "%Y-%m-%d %H", tz="UTC"), by="1000 sec"), Surface="test" )` – Arienrhod Aug 13 '19 at 12:42
  • `ggplot(df2, aes(x=Dev.Date.Time, y=Temperature)) + geom_line() + geom_text(data=df1, aes(label=Surface))` – Arienrhod Aug 13 '19 at 12:42
  • This works as predicted. Hmmm. So it looks like you have Temperature in both data frames. One of the temperatures could be dummy though I guess? The other slight nuance is that I'd like to write those labels just above or below the x axis. OR even in a graph parallel to the bottom of the y axis. What do you think? – HCAI Aug 13 '19 at 12:47
  • There must be something up with my data frames... but I can't see what yet. – HCAI Aug 13 '19 at 12:47
  • 1
    Oh, ok - I've just realised I was to quick for my own good and just assumed you have `Temperature` in both data frames. In that case, you can def add a dummy temperature in `df1`. If you want the labels to be just above the `x-axis`, set them to a value that's slightly below the `min` of `df2$Temperature`. My edit is above. – Arienrhod Aug 13 '19 at 13:02
  • Thank you very much for your help. I will work out why the timezones in my dframes are making the plots at different times and how to make the labels vertical, this should fix it. – HCAI Aug 13 '19 at 19:54