前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言可视化——柱形图美化(簇状、堆积、百分比)

R语言可视化——柱形图美化(簇状、堆积、百分比)

作者头像
数据小磨坊
发布2018-04-11 11:55:04
5.6K0
发布2018-04-11 11:55:04
举报
文章被收录于专栏:数据小魔方

昨天以最简单的单序列柱形图作为对象详细的讲解了关于套用主题以及图表美化的思路。

今天就我们常用的几种柱形图的衍生图表——簇状柱形图、堆积柱形图、百分比堆积柱形图的美化工作进行讲解。

我们还是以昨天的数据作为演示数据,同时添加两年度数据。

data<-data.frame(Name = c("苹果","谷歌","脸书","亚马逊","腾讯"),Conpany = c("Apple","Google","Facebook","Amozon","Tencent"),Sale2015 = c(5000,3500,2300,2100,3100),Sale2016 = c(5050,3800,2900,2500,3300))

由于今天的案例数据中有两个年份的数据,其实算是汇总过的二维表(宽数据),不符合R语言图表数据源的结构(一维表、长数据),所以需要使用reshape2包中的melt函数对数据进行重塑,将其变为长数据进行作图:

library(reshape2)

mydata <- melt(data1,id.vars="Conpany",variable.name="Year",value.name="Sale")

接下来就要使用语法作图喽,一定要瞪大眼睛哦~

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")+ggtitle("The Financial Performance of Five Giant")+theme_wsj()+scale_fill_wsj()

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+ggtitle("The Financial Performance of Five Giant")+theme_wsj()+scale_fill_wsj("rgby", "")

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+ggtitle("The Financial Performance of Five Giant")+theme_wsj()+scale_fill_wsj("rgby", "")+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+ggtitle("The Financial Performance of Five Giant")+theme_economist(base_size=14)+scale_fill_economist()+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))

堆积柱形图套用主题:

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="stack")+ggtitle("The Financial Performance of Five Giant")+theme_wsj()+scale_fill_wsj("rgby", "")+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="stack")+ggtitle("The Financial Performance of Five Giant")+theme_economist(base_size=14)+scale_fill_economist()+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))

百分比堆积柱形图套用主题:

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="fill")+ggtitle("The Financial Performance of Five Giant")+theme_wsj()+scale_fill_wsj("rgby", "")+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="fill")+ggtitle("The Financial Performance of Five Giant")+theme_economist(base_size=14)+scale_fill_economist()+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))

将以上所有图表通过添加旋转参数调整为条形图:

簇状条形形图:

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+ggtitle("The Financial Performance of Five Giant")+theme_wsj()+scale_fill_wsj("rgby", "")+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+coord_flip()

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="dodge")+ggtitle("The Financial Performance of Five Giant")+theme_economist(base_size=14)+scale_fill_economist()+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+coord_flip()

堆积条形图:

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="stack")+ggtitle("The Financial Performance of Five Giant")+theme_wsj()+scale_fill_wsj("rgby", "")+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+coord_flip()

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="stack")+ggtitle("The Financial Performance of Five Giant")+theme_economist(base_size=14)+scale_fill_economist()+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+coord_flip()

百分比堆积条形图:

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="fill")+ggtitle("The Financial Performance of Five Giant")+theme_wsj()+scale_fill_wsj("rgby", "")+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+coord_flip()

ggplot(mydata,aes(Conpany,Sale,fill=Year))+geom_bar(stat="identity",position="fill")+ggtitle("The Financial Performance of Five Giant")+theme_economist(base_size=14)+scale_fill_economist()+theme(axis.ticks.length=unit(0.5,'cm'))+guides(fill=guide_legend(title=NULL))+coord_flip()

以上还有好多可以修改的细节,比如旋转参数只旋转了绘图区,而x轴刻度线以及Y轴网格线仍然没有调整。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2016-09-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据小魔方 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档