This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Boxplot with adjustment for variable

Good afternoon,

I would like to create a boxplot, e.g. with y= score and y=Sex. This is not a problem with:

ggplot(data = mydata, aes(x=Sex, y=score)) + geom_boxplot(aes(),show.legend = TRUE) 

However, now I want to "adjust" my boxplot for age so that I can better compare the effect between males and females without effect of age.

I read this post, however it is not really working for me:

https://stats.stackexchange.com/questions/513295/how-to-plot-a-box-plot-that-accounts-for-the-effect-of-another-factor

Has someone an idea?

Thank you!

r boxplot

it is not really working for me

Please share example data and codes.

1 answer

Using the data from the linked post:

# head(X)
#            type income education prestige   Profession   Residual
# accountant prof     62        86       82 White collar  -2.346348
# pilot      prof     72        76       83 White collar   7.352559
# architect  prof     75        92       90 White collar   5.470282
# author     prof     55        90       76 White collar  -5.733058
# chemist    prof     64        86       90 White collar  -5.336139
# minister   prof     21        84       87 White collar -46.400441

Here are the same plots using ggplot:

library(ggplot2)

ggplot(X, aes(income, Profession)) +
  geom_boxplot() +
  scale_x_continuous(name = "Percent exceeding income threshold")

ggplot(X, aes(Residual, Profession)) +
  geom_boxplot() +
  geom_vline(xintercept = 0, linetype = "dashed") +
  scale_x_continuous(name = "Difference of the Percent")

Thank you so much for your answer. So I understand that you used these lines from the post I shared to calculate the residual, correct?

  Step 1:  fit <- lm(income ~ education + prestige, X)

Step 2: X$Residual <- residuals(fit) # Take out the effects of education and prestige

I am able to plot the boxplots now with your code without error :) Awesome!

Just for my understanding: In step 1 we build a model of the effect of education and prestige on income. In step 2 we remove the effect education and prestige on income

Correct?

Thanks again!

Yes, we are plotting the residuals.

Log in to answer this question.