0
ggplot(R3L12, aes(x=Time, y=HeartRate)) +
  geom_point() +
  geom_smooth()

This is the output of dput(head(R3L12, 20))

structure(list(Date = c("2015-05-23", "2015-05-23", "2015-05-23", 
"2015-05-23", "2015-05-23", "2015-05-23", "2015-05-23", "2015-05-23", 
"2015-05-23", "2015-05-23", "2015-05-23", "2015-05-23", "2015-05-23", 
"2015-05-23", "2015-05-23", "2015-05-23", "2015-05-23", "2015-05-23", 
"2015-05-23", "2015-05-23"), Time = c("07:25:00", "07:40:00", 
"07:45:00", "09:10:00", "11:45:00", "11:55:00", "12:05:00", "12:35:00", 
"12:45:00", "13:30:00", "13:40:00", "13:45:00", "13:55:00", "14:00:00", 
"14:05:00", "14:10:00", "14:20:00", "14:25:00", "14:30:00", "14:35:00"
), Turtle = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("R3L1", "R3L11", 
"R3L12", "R3L2", "R3L4", "R3L8", "R3L9", "R4L8", "R8L1", "R8L4", 
"R8NAT123"), class = "factor"), HeartRate = c(7.56457, 6.66759, 
17.51107, 9.72277, 19.44553, 13.07674, 28.115, 14.99467, 17.16947, 
40.40479, 37.76642, 29.98933, 43.5329, 49.61471, 47.74245, 44.10196, 
21.35316, 44.68609, 49.25255, 29.98933)), row.names = c(NA, 20L
), class = "data.frame")

Here is the code of a plot I tried to create. I'm confused because when I plot it, the geom_smooth doesn't show up. Time is a factor variable, and HeartRate is a double. I'm new to R so let me know if you need more information!

enter image description here

Cassidy
  • 395
  • 1
  • 11
  • 6
    Your `Time` value is not in a numerical format. Maybe you should check its format and convert it either as a numerical or a date format. If this is not working, please provide a reproducible example of your dataset. – dc37 Feb 28 '20 at 14:24
  • @dc37 I'm not exactly sure how I would do that since my data set is very big. – Cassidy Feb 28 '20 at 14:34
  • 1
    Can you post sample data? Please edit **the question** with the output of `dput(head(R3L12, 20))`. If your data set is very big, like you say, this will select only the first 20 rows. – Rui Barradas Feb 28 '20 at 14:35
  • `ggplot(R3L12, aes(x=as.numeric(Time), y=HeartRate)) + geom_point() + geom_smooth()` ? – cbo Feb 28 '20 at 14:35

3 Answers3

1

This is a different solution in two ways. (1) It redefines aes inside geom smooth. (2) It treats the time character values as ordinal, and thus it treats different measurements as equidistant. Depending on your hypotheses, this is not always the best thing to do. But sometimes it may be exactly what you want.

R3L12<-structure(list(Date=c("2015-05-23","2015-05-23","2015-05-23","2015-05-23","2015-05-23","2015-05-23","2015-05-23","2015-05-23","2015-05-23","2015-05-23","2015-05-23","2015-05-23","2015-05-23","2015-05-23","2015-05-23","2015-05-23","2015-05-23","2015-05-23","2015-05-23","2015-05-23"),
                      Time=c("07:25:00","07:40:00","07:45:00","09:10:00","11:45:00","11:55:00","12:05:00","12:35:00","12:45:00","13:30:00","13:40:00","13:45:00","13:55:00","14:00:00","14:05:00","14:10:00","14:20:00","14:25:00","14:30:00","14:35:00"),
                      Turtle=structure(c(3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L),
                                         .Label = c("R3L1","R3L11","R3L12","R3L2","R3L4","R3L8","R3L9","R4L8","R8L1","R8L4","R8NAT123"),class="factor"),
                      HeartRate=c(7.56457,6.66759,17.51107,9.72277,19.44553,13.07674,28.115,14.99467,17.16947,40.40479,37.76642,29.98933,43.5329,49.61471,47.74245,44.10196,21.35316,44.68609,49.25255,29.98933)),row.names=c(NA,20L),class="data.frame")

ggplot(R3L12,aes(x=Time,y=HeartRate)) +
  geom_point() +
  geom_smooth(aes(x=as.numeric(factor(Time)),y=HeartRate))

enter image description here

1

If times are unevenly spaced, as this is the case, and you want distance between x-axis points to reflect the actual time that is elapsed, you must convert your time variable into a POSIXct object. This may also change the way the smoothing line is shown, because the density of the points will change over the x-axis.

R3L12$time.posix <- as.POSIXlt(paste(R3L12$Date, R3L12$Time), format="%Y-%m-%d %H:%M:%S")
ggplot(R3L12, aes(x=time.posix, y=HeartRate)) +
   geom_point() +
   geom_smooth()

enter image description here

Claudio
  • 1,229
  • 6
  • 11
1

An alternative solution using lubridate package:

library(lubridate)
library(scales)
library(dplyr)
library(ggplot2)
df %>% 
  mutate(DateTime = ymd_hms(paste(Date, Time))) %>%
  ggplot(aes(x= DateTime, y = HeartRate))+
  geom_point()+
  geom_smooth()+
  scale_x_datetime( breaks=date_breaks("1 hour"), labels = date_format("%H:%M"))

enter image description here

dc37
  • 15,840
  • 4
  • 15
  • 32