Boxplot with adjustment for variable
1
1
Entering edit mode
8 weeks ago
Bine ▴ 90

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 • 504 views
ADD COMMENT
0
Entering edit mode

it is not really working for me

Please share example data and codes.

ADD REPLY
1
Entering edit mode
8 weeks ago
zx8754 12k

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")
ADD COMMENT
0
Entering edit mode

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!

ADD REPLY
0
Entering edit mode

Yes, we are plotting the residuals.

ADD REPLY

Login before adding your answer.

Traffic: 2516 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6