ggplot2 Error "Error: Must subset columns with a valid subscript vector."

Hi! I am having an error trying to plot my data. The errors I am getting are "Error: Must subset columns with a valid subscript vector. x Can't convert from to due to loss of precision."
and "Error: Aesthetics must be either length 1 or the same as the data (40161): x and colour" Please let me know if you have any idea why this is happening. Thank you!
The errors come about when executing the pivot_longer function near the bottom of my code below. Thanks again.

library(deSolve)
library(plotly)
library(tidyverse)

CCHFModelCombined = function(t,x,params)
{
  # get SIR values
  SH = x[1]
  EH = x[2]
  IH = x[3]
  RH = x[4]
  ST = x[5]
  ET = x[6]
  IT = x[7]
  SC = x[8]
  EC = x[9]
  IC = x[10]
  RC = x[11]
  
  # Beta values
  betaHH = params["betaHH"]
  betaTH = params["betaTH"]
  betaCH = params["betaCH"]
  betaTC = params["betaTC"]
  betaCT = params["betaCT"]
  betaTTV = params["betaTTV"] # vertical transmission
  betaTTH = params["betaTTH"]
  
  # Gamma value
  gamma = params["gamma"]
  
  # death rates
  muH = params["muH"]
  muT = params["muT"]
  muC = params["muC"]
  
  # birth rates
  piH  = params["piH"]
  piT = params["piT"]
  piC = params["piC"]
  
  # incubation
  deltaH1 = params["deltaH1"]
  deltaT = params["deltaT"]
  deltaC = params["deltaC"]
  
  # recovery rate
  alpha = params["alpha"]
  alpha2 = params["alpha2"]
  
  # total population
  NH = params["NH"] #(SH + IH + EH + RH) + (piH * SH) - (muH * SH)
  NT = params["NT"] #(ST + ET + IT) +  (piT * ST) - (muT * ST)
  NC = params["NC"] #(SC + EC + IC) +  (piC * SC) - (muH * SC)
  
  #computations
  
  dSHdt <- (piH * NH) - (betaHH * (SH/NH) * IH) - (betaCH * (SH/NH) * IC) - (betaTH * (SH/NH)* IT) - (muH * SH)
  dEHdt <- (betaHH * (SH/NH) * IH) + (betaCH * SH/NH * IC) + (betaTH * (SH/NH) * IT) - ((deltaH1 + muH)*EH)
  dIHdt <- (deltaH1 * EH) - ((alpha + gamma + muH)* IH)
  dRHdt <- ((alpha + gamma + muH)* IH) - (muH * RH)
  dSTdt <- piT * (NT - (betaTTV * IT)) - (betaCT * (ST/NT) * IC) - (betaTTH * (ST/NT) * IT) - (muT * ST)
  dETdt <- (piT * betaTTV * IT) +(betaCT * (ST/NT) * IC) + (betaTTH * (ST/NT) * IT) - ((deltaT + muT)* ET)
  dITdt <- (deltaT * ET) - (muT * IT)
  dSCdt <- (piC * NC) - (betaTC * (SC/NC) * IT) - (muC * SC)
  dECdt <- (betaTC * (SC/NC) * IT) - ((deltaC + muC)* EC)
  dICdt <- (deltaC * EC) - (muC * IC)
  dRCdt <- (alpha2 * IC) - (muC * RC)
  
  # return results
  list(c(dSHdt, dEHdt, dIHdt, dRHdt, dSTdt, dETdt, dITdt, dSCdt, dECdt, dICdt, dRCdt))
}

paramsSpringSummer = c(betaHH = .0479,  
           betaTH = .1567, 
           betaCH = .1151, 
           betaTC = (1/365), # One tick attaches to one carrier per year
           betaCT = 99/365, # One cattle infects 99 ticks per year (assuming 100 ticks on cattle)
           betaTTV = ((1/(365 * 2)) * .04) * 280, # ticks give birth once in a lifetime to 7000 ticks with a 4% chance of vertical transmission
           betaTTH = 1/365, # check horizontal transmission
           gamma = 1/10, # death occurs 7-9th day after onset of illness plus 2 day incubation
           muH = (1/(365 * 79)), 
           muT = (1/(365* 2)),
           muC = (1/(8 * 365)), #sheep/deer live 6-11 years
           piH = 1.25/(79 * 365), # one couple produces 2.5 children in a lifetime, so one mother produces 1.25
           piT =  1 /365000, #280/ ( 365 * 2), # 4% of eggs survive 
           piC = 7/(8 * 365), # sheep produce 7 babies in their life
           deltaH1 = 1/2.5, # 1-3 days from ticks, 5-6 days from blood contact 
           deltaT = 1/1.5, 
           deltaC = 1/2,
           alpha = 1/17, # recovery after 15 days
           alpha2 = 1/7,
           NH = 101000,
           NT = 1010000,
           NC = 10100)

paramsFall = c(betaHH = 0.00001,  
                     betaTH = 0.00001, 
                     betaCH = 0.00001, 
                     betaTC = 0.000000001, # One tick attaches to one carrier per year
                     betaCT = 0.00001, # One cattle infects 99 ticks per year (assuming 100 ticks on cattle)
                     betaTTV = ((1/(365 * 2)) * .04) * 280, # ticks give birth once in a lifetime to 7000 ticks with a 4% chance of vertical transmission
                     betaTTH = 1/36500, # check horizontal transmission
                     gamma = 1/10, # death occurs 7-9th day after onset of illness plus 2 day incubation
                     muH = (1/(365 * 79)), 
                     muT = (1/(365* 2)),
                     muC = (1/(8 * 365)), #sheep/deer live 6-11 years
                     piH = 1.25/(79 * 365), # one couple produces 2.5 children in a lifetime, so one mother produces 1.25
                     piT =  280/ ( 365 * 2), # 4% of eggs survive 
                     piC = 7/(8 * 365), # sheep produce 7 babies in their life
                     deltaH1 = 1/2.5, # 1-3 days from ticks, 5-6 days from blood contact 
                     deltaT = 1/1.5, 
                     deltaC = 1/2,
                     alpha = 1/17, # recovery after 15 days
                     alpha2 = 1/7,
                     NH = 101000,
                     NT = 1010000,
                     NC = 10100)

paramsWinter = c(betaHH = 0.00001,  
                 betaTH = 0.00001, 
                 betaCH = 0.00001, 
                 betaTC = 0.000000001, # One tick attaches to one carrier per year
                 betaCT = 0.00001, # One cattle infects 99 ticks per year (assuming 100 ticks on cattle)
                 betaTTV = ((1/(365 * 2)) * .04) * 280, # ticks give birth once in a lifetime to 7000 ticks with a 4% chance of vertical transmission
                 betaTTH = 1/36500, # check horizontal transmission
                 gamma = 1/10, # death occurs 7-9th day after onset of illness plus 2 day incubation
                 muH = (1/(365 * 79)), 
                 muT = (1/(365* 2)),
                 muC = (1/(8 * 365)), #sheep/deer live 6-11 years
                 piH = 1.25/(79 * 365), # one couple produces 2.5 children in a lifetime, so one mother produces 1.25
                 piT = 1/360000, # 4% of eggs survive 
                 piC = 7/(8 * 365), # sheep produce 7 babies in their life
                 deltaH1 = 1/2.5, # 1-3 days from ticks, 5-6 days from blood contact 
                 deltaT = 1/1.5, 
                 deltaC = 1/2,
                 alpha = 1/17, # recovery after 15 days
                 alpha2 = 1/7,
                 NH = 101000,
                 NT = 1010000,
                 NC = 10100)

# time to start solution 
timeCombined = seq(from = 0, to = 365, by = 0.1)

#initialize initial conditions
initialXCombined = c(SH = 100000, EH = 0, IH = 0, RH = 0, ST = 1000000, ET = 0, IT = 1, SC = 10000, EC = 0, IC = 0, RC = 0)

dataSetCombined = ode(y = initialXCombined, times = timeCombined, func = CCHFModelCombined, parms = paramsSpringSummer, #ifelse(time <= 182, paramsSpringSummer, ifelse(time > 182 && time <= 273, paramsFall, paramsWinter)),
                      method = "ode45")%>%
                      as.data.frame()


# graph data
dataCombined <- pivot_longer(data=dataSetCombined,
                      cols=-timeCombined,
                      names_to = "initialXCombinef",
                      values_to="value")

myPlotCombined <- ggplot(dataCombined, aes(x=timeCombined, y= value,color=initialXCombined)) + geom_line() + ggtitle("CCHF")


ggplotly(p = myPlotCombined, tooltip = "all", dynamicTicks = TRUE, originalData = TRUE)

To help us help you, could you please turn this into a proper reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

1 Like

Seems to me you had a a variable confusion time vs timeCombined, also a typo on initialXCombined(it had an f on the end)

dataCombined <- pivot_longer(data=dataSetCombined,
                             cols=-time,
                             names_to = "initialXCombined",
                             values_to="value")

myPlotCombined <- ggplot(dataCombined, aes(x=time, y= value,color=initialXCombined)) + geom_line() + ggtitle("CCHF")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.