Currently I have a data frame where I want to plot three variables into one boxplot:
livingsetting factor outcome
1 1 CKD 2
2 1 CKD 13
3 1 CKD 23
4 13 CKD 12
5 7 CKD -14
The livingsetting variable contains factors "1", "7", and "13". The factor variable contains factors "CKD", "HD", and "Transplant". The outcome variable is a continuous outcome variable.
This is my code for the boxplot:
ggplot(df, aes(x = interaction(livingsetting, factor),
y= outcome)) + geom_boxplot(aes(fill = livingsetting)) + xlab("Factors")+ ylab("Y")
And my plot looks like this:
The x-axis labels show 1.CKD, 13.CKD, 7.CKD, 1.HD, 13.HD, etc., but is it possible to tweak the xlab part so that the boxplot shows "CKD", "HD", and "Transplant" as the labels? (so that each of the individual plots are grouped by threes).
For example, the first red, green, and blue plots will be labeled as "CKD" (as the group), the second red, green, and blue plots will be labeled as "HD", etc.
aes(x = factor, y = outcome, fill = livingsetting)
? Works only iflivingsetting
really is a factor, else transform to factor... If I understood correctly what you are looking for, you don't need theinteraction
. – Tino