Solution for How can I add extra data points to a line graph in ggplot2?
is Given Below:
I am working on creating a simulation of how min max levels would affect inventory based on past sales history. The idea is to be able to use this code with any data set, choose any min, max, and lead time, and see how the inventory would have been affected.
My data set has sales orders with the qty of the part ordered and the date it was issued.
I currently have a pretty good method that maps changes to the inventory levels based on what was sold, and creates simulated purchases with lead times and a ordered quantity to be added once the lead time is finished.
My current issue is that the way I structured the simulation, when I graph the results I can show points for all the inventory statuses, but I can only connect points affected by sales with geom_line, and not the points affected by purchases.
I want the graph to connect all points with the line.
I understand that this issue is happening cause the geom_line function is reading the date column and the stock_run column as its variables and doesn’t recognize a third column when drawing the line. I was able to add a geom_point function to add the extra points to the graph but I have not found a way to connect them to the line.
Data set
“so_num” | “part_num” | “qty” | “date” |
---|---|---|---|
SO00001 | part_number | 5 | 7/30/2020 |
SO00002 | part_number | 18 | 8/12/2020 |
SO00003 | part_number | 32 | 9/4/2020 |
SO00004 | part_number | 18 | 10/2/2020 |
SO00005 | part_number | 4 | 10/26/2020 |
SO00006 | part_number | 24 | 10/29/2020 |
SO00007 | part_number | 48 | 10/30/2020 |
SO00008 | part_number | 20 | 1/18/2021 |
SO00009 | part_number | 8 | 2/11/2021 |
SO00010 | part_number | 3 | 3/4/2021 |
SO00011 | part_number | 40 | 3/12/2021 |
SO00012 | part_number | 10 | 3/18/2021 |
SO00013 | part_number | 10 | 6/4/2021 |
SO00014 | part_number | 3 | 7/16/2021 |
Code
#change class of date column to DATE
table_part_number$date <- as.Date(table_part_number$date,format = "%m/%d/%Y")
#Set simulation parameters
#set part min
pmin <- 68
#set part max
pmax <- 143
#Set starting Inventory
stock <- pmax
#Set date of next PO arriving to Null
arrive_date <- NULL
# Set parts ordered to 0
pordered <- 0
# Set lead time (number of days)
lead_time <- 87
# Simulation code
for(i in 1:nrow(table_part_number)){
message("i = ",i)
if(is.null(arrive_date)){
if(stock > pmin){
stock <- (stock - table_part_number$qty[i])
table_part_number$stock_run[i] <- stock
if(is.null(arrive_date) == FALSE){
table_part_number$po_date[i] <- as.Date(arrive_date)
}
else{ table_part_number$po_date[i] <- NA}
message(stock)
}
else if(stock <= pmin){
arrive_date <- as.Date(table_part_number$date[i-1] + lead_time)
pordered <- (pmax - stock)
stock <- (stock - table_part_number$qty[i])
table_part_number$stock_run[i] <- stock
if(is.null(arrive_date) == FALSE){
table_part_number$po_date[i] <- as.Date(arrive_date)
}
else{ table_part_number$po_date[i] <- NA}
message(stock)
}
}
else {
if(table_part_number$date[i] > arrive_date){
stock <- (stock + pordered)
arrive_date <- NULL
pordered <- 0
}
if((is.null(arrive_date)) & (stock <= pmin)){
arrive_date <- as.Date(table_part_number$date[i] + lead_time)
pordered <- (pmax - stock)
}
stock <- (stock - table_part_number$qty[i])
table_part_number$stock_run[i] <- stock
if(is.null(arrive_date) == FALSE){
table_part_number$po_date[i] <- as.Date(arrive_date)
}
else{ table_part_number$po_date[i] <- NA}
message(stock)
}
table_part_number$po_qty[i] <- pordered}
#Make po_date correctly display
table_part_number$po_date <- as.Date.numeric(table_part_number$po_date, origin = '1970-01-01')
#Graph The results
ggplot(aes(x = date, y = stock_run), data = table_part_number) +
geom_point(shape = 16, alpha = 1, colour = "black") +
geom_abline(slope = 0, intercept = pmin, colour = "#cccc00", size = 2, alpha = .5)+
geom_abline(slope = 0, intercept = pmax, colour = "green", size = 2, alpha = .5)+
geom_abline(slope = 0, intercept = 0, colour = "red", size = 2, alpha = .5)+
geom_point(aes(x=po_date, y = po_qty), colour = "purple")+
geom_line( size = 1, colour = "#716dff", alpha = .6) +
scale_x_date(limits = as.Date(c("2020-07-23","2021-07-24"))) +
scale_y_continuous(limits = c(NA, pmax), breaks = (10 * c(-100:3000))) +
xlab("Date Issued of SO") +
ylab("Quantity After Transaction") +
ggtitle(paste0("Simulated Inventory of ",table_part_number$part_num[1], "n", " Min ",pmin," Max ",pmax, "n" , " Lead time of ", lead_time))