绘制10分钟间隔

时间:2018-10-04 18:15:22

标签: r plot timing

我想作图-每隔10分钟绘制一次-时间记录从日记开始的活动。

“ UKTUS”记录数据定义为

的每10分钟的人员活动
  

act1_1,act1_2,...,act1_144

(144列,每10分钟间隔1个)。

行是观察次数(16533),列是一天中进行的活动(144次活动x 10分钟)。

每个活动都分解为不同的编码方案。例如,睡眠编码如下:

sleep <- data.frame(
  code = c(110, 111, 120),
  description = c("Sleep", "In bed not asleep", "Sick in bed"))

#    code       description
# 1:  110             Sleep
# 2:  111 In bed not asleep
# 3:  120       Sick in bed

活动已定义:

主要活动

Activities <-uktus15_diary_wide[,c ("serial", "pnum","daynum","ddayw","DVAge", "dmonth", "dyear","WhenDiary","AfterDiaryDay", "WhereStart","WhereEnd","RushedD","Ordinary","KindOfDay","Trip","enjm1","act1_1", "act1_2", "act1_3", "act1_4", "act1_5", "act1_6", "act1_7", "act1_8", "act1_9", "act1_10",
                                    "act1_11", "act1_12", "act1_13", "act1_14", "act1_15", "act1_16", "act1_17", "act1_18", "act1_19", "act1_20",
                                    "act1_21", "act1_22", "act1_23", "act1_24", "act1_25", "act1_26", "act1_27", "act1_28", "act1_29", "act1_30",
                                    "act1_31", "act1_32", "act1_33", "act1_34", "act1_35", "act1_36", "act1_37", "act1_38", "act1_39", "act1_40",
                                    "act1_41", "act1_42", "act1_43", "act1_44", "act1_45", "act1_46", "act1_47", "act1_48", "act1_49", "act1_50",
                                    "act1_51", "act1_52", "act1_53", "act1_54", "act1_55", "act1_56", "act1_57", "act1_58", "act1_59", "act1_60",
                                    "act1_61", "act1_62", "act1_63", "act1_64", "act1_65", "act1_66", "act1_67", "act1_68", "act1_69", "act1_70",
                                    "act1_71", "act1_72", "act1_73", "act1_74", "act1_75", "act1_76", "act1_77", "act1_78", "act1_79", "act1_80",
                                    "act1_81", "act1_82", "act1_83", "act1_84", "act1_85", "act1_86", "act1_87", "act1_88", "act1_89", "act1_90",
                                    "act1_91", "act1_92", "act1_93", "act1_94", "act1_95", "act1_96", "act1_97", "act1_98", "act1_99", "act1_100",
                                    "act1_101", "act1_102", "act1_103", "act1_104", "act1_105", "act1_106", "act1_107", "act1_108", "act1_109",
                                    "act1_110", "act1_111", "act1_112", "act1_113", "act1_114")]


I created time use variables as follows:

> 
sleep<-0
eating<-0
work1<-0
work2<-0
tv <-0
radio <-0
internet<-0
personalcare<-0
study<-0
tidy1<-0
tidy2<-0
foodmanagement<-0
dishes<-0
cooking<-0
houseclean<-0
diy1<-0
diy2<-0
diy3<-0
diy4<-0
laundry<-0
ironing<-0
gardening<-0
pets<-0
hobby1<-0
shopping<-0
services<-0
childcare<-0
care<-0
helpothers<-0
social<-0
cultural<-0
library<-0
sportevents<-0
leisure1<-0
sports1<-0
sports3<-0
leisure2<-0
sports2<-0
leisure3<-0
computer<-0
hobby2<-0
computer2<-0
internet1<-0
computer3<-0
internet2<-0
computer4<-0
games1<-0
computer5<-0
games2<-0
internet3<-0
reading<-0
travel<-0
unknown<-0
waiting<-0

for (i in 1:144) {

  sleep<-ifelse(Activities[,i+15]>109 & Activities[, i+15]< 121, sleep+10, sleep)

  eating<-ifelse(Activities[,i+15]>209 & Activities[, i+15]<211, eating+10, eating)  
  personalcare<-ifelse(Activities[, i+15]>299 & Activities[, i+15]<391, personalcare+10, personalcare)
  work1<-ifelse(Activities[, i+15]>999 & Activities[, i+15]<1400, work1+10, work1)
  study<-ifelse(Activities[, i+15]>1999 & Activities[, i+15] <2209, study+10, study )
  tidy1<-ifelse(Activities[, i+15]>2999 & Activities[, i+15] <3001, tidy1+10, tidy1 )
  foodmanagement<-ifelse(Activities[, i+15]>3009 & Activities[, i+15] <3109 ,foodmanagement+10, foodmanagement)
  dishes<-ifelse(Activities[, i+15]>3129 & Activities[, i+15] <3130 , dishes+10, dishes)
  cooking<-ifelse(Activities[,i+15]>3139 & Activities[,i+15] <3201, cooking+10, cooking)
  houseclean<-ifelse(Activities[, i+15]>3109 & Activities[, i+15] <3301, houseclean+10, houseclean)
  diy1<-ifelse(Activities[, i+15]>3209 & Activities[, i+15] < 3301, diy1+10, diy1 )
  laundry<-ifelse(Activities[, i+15]>3309 & Activities[, i+15] < 3311, laundry+10, laundry)
  ironing<-ifelse(Activities[, i+15]>3319 & Activities [, i+15] < 3321, ironing+10, ironing)
  diy2<-ifelse(Activities[, i+15]>3329 & Activities [,i+15] < 3400, diy2+10, diy2)
  gardening<-ifelse(Activities[, i+15]>3409 & Activities [, i+15] < 3411, gardening+10, gardening)
  pets<-ifelse(Activities[, i+15]>3419 & Activities [, i+15]> 3491, pets+10, pets)
  diy3<-ifelse(Activities[, i+15]>3499 & Activities [, i+15]>3531, diy3+10, diy3)
  hobby1<-ifelse(Activities[, i+15]>3530 & Activities [, i+15]> 3540, hobby1+10, hobby1)
  diy4<-ifelse(Activities[, i+15]>3539 & Activities[, i+15]> 3591, diy4+10, diy4)
  shopping<-ifelse(Activities[, i+15]>3599 & Activities [, i+15] >3620, shopping+10, shopping)
  services<-ifelse(Activities[, i+15]> 3619 & Activities [, i+15]> 3691, services+10, services)
  tidy2<-ifelse(Activities[, i+15]>3709 & Activities[, i+15]>3711, tidy2+10, tidy2)
  internet<-ifelse(Activities[, i+15]>3712 & Activities[, i+15]>3730, internet+10, internet)
  childcare<-ifelse(Activities[, i+15]>3799 & Activities[,i+15]>3891, childcare+10, childcare)
  care<-ifelse(Activities[, i+15]>3909 & Activities[, i+15]>3999 ,care+10, care)
  work2<-ifelse(Activities[, i+15]>4099 & Activities[, i+15]>4191, work2+10, work2) 
  helpothers<-ifelse(Activities[,i+15]>4199 & Activities[, i+15]>4291, helpothers+10, helpothers)  
  social<-ifelse(Activities[, i+15]>4299 & Activities[, i+15]>5201, social+10, social)
  tv <- ifelse(Activities[, i+15]>8209 & Activities[, i+15]<8230, tv+10, tv)
  radio <- ifelse(Activities[, i+15]>8229 & Activities[, i+15]<8321, radio+10, radio)
  cultural<-ifelse(Activities[, i+15]>5199 & Activities[, i+15]<5231, cultural+10, cultural)
  library<-ifelse(Activities[, i+15]>5239 & Activities[, i+15]<5250, library+10, library)
  sportevents<-ifelse(Activities[, i+15]>5249 & Activities[, i+15]<5250, sportevents+10, sportevents)
  leisure1<-ifelse(Activities[, i+15]>5289 & Activities[, i+15]<5311, leisure1+10, leisure1)
  sports1<-ifelse(Activities[, i+15]>5999 & Activities[, i+15]<6111, sports1+10, sports1)
  leisure2<-ifelse(Activities[, i+15]>6109 & Activities[, i+15]<6120, leisure2+10, leisure2)
  sports2<-ifelse(Activities[, i+15]>6119 & Activities[, i+15]<6201, sports2+10, sports2)
  leisure3<-ifelse(Activities[, i+15]>6209 & Activities[, i+15]<6221, leisure3+10, leisure3)
  sports3<-ifelse(Activities[, i+15]>6289 & Activities [,i+15]<6313, sports3+10, sports3)
  computer<-ifelse(Activities[, i+15]>6999 & Activities[, i+15]<7001, computer+10, computer)
  hobby2<-ifelse(Activities[, i+15]>7099 & Activities[, i+15]<7191, hobby2+10, hobby2)
  computer2<-ifelse(Activities[, i+15]>7219 & Activities[, i+15]<7231, computer2+10, computer2)
  internet1<-ifelse(Activities[, i+15]>7230 & Activities[, i+15]<7250, internet1+10, internet1)
  computer3<-ifelse(Activities[, i+15]>7249 & Activities[, i+15]<7251, computer3+10, computer3)
  internet2<-ifelse(Activities[, i+15]>7250 & Activities[, i+15]<7252, internet2+10, internet2)
  computer4<-ifelse(Activities[, i+15]>7258 & Activities[, i+15]<7260, computer4+10, computer4)
  games1<-ifelse(Activities[, i+15]>7299 & Activities[, i+15]< 7330, games1+10, games1)
  computer5<-ifelse(Activities[, i+15]>7329 & Activities [, i+15]<7331, computer5+10, computer5)
  games2<-ifelse(Activities[, i+15]>7339 & Activities[, i+15]<7391, games2+10, games2)
  internet3<-ifelse(Activities[, i+15]>7999 & Activities[, i+15]<8001, internet3+10, internet3)
  reading<-ifelse(Activities[, i+15]>8099 & Activities[, i+15]<8191, reading+10, reading)
  travel<-ifelse(Activities[, i+15]>8999 & Activities[, i+15]<9891, travel+10, travel)
  unknown<-ifelse(Activities [, i+15]>9940 & Activities[, i+15]<10000, unknown+10, unknown)
  waiting<-ifelse(Activities [, i+15]>9939 & Activities[, i+15]<9941, waiting+10, waiting)
}

现在,我想绘制汇总变量(例如睡眠,洗涤等)。我没有时间变量(HH:mm:ss)。数据集中的时间由以下变量定义:dmonth(或日记月份,第4列,数字变量),dyarar(或日记年份,第5列,第2014-2015年,数字1)。

我要遵循的代码是

Activities.timing<-ts(Activities[25:30],frequency=60*24*365/10)
plot(Activities.timing)

example plot

有没有更有效的方法?是否可以更改x轴以10分钟为间隔显示时间?

1 个答案:

答案 0 :(得分:0)

不确定我完全了解您想要实现的目标。我认为您将需要将数据转换为长格式(当前为宽格式),并根据染整,dmonth(我假设您有dday)和act1_ ??创建一个日期时间列。价值观。在x轴上使用日期时间进行绘图时,时间将更好地表示。

这是使用我模拟的一些数据完成的方法:

library(dplyr)
library(lubridate)
library(ggplot2)
library(tidyr)

# data creation
sleep <- data.frame(
  code = c(110, 111, 120),
  description = c("Sleep", "In bed not asleep", "Sick in bed"))

df <- matrix(data = NA, nrow = 16533, ncol = 10) %>% tbl_df()
colnames(df) <- c("dday", "dmonth", "dyear", paste("act1", 1:7, sep = "_"))

df$dday <- 1
df$dmonth <- 1
df$dyear <- 2018

# wide to long format
df <- df %>% gather(interval, activity, -dday, -dmonth, -dyear) %>%
  arrange(dyear, dmonth)

# random activites
set.seed(1724)
df$activity <- sleep[round(runif(nrow(df), 1, 3), 0), ]$description

# convert various columns into a single datetime column
df <- df %>%
  mutate(mins = gsub("act1_", "", df$interval) %>%
  as.integer() * 10,
  hh = floor(mins/60),
  mm = mins %% 60, 
  datetime = paste(
    paste(dyear, dmonth, dday, sep = "-"),
    paste(hh, mm, sep = ":")) %>%
    ymd_hm()) %>%
  select(-dday, -dmonth, -dyear, -interval, -mins, -hh, -mm)

# plot
df %>%
  group_by(datetime, activity) %>%
  summarise(frequency = n()) %>%
  ggplot(aes(x = datetime, y = frequency)) +
  geom_line() +
  facet_grid(activity ~ .)

如果这不是您想要的,请更新您的问题或发表评论。也很乐意解释您未遵循的答案的任何部分。