How can I add extra data points to a line graph in ggplot2?

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”
SO00001part_number57/30/2020
SO00002part_number188/12/2020
SO00003part_number329/4/2020
SO00004part_number1810/2/2020
SO00005part_number410/26/2020
SO00006part_number2410/29/2020
SO00007part_number4810/30/2020
SO00008part_number201/18/2021
SO00009part_number82/11/2021
SO00010part_number33/4/2021
SO00011part_number403/12/2021
SO00012part_number103/18/2021
SO00013part_number106/4/2021
SO00014part_number37/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))

enter image description here