0

Here's some of the data:

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")
ggplot(R3L12, aes(x=Time, y=HeartRate)) +
  stat_summary(fun.y=mean, geom="point") +
  geom_smooth(aes(x=as.numeric(factor(Time)), y=HeartRate)) +
  labs(title = "Turtle R3L12 Average Heart Rate", ylab = "Heart Rate") +
   theme(axis.text.x = element_text(angle = 90, hjust=1, size = 3, color = "black"), plot.title = element_text(face = "bold", hjust = 0.5, size = 15)) 

This is the code I have to graph a scatter plot with a smooth line. I have time intervals from 00:00 to 23:55:00. I only want the times to show up every 30 minutes so the x-axis isn't crowded but I haven't figured out how to do that. I'm very new to R so please keep the explanation simple.

The time variable is currently a character object like this HH:MM:SS.

Cassidy
  • 395
  • 1
  • 11
  • 1
    Can you add data to make this [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? It's also likely that similar enough questions are already on SO, and with a clearer sense of what you're working with, folks can point you toward those – camille Mar 01 '20 at 19:49

1 Answers1

2

One solution is to set your time variable as a date time format using ymd_hms function from lubridate package and then use scale_x_datetime function to set appropriate parameters.

If you have multiple dates and you want to plot only one date, you can subset the dataframe:

library(lubridate)
library(dplyr)
library(ggplot2)
library(scales)
R3L12 <- R3L12 %>% mutate(Date_Time = ymd_hms(paste(Date,Time, sep = " "))) 

ggplot(subset(R3L12, Date == "2015-05-23"), aes(x = Date_Time, y = HeartRate))+
  stat_summary(fun.y=mean, geom="point") +
  geom_smooth() +
  labs(title = "Turtle R3L12 Average Heart Rate", ylab = "Heart Rate") +
  scale_x_datetime( breaks=date_breaks("30 min"), labels = date_format("%H:%M"))

enter image description here


EDIT: Plotting multiple dates

If you have multiple dates that you want to plot on the same time axis without making a time series, you can create a column with Date and Time combined but with the date being a single unique date.

Let me explain. First, based on your initial dataframe (called df) I generated a second df and I bind both of them together in order to have data for two dates on the same time:

df2 <- df
df2$Date <- "2015-05-24"
df2$HeartRate <- df$HeartRate * 1.5
DF <- rbind(df,df2)

Then, I will create a new colum DateTime that will be the combination of different time with an unique date. Like that I will make ggplot2 think that these data are at the same date and I will be able to use scale_x_datetime function.

library(dplyr)
DF <- DF %>% mutate(DateTime2 = ymd_hms(paste("2020-01-01",Time, sep = " "))) 

Then, to plot each date together on the same time you can do:

library(ggplot2)
ggplot(DF, aes(x = DateTime2, y = HeartRate, color = Date))+
  geom_point()+
  scale_x_datetime( breaks=date_breaks("30 min"), labels = date_format("%H:%M"))+
  stat_summary(geom = "point", color = "black", fun.y = mean)+
  geom_smooth()

enter image description here

Here, I use stat_summary to plot average of all dates (black color).

If you want to plot only average point and get the smooth on the average point, you can calculate the mean outside of ggplot2 by doing:

library(dplyr)
DF %>% mutate(DateTime2 = ymd_hms(paste("2020-01-01",Time, sep = " "))) %>%
  group_by(DateTime2) %>%
  summarise(Mean = mean(HeartRate))

And get the plot by doing:

library(dplyr)
library(ggplot2)
DF %>% mutate(DateTime2 = ymd_hms(paste("2020-01-01",Time, sep = " "))) %>%
  group_by(DateTime2) %>%
  summarise(Mean = mean(HeartRate)) %>%
  ggplot(aes(x = DateTime2, y = Mean))+
  geom_point()+
  geom_smooth()+
  scale_x_datetime( breaks=date_breaks("30 min"), labels = date_format("%H:%M"))

enter image description here Does it answer your question ?


NB: I made a similar answer to one of your previous questions few days ago: Geom_smooth not appearing in simple plot. Maybe you should had take a look at it before posting this question.

dc37
  • 15,840
  • 4
  • 15
  • 32
  • this didn't work. Basically, I get an error saying that it needs to be the class of POSIXct. I will add a reproducible example. – Cassidy Mar 01 '20 at 20:06
  • I edited my answer accordingly. Also, I provide you a similar answer few days ago https://stackoverflow.com/questions/60453637/geom-smooth-not-appearing-in-simple-plot. Have you try this solution ? Why it did not work for you ? Does the new one works ? – dc37 Mar 01 '20 at 20:20
  • I think I tried the solution on the other post and it didn't work. I tried your solution above and it ended up creating a timeseries because of the fact that my data has multiple dates in it. I just want to use one date. – Cassidy Mar 07 '20 at 16:16
  • I edited my answer. You can use `subset` to get only one date display. Is it what you are looking for ? – dc37 Mar 07 '20 at 19:37
  • I think I said that wrong. This definitely worked for one date and changing the x-axis, but what about the average of all dates? Without subsetting, that's when I get the time series. – Cassidy Mar 08 '20 at 19:33
  • Ok, I think I understand it. I edited my answer to show you how to lpot all the dates together and calculate the mean of each time point across all dates. Please let me know if it is working. – dc37 Mar 08 '20 at 21:59