如何在R中使用润滑时数制作ggplot2散点图?

时间:2017-09-20 15:57:01

标签: r ggplot2 lubridate

我有一小时(大于24小时)代表一些学生在学习课程上花费的时间。我想用ggplot2制作一个研究时间和考试成绩的散点图。问题是数据的总时间是以rubridate的时间格式,我如何用这个绘图?例如:

            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

1 个答案:

答案 0 :(得分:2)

您的lubridate Total Time列表示为无法直接在ggplot中绘制的句点。幸运的是,很容易将一个润滑期转换成数字对象,无论你想要什么级别(即小时,分钟,秒)。

之后,使用数字时间维度作为x轴并将结果作为y轴进行绘图非常容易。

以下是您的数据示例:

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()