使用R中的ggplot在绘图上的数据集中获取时间序列

时间:2018-05-03 03:29:04

标签: r ggplot2

我有一个类似的数据集:

Asset Name  Time    Usage
A   10:39:49    CPU usage
A   10:46:35    CPU usage
B   11:06:55    CPU usage
C   11:24:05    Memory usage
A   11:31:35    Memory usage
B   13:46:17    CPU usage
A   10:35:43    Virtual Machine Failover Failed
C   10:40:55    CPU usage
B   10:45:05    CPU usage
A   11:31:35    CPU usage
B   10:59:44    Memory usage
B   11:44:36    CPU usage
A   11:49:22    Memory usage

我想根据用法绘制资产名称与时间的关系。 我用了代码:

ggplot(DTPlot, aes(DTPlot$Time, Asset.Name, color = Usage)) + geom_point() +
  scale_x_time() + xlab("") + ylab("Asset Name")

我在X轴上的时间从00:00:00到00:00:24。如何在图上的数据集中获取时间序列?

提前致谢!

1 个答案:

答案 0 :(得分:2)

你的意思是这样吗?

df %>%
    mutate(Time = as.POSIXct(Time, format = "%H:%M:%S")) %>%
    ggplot(aes(Time, Asset.Name, color = Usage)) +
    geom_point() +
    labs(x = "", y = "Asset Name")

enter image description here

样本数据

df <-read.table(text =
    "'Asset Name'  Time    Usage
A   10:39:49    'CPU usage'
A   10:46:35    'CPU usage'
B   11:06:55    'CPU usage'
C   11:24:05    'Memory usage'
A   11:31:35    'Memory usage'
B   13:46:17    'CPU usage'
A   10:35:43    'Virtual Machine Failover Failed'
C   10:40:55    'CPU usage'
B   10:45:05    'CPU usage'
A   11:31:35    'CPU usage'
B   10:59:44    'Memory usage'
B   11:44:36    'CPU usage'
A   11:49:22    'Memory usage'", header = T)