R语言可视化 |
您所在的位置:网站首页 › 条形图倒序排列 › R语言可视化 |
今天跟大家分享多系列与分面组图的美化技巧! 昨天讲的关于多序列柱形图与条形图美化技巧,其实还漏掉了一些一点儿。 当数据序列比较多的时候,特别是超过四个以后,还用堆积柱形图(条形图)、或者簇状柱形图的话,图表必然会因为系列太多而受到挤压或者变形,整体就会不协调、不美观。 还有ggplot不支持次坐标轴功能,它的作图思维基本源于塔夫脱的可视化理念,而且作者个人的审美也接受次坐标轴(大牛任性),但是他留给大家解决多序列图表的方案是——分面组图~ data数据重塑(宽转长): mydata作图函数: ggplot(mydata,aes(Conpany,Sale,fill=Year))+ geom_bar(stat="identity",position="dodge")默认图表的配色确实挺难看的,这里我们使用华尔街日报、经济学人的主题、及配色模板。 ggplot(mydata,aes(Conpany,Sale,fill=Year))+ geom_bar(stat="identity",position="dodge")+ theme_wsj()+ scale_fill_wsj("rgby", "")+ theme(axis.ticks.length=unit(0.5,'cm'))+ guides(fill=guide_legend(title=NULL))+ ggtitle("The Financial Performance of Five Giant")+ theme(axis.title = element_blank())以上是我们使用传统的方法通过将颜色映射到不同类别的年度收入变量上,达到了区分效果,可是这样终究不是办法,五个序列实在是有点多,已经让然有点儿眼花缭乱了,如果有8个序列、10个序列呢,那又该怎么办呢~ 下面跟大家将其中一种比较有效的解决办法:通过分面组图解决多序列图表: 横排分面: 柱形分面(横排): ggplot(mydata,aes(Conpany,Sale,fill=Year))+ geom_bar(stat="identity",position="dodge")+ theme_wsj()+ scale_fill_wsj("rgby", "")+ theme(axis.ticks.length=unit(0.5,'cm'))+ guides(fill=guide_legend(title=NULL))+ ggtitle("The Financial Performance of Five Giant")+ theme(axis.title = element_blank(),legend.position='none')+ facet_grid(.~Year)条形分面(横排): ggplot(mydata,aes(Conpany,Sale,fill=Year))+ geom_bar(stat="identity",position="dodge")+ theme_wsj()+ scale_fill_wsj("rgby", "")+ theme(axis.ticks.length=unit(0.5,'cm'))+ guides(fill=guide_legend(title=NULL))+ ggtitle("The Financial Performance of Five Giant")+ theme(axis.title = element_blank(),legend.position='none')+ facet_grid(.~Year)+ coord_flip()竖排分面: 柱形分面(竖排): ggplot(mydata,aes(Conpany,Sale,fill=Year))+ geom_bar(stat="identity",position="dodge")+ theme_wsj()+ scale_fill_wsj("rgby", "")+ theme(axis.ticks.length=unit(0.5,'cm'))+ guides(fill=guide_legend(title=NULL))+ ggtitle("The Financial Performance of Five Giant")+ theme(axis.title = element_blank(),legend.position='none')+ facet_grid(Year~.)条形分面(竖排): ggplot(mydata,aes(Conpany,Sale,fill=Year))+ geom_bar(stat="identity",position="dodge")+ theme_wsj()+ scale_fill_wsj("rgby", "")+ theme(axis.ticks.length=unit(0.5,'cm'))+ guides(fill=guide_legend(title=NULL))+ ggtitle("The Financial Performance of Five Giant")+ theme(axis.title = element_blank(),legend.position='none')+ facet_grid(Year~.)+ coord_flip()关于簇状、分面图表数据标签问题: 昨天在讲解的时候忘记了图表数据标签这回事儿,而且当时确实也不太会处理这块儿,后来突然找到了处理方法: 簇状图标签数据处理: ggplot(mydata,aes(Conpany,Sale,fill=Year,label =Sale))+ geom_bar(stat="identity",position="dodge")+ theme_wsj()+ scale_fill_wsj("rgby", "")+ theme(axis.ticks.length=unit(0.5,'cm'))+ guides(fill=guide_legend(title=NULL))+ ggtitle("The Financial Performance of Five Giant")+ theme(axis.title = element_blank())+ geom_text(aes(y = Sale + 0.05), position = position_dodge(0.9), vjust = -0.5)横向分面柱图数据标签问题: ggplot(mydata,aes(Conpany,Sale,fill=Year,label =Sale))+ geom_bar(stat="identity",position="dodge")+ theme_wsj()+ scale_fill_wsj("rgby", "")+ theme(axis.ticks.length=unit(0.5,'cm'))+ guides(fill=guide_legend(title=NULL))+ ggtitle("The Financial Performance of Five Giant")+ theme(axis.title = element_blank(),legend.position='none')+ facet_grid(.~Year)+ geom_text(aes(y = Sale + 0.05), position = position_dodge(0.9), vjust = -0.5)横向分面条形图数据标签问题: ggplot(mydata,aes(Conpany,Sale,fill=Year,label =Sale))+ geom_bar(stat="identity",position="dodge")+ theme_wsj()+ scale_fill_wsj("rgby", "")+ theme(axis.ticks.length=unit(0.5,'cm'))+ guides(fill=guide_legend(title=NULL))+ ggtitle("The Financial Performance of Five Giant")+ theme(axis.title = element_blank(),legend.position='none')+ facet_grid(.~Year)+ geom_text(aes(y = Sale + 0.05), position = position_dodge(0.9), vjust = -0.5)+ coord_flip()竖向分面柱形图数据标签问题: ggplot(mydata,aes(Conpany,Sale,fill=Year,label =Sale))+ geom_bar(stat="identity",position="dodge")+theme_wsj()+ scale_fill_wsj("rgby", "")+ theme(axis.ticks.length=unit(0.5,'cm'))+ guides(fill=guide_legend(title=NULL))+ ggtitle("The Financial Performance of Five Giant")+ theme(axis.title = element_blank(),legend.position='none')+ facet_grid(Year~.)+ geom_text(aes(y = Sale + 0.05), position = position_dodge(0.9), vjust = -0.5)竖向分面条形图数据标签问题: ggplot(mydata,aes(Conpany,Sale,fill=Year,label =Sale))+ geom_bar(stat="identity",position="dodge")+ theme_wsj()+ scale_fill_wsj("rgby", "")+ theme(axis.ticks.length=unit(0.5,'cm'))+ guides(fill=guide_legend(title=NULL))+ ggtitle("The Financial Performance of Five Giant")+ theme(axis.title = element_blank(),legend.position='none')+ facet_grid(Year~.)+ geom_text(aes(y = Sale + 0.05), position = position_dodge(0.9), vjust = -0.5)+ coord_flip()微信:ljty1991 博客主页:raindu's home 个人公众号:数据小魔方(datamofang) 团队公众号:EasyCharts qq交流群:[魔方学院]553270834 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |