setwd("E:/Documents/RWF/GFM")
f_axles = read.csv("forge_data-std.csv")
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
f_axles$Axle.Type = as.factor(f_axles$Axle.Type)
f_axles$Type.Of.Axle = as.factor(f_axles$Type.Of.Axle)
f_axles$Start.Date = dmy(f_axles$Start.Date)
## Warning: 148 failed to parse.
f_axles$Start.Time = hms(f_axles$Start.Time)
## Warning in .parse_hms(..., order = "HMS", quiet = quiet): Some strings failed to
## parse, or all strings are NAs
f_axles$End.Date = dmy(f_axles$End.Date)
## Warning: 138 failed to parse.
f_axles$End.Time = hms(f_axles$End.Time)
## Warning in .parse_hms(..., order = "HMS", quiet = quiet): Some strings failed to
## parse, or all strings are NAs
f_axles$Piece.No.= as.numeric(f_axles$Piece.No.)
## Warning: NAs introduced by coercion
f_axles$Forge.Time = hms(f_axles$Forge.Time)
## Warning in .parse_hms(..., order = "HMS", quiet = quiet): Some strings failed to
## parse, or all strings are NAs
f_axles = na.omit(f_axles)
f_axles$Start.Instant = ymd_hms(paste(f_axles$Start.Date, f_axles$Start.Time))
## Warning: 1108 failed to parse.
f_axles = na.omit(f_axles)
f_axles$End.Instant = ymd_hms(paste(f_axles$End.Date, f_axles$End.Time))
## Warning: 21 failed to parse.
f_axles = na.omit(f_axles)

start.data = f_axles[, c("Type.Of.Axle",
                     "Start.Instant", 
                     "End.Instant", 
                     "Piece.No.", 
                     "Forge.Time")]

names(start.data)[1] = "Start.Type.Of.Axle"
names(start.data)[2] = "Next.Start.Instant"
names(start.data)[3] = "Next.End.Instant"
names(start.data)[4] = "Start.Piece.No."
names(start.data)[5] = "Start.Forge.Time"

end.data = f_axles[, c("Type.Of.Axle",
                         "Start.Instant", 
                         "End.Instant", 
                         "Piece.No.", 
                         "Forge.Time")]

names(end.data)[1] = "End.Type.Of.Axle"
names(end.data)[2] = "Prev.Start.Instant"
names(end.data)[3] = "Prev.End.Instant"
names(end.data)[4] = "End.Piece.No."
names(end.data)[5] = "End.Forge.Time"

end.data = end.data[1:24050,]
start.data = start.data[2:24051,]

end.data = end.data[order(end.data$Prev.Start.Instant,decreasing = FALSE),]
start.data = start.data[order(start.data$Next.Start.Instant,decreasing = FALSE),]



composite.data = cbind(end.data,start.data)

composite.data$Time.Lapse = 
  composite.data$Next.Start.Instant - composite.data$Prev.End.Instant


composite.data$Setup.Change <- ifelse(
  composite.data$End.Type.Of.Axle == composite.data$Start.Type.Of.Axle, 
  'No', 'Yes')
hist(as.numeric(composite.data$Time.Lapse))

delay.forge = subset(composite.data, composite.data$Time.Lapse > 300 & composite.data$Time.Lapse < 1800 & composite.data$Setup.Change == "No")

hist(as.numeric(delay.forge$Time.Lapse))

table(delay.forge$End.Piece.No.)
## 
##  1  2  3  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 
## 13  9  5  3  2  5  3  3  5  3  3  9  1  3  4  1  2  1  6  2  1  4  4  6  6  3 
## 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 
##  7  3  5  4  3  1  4  1  1  3  3  4  6  6  8 17  7  8  4  3  6  3  2  4  5  2 
## 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 
##  6  3  3  3  2  3  5  3  2  3  4  3  2  1  3  2  3  4  3  2  1  5  5  3  1  1 
## 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 98 
##  4  2  6  6  5 14  8  5  6  5 36 16  2  3  7  6 59  1
plot(table(delay.forge$End.Piece.No.))

p1 = ggplot(data = delay.forge, aes(End.Piece.No., Start.Piece.No.))
p1 + geom_point()

p2 = ggplot(data = delay.forge, aes(End.Piece.No.,as.numeric(Time.Lapse)))
p2 + geom_point(aes(color = End.Type.Of.Axle, size = End.Type.Of.Axle)) + geom_jitter()
## Warning: Using size for a discrete variable is not advised.

p3 = ggplot(data = composite.data, aes(End.Piece.No.,as.numeric(Time.Lapse)))
p5 = p3 + geom_point(aes(color = hour(Next.Start.Instant))) + geom_jitter()

p5 + facet_grid(vars(Setup.Change))

p4 = ggplot(data = composite.data, aes(hour(Next.Start.Instant), Time.Lapse))
p4 + geom_point(aes(color = End.Piece.No.)) + geom_jitter() + facet_grid(vars(Setup.Change))
## Don't know how to automatically pick scale for object of type difftime. Defaulting to continuous.

p6 = ggplot(data = delay.forge, aes(as.numeric(Time.Lapse)))
p6 + geom_histogram(binwidth = 60) + facet_grid(vars(hour(Prev.End.Instant)))

p7 = ggplot(data = delay.forge, aes(hour(Prev.End.Instant),as.numeric(Time.Lapse)),color = Start.Piece.No.)
p7 + geom_point() + facet_grid(vars(End.Type.Of.Axle)) + 
  scale_x_continuous(n.breaks = 96) +
  theme(axis.text.x = element_text(angle = 90))