ggplot:如何指定多个盒形图的垂直顺序? [英] ggplot: how to specify vertical order of multiple boxplots?

查看:417
本文介绍了ggplot:如何指定多个盒形图的垂直顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下图中更改医院的堆叠顺序,所以#1位于顶部,#4位于底部。



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

我需要top->顺序是与现在相反。我不确定为什么它现在是这样。 医院专栏是一个重要因素。



非常感谢!



解决方案

您有几个选项。最简单的方法是添加

  p + scale_x_reverse()

到图中。您也可以逆转因素的水平

  relevel(survey $ hospital,rev(levels(survey $ hospital))) >调查$ hospital 

或者,您可以使用 xlim()

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


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 ("\nPatient 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!

解决方案

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")

这篇关于ggplot:如何指定多个盒形图的垂直顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆