用R中的时间变量绘制数据

时间:2014-08-02 08:07:11

标签: r plot

我试图绘制下面显示的数据。每个ID至少有16小时的数据,这些数据可以按严重程度进行分组。请问有更好的方法吗?

ID                TIME           SEVERITY
00-00-0C-12-43-02 2/21/2015 0:00 GOOD
00-00-0C-12-43-02 2/21/2015 1:00 GOOD
00-00-0C-12-43-02 2/21/2015 2:00 CRASH
00-00-0C-12-43-02 2/21/2015 3:00 CRASH
00-00-0C-12-43-02 2/21/2015 4:00 ERROR
00-00-0C-12-43-02 2/21/2015 5:00 WARN
00-00-0C-12-43-02 2/21/2015 6:00 GOOD
00-00-0C-12-43-02 2/21/2015 7:00 CRASH
00-00-0C-12-43-02 2/21/2015 8:00 GOOD
00-00-0C-12-43-02 2/21/2015 9:00 GOOD
00-00-0C-12-43-02 2/21/2015 10:00 GOOD
00-00-0C-12-43-02 2/21/2015 11:00 GOOD
00-00-0C-12-43-02 2/21/2015 12:00 GOOD
00-00-0C-12-43-02 2/21/2015 13:00 ERROR
00-00-0C-12-43-02 2/21/2015 14:00 ERROR
00-00-0C-12-43-02 2/21/2015 15:00 GOOD
00-00-0C-12-43-02 2/21/2015 16:00 GOOD
00-03-E0-43-11-19 2/21/2015 0:00 GOOD
00-03-E0-43-11-19 2/21/2015 1:00 GOOD
00-03-E0-43-11-19 2/21/2015 2:00 CRASH
00-03-E0-43-11-19 2/21/2015 3:00 ERROR
00-03-E0-43-11-19 2/21/2015 4:00 GOOD
00-03-E0-43-11-19 2/21/2015 5:00 ERROR
00-03-E0-43-11-19 2/21/2015 6:00 ERROR
00-03-E0-43-11-19 2/21/2015 7:00 GOOD
00-03-E0-43-11-19 2/21/2015 8:00 GOOD
00-03-E0-43-11-19 2/21/2015 9:00 GOOD
00-03-E0-43-11-19 2/21/2015 10:00 GOOD
00-03-E0-43-11-19 2/21/2015 11:00 GOOD
00-03-E0-43-11-19 2/21/2015 12:00 GOOD
00-03-E0-43-11-19 2/21/2015 13:00 ERROR
00-03-E0-43-11-19 2/21/2015 14:00 GOOD
00-03-E0-43-11-19 2/21/2015 15:00 GOOD
00-03-E0-43-11-19 2/21/2015 16:00 ERROR

我可以在下面的图中获取ID和Severity列,

tsb <- table(plot_data[,1],plot_data[,3]) 
plot(tsb,col=c("red","yellow","green","pink"),
  main="Status for Last 17 hrs")

想要包含时间列。 试过这个,

tsb <- table(plot_data[,1],order(plot_data[,2]),plot_data[,3]),

但由于y轴上的数据,并且在每小时数据之间得到一些暗线,因此情节看起来不太好。是否有正确/更好的方法可以在情节上清楚地了解这些数据。我是R的新手,从几天开始学习......

1 个答案:

答案 0 :(得分:0)

尝试:

ddf = structure(list(ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("00-00-0C-12-43-02", 
"00-03-E0-43-11-19"), class = "factor"), DATE = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), .Label = "2/21/2015", class = "factor"), TIME = structure(c(1L, 
2L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 1L, 2L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L), .Label = c("0:00", "1:00", "10:00", "11:00", 
"12:00", "13:00", "14:00", "15:00", "16:00", "2:00", "3:00", 
"4:00", "5:00", "6:00", "7:00", "8:00", "9:00"), class = "factor"), 
    SEVERITY = structure(c(3L, 3L, 1L, 1L, 2L, 4L, 3L, 1L, 3L, 
    3L, 3L, 3L, 3L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 2L, 3L, 2L, 2L, 
    3L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 2L), .Label = c("CRASH", 
    "ERROR", "GOOD", "WARN"), class = "factor")), .Names = c("ID", 
"DATE", "TIME", "SEVERITY"), class = "data.frame", row.names = c(NA, 
-34L))

levels(ddf$SEVERITY) = c("CRASH","ERROR","WARN","GOOD")

ss = strsplit(as.character(ddf$TIME), ':')

for(i in 1:length(ss)) (ddf$newtime[i]=ss[[i]][1])

head(ddf)
                 ID      DATE TIME SEVERITY newtime
1 00-00-0C-12-43-02 2/21/2015 0:00     GOOD       0
2 00-00-0C-12-43-02 2/21/2015 1:00     GOOD       1
3 00-00-0C-12-43-02 2/21/2015 2:00    CRASH       2
4 00-00-0C-12-43-02 2/21/2015 3:00    CRASH       3
5 00-00-0C-12-43-02 2/21/2015 4:00    ERROR       4
6 00-00-0C-12-43-02 2/21/2015 5:00     WARN       5


ggplot(ddf, aes(x=as.numeric(newtime), y=SEVERITY, group=ID, color=ID))+geom_line()+geom_point()

enter image description here

编辑: 对于条形图:

ggplot(ddf) + geom_bar(aes(x=as.numeric(newtime), y=SEVERITY, group=ID, fill=ID), stat="identity",position="dodge")

enter image description here