Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
691 views
in Technique[技术] by (71.8m points)

r - ggplot: how to specify vertical order of multiple boxplots?

I would like to change the stacking order of hospitals in the diagram below so #1 is at the top and #4 at the bottom.

The diagram was produced with

ggplot(survey,aes(x=hospital, y=age))+geom_boxplot()+coord_flip()+xlab("")+ylab ("
Patient Age")

and I need the top->down order to be the reverse of what is now. I'm not sure why it comes the way it does now. The 'hospital' column is a factor in case it matters.

Many thanks!

alt text

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have a few options. The easiest would be to add

p + scale_x_reverse()

to the plot. You could also reverse the levels of the factor

relevel(survey$hospital, rev(levels(survey$hospital))) -> survey$hospital

Or, you could determine the order you want the boxes by hand with xlim()

p + xlim("Hospital #4","Hospital #3","Hospital #2","Hospital #1")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...