How to fix 'data' must be a <data.frame>, or an object coercible by 'fortify(), not a list

52 Views Asked by At

I am new to R Studio. I am trying to create a Ribbon plot of my data but I keep getting this error. I have tried multiple searches to find out how to fix it but with no luck. Any advice would be appreciated.

This is the code I created:

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)
   replicate(n=n,
         qbp(p=runif(1),
             probs=probs,
             ints=ints))
 probs<-c(.35,.4,.2,.05)
 ints<-c(2,5,10,14,15)

 FP<-150000
 DP<-70000

 pqty_list<-c(4,8,12,15)

 n<-10000
 p<-runif(n)
 p2<-runif(n)
 p3<-runif(n)
 mc_sim<-function(pqty)
   data.frame(
     VC=qtriang(p2,a=77000,b=100000,c=90000),
     FC=qnorm(p3,300000,60000),
     demand=qbp(p,probs,ints)%>%round(3))%>%
   mutate(
     sold_fp=pmin(demand,pqty),
     sold_dp=pqty-sold_fp,
     profit=FP*sold_fp+DP*sold_dp-VC*pqty-FC)
 df_list4<-lapply(pqty_list,mc_sim)

 sapply(df_list4,function(df)mean(df$profit))
 ggplot(data=df_list4) + geom_point(aes(x=Sales, y=AvgRevenue)) + 
   geom_ribbon(aes(x=Sales, ymin=P80, ymax=P95), alpha=0.3, fill='red')

This is the error I keep receiving: Error in fortify(): ! data must be a <data.frame>, or an object coercible by fortify(), not a list.

0

There are 0 best solutions below