2017-05-04 38 views
0
類別

打破我有收集了以下數據集(真實數據組是更大的):R:視覺,其中日期由

DATE;TIME;TAG 
20.3.2017;0:01;HAR 
20.3.2017;16:05;JKT 
20.3.2017;16:26;RVA 
20.3.2017;18:35;JKT 
20.3.2017;19:43;JVA 
20.3.2017;20:00;PER 
20.3.2017;21:42;RVA 
20.3.2017;22:05;HAR 
20.3.2017;23:59;HAR 

首先我計算出的實際的時間標記如以下

dataset$timestamp <- with(dataset, ISOdatetime(substring(dataset$DATE, 1, 4),substring(dataset$DATE, 7, 7),substring(dataset$DATE, 9, 10),substring(dataset$TIME,1,regexpr(':', dataset$TIME)-1),substring(dataset$TIME,regexpr(':', dataset$TIME)+1,length(dataset$TIME)),0)) 

您可能會注意到,在任何給定時刻只有一個標籤處於活動狀態。我想在下圖中看到它。

想想一個矩陣是1440單位高(日分鐘)和28單位寬(我的數據集中的天數)。每個正方形都根據TAG在那一分鐘內處於活動狀態而着色。

有沒有辦法做到這一點在R或我需要使用其他可視化工具?

回答

0

我假設你不是問如何處理你的數據到一個合適的格式,而只是如何產生圖。有幾種可能,比如使用ggplot2

#generate some toy data 
toyDf <- data.frame(minute = sample(1440, 100, TRUE), 
        day = sample(28,100,TRUE), 
        tag = sample(c("foo","bar","baz"),100,TRUE)) 

#plot 
library(ggplot2) 
# specify data, x, and y: 
ggplot(toyDf, aes(day,minute)) + 
# specify plot type and fill colour levels: 
    geom_raster(aes(fill = tag),hjust=0, vjust=0) + 
# specify where to draw minor gridlines: 
    scale_x_continuous(minor_breaks = seq(1, 28, 1)) + 
    scale_y_continuous(minor_breaks = seq(1, 1440, 1)) + 
#set plot aspect ratio (because it will be very high): 
    theme(aspect.ratio=30/5, 
#tweak some visual elements (gridlines, background): 
     panel.grid.minor = element_line(colour="black", size=0.05), 
     panel.background = element_blank()) 
+0

此代碼給我下面的錯誤: 錯誤,如果(is.waive(數據)||空的(數據))收益率(cbind(數據,PANEL =整數(0))): 缺少值,其中TRUE/FALSE需要 –

+0

抱歉!我忘了在ggplot調用中將我的原始'df'重命名爲'toyDf'。 –