ERROR while rich displaying an object: Error: Continuous value supplied to discrete scale

created at 10-01-2021 views: 10

problem

Pre0 <-rms::Predict(fit,Hb,fun=exp,type="predictions",ref.zero=TRUE,conf.int = 0.95,digits=2);
##Where fun is the conversion function
ggplot(Pre0)
View(Pre0)


ggplot()+
  geom_line(data=Pre0,
            aes(Hb,yhat,colour=gender),alpha=0.7)+
  scale_color_nejm()+ ##Using England in the ggsci package, other colors are also available
  geom_ribbon(data=Pre0,
              aes(Hb, ymin=lower,ymax=upper,fill=gender),alpha=0.1)+
  scale_fill_nejm()+
  geom_hline(yintercept=1,linetype=2,size=0.75)

solution

as.factor converts the sex variable into a factor;

Pre0 <-rms::Predict(fit,Hb,fun=exp,type="predictions",ref.zero=TRUE,conf.int = 0.95,digits=2);
##Where fun is the conversion function
ggplot(Pre0)
View(Pre0)


ggplot()+
  geom_line(data=Pre0,
            aes(age,yhat,colour=as.factor(sex)),alpha=0.7)+
  scale_color_nejm()+ ##Using England in the ggsci package, other colors are also available
  geom_ribbon(data=Pre0,
              aes(age, ymin=lower,ymax=upper,fill=as.factor(sex)),alpha=0.1)+
  scale_fill_nejm()+
  geom_hline(yintercept=1,linetype=2,size=0.75)

Reference example mtcars

ggplot(mtcars, aes(x=wt, y=mpg, color=as.factor(cyl), shape=as.factor(cyl))) +
  geom_point() + 
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
  scale_shape_manual(values=c(3, 16, 17))+ 
  scale_color_manual(values=c('#999999','#E69F00', '#56B4E9'))+
  theme(legend.position="top")
created at:10-01-2021
edited at: 10-01-2021: