Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fix "Error: Discrete value supplied to continuous scale" in R

I have a data frame that was returned after running a model that contains production values, the mean profit, and upper/lower boundaries for 80% and 90% confidence intervals. From there, I’m trying to create an overlapping line plot with the mean and ribbon plot for the confidence intervals. When running the code below, I end up getting the message "Error: Discrete value supplied to continuous scale"

#code from assignment 3
set.seed(114)
n <- 100000
probs <- c(.3,.45,.2,.05)
ints <- c(2000,5001,10001,14001,15000)

qbp <- function(p,probs,ints){
  ifelse(p<probs[1],
         qunif(p/probs[1],ints[1],ints[2]),qbp(p-probs[1],probs[-1],ints[-1]))
}

rbp <- function(n,probs,ints){
  p <- runif(n)
  sapply(p,qbp,probs=probs,ints=ints)
}

boat_df <- data.frame(
  demand = rbp(n,probs,ints) %>% round(),
  cost_fix = rnorm(n,300000000,60000000) %>% round(2),
  cost_var = rtriang(n,77000,100000,90000) %>% round(2)
)


#model updated to return only profit
boat_sim <- function(prod){

retail_full <- 150000
retail_disc <- 75000

boat_df <- boat_df %>%
  mutate(
    units_full = ifelse(prod > demand, demand, prod),
    units_disc = ifelse(prod > demand, prod-demand, 0),
    revenue = units_full*retail_full + units_disc*retail_disc,
    cost = cost_fix + prod*cost_var,
    profit = revenue - cost
  )
return(boat_df$profit)
}

#generate summary results
prod_1a <- seq(2000,15000,1000)

sum_stats <- function(prod_1a){
  x <- boat_sim(prod_1a)
  return(
    c(prod_1a=prod_1a,
      mean=mean(x),
      quantile(x,c(.005,.100,.900,.995))))
}

summary_1a <-
  sapply(prod_1a,sum_stats) %>%
  t() %>%
  as.data.frame()

#line & ribbon plot
ggplot(summary_1a)+
  geom_line(aes(x=prod_1a,
                y=mean))+
  geom_ribbon(aes(x=prod_1a,
                  ymin='0.5%',
                  ymax='99.5%'),
              alpha=.3)+
  geom_ribbon(aes(x=prod_1a,
                  ymin='10%',
                  ymax='90%'),
              alpha=.3)

From searching the issue, it seems like everyone is saying that I need to ensure that all of the variables are numeric, but when I run str(summary_1a) it shows that they all are numeric so I’m not sure where I’m going wrong with my code.

'data.frame':   14 obs. of  6 variables:
 $ prod_1a: num  2000 3000 4000 5000 6000 7000 8000 9000 10000 11000 ...
 $ mean   : num  -1.78e+08 -1.21e+08 -7.12e+07 -2.91e+07 5.90e+06 ...
 $ 0.5%   : num  -3.35e+08 -2.86e+08 -2.80e+08 -2.96e+08 -3.13e+08 ...
 $ 10%    : num  -2.56e+08 -2.02e+08 -1.65e+08 -1.53e+08 -1.64e+08 ...
 $ 90%    : num  -1.00e+08 -4.07e+07 1.79e+07 7.58e+07 1.33e+08 ...
 $ 99.5%  : num  -21006018 40849682 102371443 163326909 225063557 ...

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Replace the ticks (‘ ‘) with backticks (“). You can also simplify your code by putting common aesthetics in the ggplot call.

ggplot(summary_1a, aes(x=prod_1a)) +
  geom_line(aes(y=mean)) +
  geom_ribbon(aes(ymin=`0.5%`,
                  ymax=`99.5%`),
              alpha=.3)+
  geom_ribbon(aes(ymin=`10%`,
                  ymax=`90%`),
              alpha=.3)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading