I am trying to reproduce this boxplots from a linear regression model output. my model and the code I used is below. I am getting this error "Aesthetics must be either length 1 or the same as the data (500)." What am I doing wrong? Please help!
out <- lm(L_SHUCK ~ L_VOLUME+CLASS, data = mydata)
r <- residuals(out)
ggplot(out , aes (x = CLASS , y = r, group = CLASS)) + geom_boxplot()
You can't really just put in the output of the lm
object to do this. You need to do something along these lines:
out = lm(mpg ~ wt + as.factor(am), mtcars)
r = residuals(out)
mtcars$residuals = r
ggplot(mtcars) + geom_boxplot(aes(x = as.factor(am), y = residuals))
I think a good ggplot2 tutorial would help you understand what is going on exactly, eg this one I wrote .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.