2016-11-17 33 views
0

下面是含有僱員考勤產生r柱狀圖員工

  date intime outtime 
2 02/11/2015 10:21:27 17:58:12 
3 03/11/2015 10:13:09 18:52:44 
4 04/11/2015 10:11:52 18:40:36 
5 05/11/2015 10:31:42 18:16:57 
6 06/11/2015 10:13:13 18:36:15 
10 10/11/2015 10:03:20 18:07:52 
11 11/11/2015 09:40:20 18:42:20 
12 12/11/2015 10:38:56 18:37:20 
13 13/11/2015 10:45:26 18:09:54 
16 16/11/2015 10:13:13 18:36:15 
17 17/11/2015 10:11:43 18:36:15 
18 18/11/2015 10:13:13 18:36:15 
19 19/11/2015 10:13:13 18:36:15 
20 20/11/2015 12:14:25 20:25:08 
23 23/11/2015 10:08:08 17:57:35 
24 24/11/2015 14:30:32 18:36:15 

由員工以小時服務的總時間記錄我的數據集是:

total_time <- with(newdata, sum(pmin(newdata$outtime, "18:00:00") - 
           pmax(newdata$intime, "08:00:00") )) 
total_time <- 24*floor(as.numeric(total_time)) 
"Total time served by employee is : 96 hours" 

我想生成每個直方圖員工每月按小時服務,共5箱。

+0

使用'r'標籤時,請使用'dput()'共享數據。將指針懸停在「r」標籤上以獲取更多信息。 –

+0

你有什麼嘗試?你看過'hist()'嗎?另一條路線可能是'barplot()'。 – Badger

+0

是的,我已經嘗試過,但不能正確地做到這一點 –

回答

0

我改變的數據,使得我們有更多個信息:只有5箱(一個更好的柱狀圖)

library(data.table) 
df = fread(" date intime outtime 
      02/11/2015 10:21:27 17:58:12 
      03/11/2015 10:13:09 18:52:44 
      04/11/2015 10:11:52 18:40:36 
      05/11/2015 10:31:42 18:16:57 
      06/11/2015 10:13:13 18:36:15 
      10/11/2015 10:03:20 18:07:52 
      11/11/2015 09:40:20 18:42:20 
      12/11/2015 10:38:56 18:37:20 
      13/11/2015 10:45:26 18:09:54 
      16/11/2015 10:13:13 18:36:15 
      17/11/2015 10:11:43 18:36:15 
      18/11/2015 10:13:13 18:36:15 
      19/11/2015 10:13:13 18:36:15 
      20/11/2015 12:14:25 20:25:08 
      23/11/2015 10:08:08 17:57:35 
      24/11/2015 14:30:32 18:36:15") 

df$intime <- as.POSIXct(df$intime, format = "%H:%M:%S") 
df$outtime <- as.POSIXct(df$outtime, format = "%H:%M:%S") 

library(lubridate) #to extract the day 
df$day <- dmy(df$date) 
df$day <- day(df$day) 

df$total_time <- difftime(pmin(df$outtime, as.POSIXct("18:00:00", format = "%H:%M:%S")), 
         pmax(df$intime, as.POSIXct("08:00:00", format = "%H:%M:%S")), units = "hours") 
df$total_time <- as.numeric(df$total_time) 

library(ggplot2) 
ggplot(df, aes(x = day, y= total_time))+geom_histogram(stat = "identity", bins = 5) 

op


(只有4箱出現在劇情因爲24-30天的間隔沒有數據):

df$breaks <- cut(df$day, breaks = c(0,5,10,15,30,max(df$day))) 
df1=ddply(df, "breaks", summarise, "total_hr"=sum(total_time)) 
ggplot(df1, aes(x = breaks, y= total_hr))+ 
    geom_histogram(stat = "identity", bins = 5, binwidth = 0) 

op

+0

我想直方圖只有一個月:) –

+0

呵呵! ohhh :(每個員工的總小時數?如何區分員工?爲此額外的列? –

+0

或者您想要x軸上的小時數嗎? –