用优雅的配色来美化你的柱形图

浏览: 2616

今天跟大家分享常用的几种柱形图衍生图表美化技巧——簇状柱形图、堆积柱形图、百分比堆积柱形图。

以下是手动创建的案例数据:

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语言ggplot2图表数据源的结构(一维表、长数据),所以需要使用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轴网格线仍然没有调整。


我是分割线~






                     

推荐 0
本文由 EasyCharts 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册