ggplot two way anova line chart

hello, I am trying to make a line chart of a model with two binary X and a continous Y. When I specify one of X as the x axis and in continuous scale, the plot is mostly fine, only that the x axis has other values between 0 and 1 (because I specified it as continuous). The code looks like below:

ggplot(mtcars,aes(y=mpg,x=vs,color=as.factor(gear)))+stat_summary(fun='mean',geom='line')

Since the x axis contains other values between 0 and 1, I specify X as a binary variable, but the code didnt work:

ggplot(mtcars,aes(y=mpg,x=as.factor(vs),color=as.factor(gear)))+stat_summary(fun='mean',geom='line')
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

Why is that? Please give me some advice. Thank you very much.

your first solution, you can control the break points displayed on the x axis

ggplot(mtcars,aes(y=mpg,x=vs,color=as.factor(gear)))+
  stat_summary(fun='mean',geom='line') +
  scale_x_continuous(breaks=c(0,1))
1 Like

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