Home > Software engineering >  Error: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with
Error: `data` must be a data frame, or other object coercible by `fortify()`, not an S3 object with

Time:11-30

I have problem in getteing my plot from ggplot and always give me error as:

Error: data must be a data frame, or other object coercible by fortify(), not an S3 object with class uneval. Did you accidentally pass aes() to the data argument?

However I already created my data as data_frame. Here is my code:

Ef2<- colMeans(f2)
Eg2<- colMeans(g2)

Eg2_Pred=Eg2*ysd
Ef2_Pred=Ef2*Eg2_Pred ymean


pred<-data.frame(Ef=Eg2_Pred,sigma=Ef2_Pred)
class(pred)
Zaccel <- as.data.frame(Zaccel) 
class(Zaccel)

  ggplot(aes(Zaccel$Times, pred$Ef, group = .draw))  
  geom_line(color="firebrick", alpha = 0.05)  
  geom_point(data=Zaccel, mapping=aes(x=Zaccel$Times,y=Zaccel$accel), size = 2, inherit.aes=FALSE) 
  geom_line(data=cbind(Zaccel,pred), mapping=aes(x=Zaccel$Times,y=Ef2_Pred), inherit.aes=FALSE, color="firebrick", size=1)  
  labs(x="Time (s), Hs=4(m) and Winch Speed=0.1 (m/s)") 
  #ylab(expression(Maximum~Z~Acceleration~of~the~Hook~(m/s^2)))
  ylab(expression(GP~Posterior~Draws~"for"~Maximum~DAF))

Could you guys help me?

CodePudding user response:

If the arguments are unnamed then ggplot expects data to be first and mapping to be second. You can do it the way you specified, but you need to name the first argument as mapping. That is, the following all do the same thing:

ggplot(mapping=aes(mtcars$cyl, mtcars$mpg))   geom_point()

ggplot(mapping=aes(cyl, mpg))   geom_point(data=mtcars)

ggplot(mtcars, aes(cyl, mpg))   geom_point()

ggplot()   geom_point(mapping=aes(cyl, mpg), data=mtcars)

CodePudding user response:

Without a reproductible example is difficult to guess the issue. Try

ggplot(Zaccel, mapping=aes(Zaccel$Times, pred$Ef, group = .draw)) 
geom_line(color="firebrick", alpha = 0.05)  
  geom_point(data=Zaccel, mapping=aes(x=Zaccel$Times,y=Zaccel$accel), size = 2, inherit.aes=FALSE) 
  geom_line(data=cbind(Zaccel,pred), mapping=aes(x=Zaccel$Times,y=Ef2_Pred), inherit.aes=FALSE, color="firebrick", size=1)  
  labs(x="Time (s), Hs=4(m) and Winch Speed=0.1 (m/s)") 
  #ylab(expression(Maximum~Z~Acceleration~of~the~Hook~(m/s^2)))
  ylab(expression(GP~Posterior~Draws~"for"~Maximum~DAF))
  • Related