Reputation: 25
I have a set of hours (some greater than 24) that represents the time some students spent on studying sessions. I want to make a scatter plot of studying time and exam results using ggplot2. The thing is the total time data is in lubridate's time format, how do I plot using this? Example:
Name ID Total Time Results
1 Student1 xx-xxxxx-xx 9H 56M 0S 37.58
2 Student2 xx-xxxxx-xx 10H 28M 0S 73.89
3 Student3 xx-xxxxx-xx 6H 40M 0S 4.14
4 Student4 xx-xxxxx-xx 3H 22M 0S 33.44
Upvotes: 1
Views: 1855
Reputation: 1228
Your lubridate
Total Time
column is represented as a period which cannot be plotted directly in something like ggplot. Fortunately, it's very easy to convert a lubridate period into numeric object at whatever level you wish (i.e. hour, minutes, seconds).
After that, it's very easy to plot with your numeric time dimension as your x axis and your results as your y axis.
Here's an example with your data:
library(lubridate)
# Replicate your data
example <- data.frame(Name = c("Student1","Student2","Student3","Student4"),
TotalTime = hms(c("09H 56M 0 S","10H 28M 0S","06H 40M 0S", "03H 22M 0S")),
Results = c(37.58,73.59,4.14,33.44))
example$TotalTime <- as.numeric(example$TotalTime, "hours")
library(ggplot2)
ggplot(example, aes(TotalTime, Results)) + geom_point()
Upvotes: 4