R语言可视化 |
您所在的位置:网站首页 › rggplot2箱线图 › R语言可视化 |
今天跟大家分享如何在R语言中利用ggplot函数制作箱线图及其美化。 箱线图也是经常会用到的用于呈现数据分布形态的重要的图表类型。 还是以ggplot2包内置的数据集为例进行案例演示: ggplot(mpg,aes(class,displ))+geom_boxplot()以上是最简单的形式,一个分类变量,一个连续性数值型;我们通过添加分类变量,让箱线图能够呈现更多的数据信息。 ggplot(diamonds,aes(cut,price,fill=color))+geom_boxplot()接下来我们还像往常一样,通过position参数的切换来验证boxplot图表的position参数使用规则: ggplot(diamonds,aes(cut,price,fill=color,alpha=1/30))+ geom_boxplot(position="identity")#当将位置设置为不做任何变换时,分类后的箱线图会在对应的X轴分类项相互叠加,造成遮挡阅读不便,不推荐此参数。 ggplot(diamonds,aes(cut,price,fill=color))+ geom_boxplot(position="stack")根据经验我们就可以猜到,估计使用堆积方式来呈现多维箱线图肯定会失败,因为即便真的将分类箱线图堆积起来,那么其呈现数据分布形态的功能也就丧失殆尽,果然软件是报错的。 ggplot(diamonds,aes(cut,price,fill=color))+ geom_boxplot(position="dodge")果然在添加有多分类变量时,箱线图默认使用的position参数是dodge。 ggplot(diamonds,aes(cut,price,fill=color))+ geom_boxplot(position="fill")同样将position参数设定为堆积百分比也毫无意义,软件没有通过并提出警示。 ggplot(diamonds,aes(cut,price,fill=color))+ geom_boxplot()+ facet_grid(.~color) ggplot(diamonds,aes(cut,price,fill=color))+ geom_boxplot()+ facet_wrap(~color)仔细体会两种分面函数效果的差异: 箱线图美化: 簇状: ggplot(diamonds,aes(cut,price,fill=color))+ geom_boxplot()+ ggtitle("Box Plot")+ theme_wsj()+ scale_fill_wsj()+ guides(fill=guide_legend(title=NULL)) ggplot(diamonds,aes(cut,price,fill=color))+ geom_boxplot()+ ggtitle("Box Plot")+ theme_economist()+ scale_fill_economist()+ guides(fill=guide_legend(title=NULL))分面: ggplot(diamonds,aes(cut,price,fill=color))+ geom_boxplot()+ ggtitle("Box Plot")+ theme_wsj()+ scale_fill_wsj()+ guides(fill=guide_legend(title=NULL))+ facet_grid(.~color) ggplot(diamonds,aes(cut,price,fill=color))+ geom_boxplot()+ ggtitle("Box Plot")+ theme_economist()+ scale_fill_economist()+ guides(fill=guide_legend(title=NULL))+ facet_grid(.~color) 联系方式:微信:ljty1991 博客主页:raindu's home 个人公众号:数据小魔方(datamofang) 团队公众号:EasyCharts qq交流群:[魔方学院]553270834 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |