用优雅的配色缔造图表专业主义

浏览: 1539

今天要跟大家分享的是如何在实际图表场景中运用ggtech包的配色及主题,案例是关于全球互联网公司市值比较(数据皆为真实数据,来源于搜狐网)。

不过这个包依赖的ggplot2版本需要很高才能搭配使用(仔细了解了下,貌似要开发版的ggplot2)

所以在开始本篇分享之前,你需要保证自己已经下载了开发版的ggplot2包

devtools

curl

以上两个是下载开发版ggplot2的必备包

然后运行以下代码

devtools::install_github('hadley/ggplot2')

也许会报错,你可能需要更新你的Rtools至最新版(Rtools34)。

如果实在没法下载成功开发版的ggplot2的话,也不要着急,据说ggplot2最新版本马上就要提供更新了,再稍微耐心等待几天,就会有更新提醒的。


因为基础的ggplot2语法已经介绍过了,这里我就不介绍具体步骤了,直接使用最终调试好的代码。

加载包:

library("ggplot2")

library("ggtech")

数据导入:


mydata <- read.table("clipboard", header = T, sep = '\t')

newdata<-mydata[1:5,]




柱形图(全球市值top5互联网公司 


数据截止2015年,单位:十亿美元


Airbnb风格:

ggplot(newdata,aes(reorder(Name,-Value),Value,fill=Name))+geom_bar(stat="identity")+

      theme_tech(theme="airbnb") + 

      scale_fill_tech(theme="airbnb") +

      labs(title="Top5 Internet Companies", 

      subtitle="Market value of Internet Co in 2015",

      caption = "http://www.sohu.com/")+

      theme(axis.title = element_blank(),

      legend.position=c(0.85,0.8)

      )


Esty风格:

ggplot(newdata,aes(reorder(Name,-Value),Value,fill=Name))+geom_bar(stat="identity")+

      theme_tech(theme="etsy") + 

      scale_fill_tech(theme="etsy") +

      labs(title="Top5 Internet Companies", 

      subtitle="Market value of Internet Co in 2015",

      caption = "http://www.sohu.com/")+

      theme(axis.title = element_blank(),

      legend.position=c(0.85,0.8)

      )




Fackbook风格:

ggplot(newdata[1:4,],aes(reorder(Name,-Value),Value,fill=Name))+geom_bar(stat="identity")+

      theme_tech(theme="facebook") +

      scale_fill_tech(theme="facebook") + 

      labs(title="Top5 Internet Companies", 

      subtitle="Market value of Internet Co in 2015",

      caption = "http://www.sohu.com/")+

      theme(axis.title = element_blank(),

      legend.position=c(0.85,0.8)

      )


Google风格:

ggplot(newdata[1:4,],aes(reorder(Name,-Value),Value,fill=Name))+geom_bar(stat="identity")+

      theme_tech(theme="google") + 

      scale_fill_tech(theme="google") + 

      labs(title="Top5 Internet Companies", 

      subtitle="Market value of Internet Co in 2015",

      caption = "http://www.sohu.com/")+

      theme(axis.title = element_blank(),

      legend.position=c(0.85,0.8)

      )


Twitter风格:

ggplot(newdata[1:4,],aes(reorder(Name,-Value),Value,fill=Name))+geom_bar(stat="identity")+

      theme_tech(theme="twitter") + 

      scale_fill_tech(theme="twitter") + 

      labs(title="Top5 Internet Companies", 

      subtitle="Market value of Internet Co in 2015",

      caption = "http://www.sohu.com/")+

      theme(axis.title = element_blank(),

      legend.position=c(0.85,0.8)

      )


以下用饼图来呈现前五大互联网公司的相对市值大小:

Airbnb风格:

ggplot(newdata,aes(x=1,y=Value,fill=Name))+

      geom_bar(stat="identity",color="white")+

      theme_tech(theme="airbnb") + 

      scale_fill_tech(theme="airbnb") +

      coord_polar(theta = "y",start=0)+

      labs(title="Top5 Internet Companies", 

      subtitle="Market value of Internet Co in 2015",

      caption = "http://www.sohu.com/")+

      guides(fill=guide_legend(title=NULL))+

      theme(

           panel.grid=element_blank(),

           panel.background=element_blank(),

           axis.text = element_blank(),

           axis.ticks = element_blank(),

           axis.title = element_blank(),

           axis.line=element_blank(),

           legend.position=c(0.1,0.1)

           )




Esty风格:

ggplot(newdata,aes(x=1,y=Value,fill=Name))+

      geom_bar(stat="identity",color="white")+

      theme_tech(theme="etsy") + 

      scale_fill_tech(theme="etsy") +

      coord_polar(theta = "y",start=0)+

      labs(title="Top5 Internet Companies", 

      subtitle="Market value of Internet Co in 2015",

      caption = "http://www.sohu.com/")+

      guides(fill=guide_legend(title=NULL))+

      theme(

           panel.grid=element_blank(),

           panel.background=element_blank(),

           axis.text = element_blank(),

           axis.ticks = element_blank(),

           axis.title = element_blank(),

           axis.line=element_blank(),

           legend.position=c(0.1,0.1)

           )


Fackbook风格:

ggplot(newdata[1:4,],aes(x=1,y=Value,fill=Name))+

      geom_bar(stat="identity",color="white")+

      theme_tech(theme="facebook") +

      scale_fill_tech(theme="facebook") + 

      coord_polar(theta = "y",start=0)+

      labs(title="Top5 Internet Companies", 

      subtitle="Market value of Internet Co in 2015",

      caption = "http://www.sohu.com/")+

      guides(fill=guide_legend(title=NULL))+

      theme(

           panel.grid=element_blank(),

           panel.background=element_blank(),

           axis.text = element_blank(),

           axis.ticks = element_blank(),

           axis.title = element_blank(),

           axis.line=element_blank(),

           legend.position=c(0.1,0.1)

           )


Google风格:

ggplot(newdata[1:4,],aes(x=1,y=Value,fill=Name))+

      geom_bar(stat="identity",color="white")+

      theme_tech(theme="google") + 

      scale_fill_tech(theme="google") + 

      coord_polar(theta = "y",start=0)+

      labs(title="Top5 Internet Companies", 

      subtitle="Market value of Internet Co in 2015",

      caption = "http://www.sohu.com/")+

      guides(fill=guide_legend(title=NULL))+

      theme(

           panel.grid=element_blank(),

           panel.background=element_blank(),

           axis.text = element_blank(),

           axis.ticks = element_blank(),

           axis.title = element_blank(),

           axis.line=element_blank(),

           legend.position=c(0.1,0.1)

           )


Twitter风格:

ggplot(newdata[1:4,],aes(x=1,y=Value,fill=Name))+

      geom_bar(stat="identity",color="white")+

      theme_tech(theme="twitter") + 

      scale_fill_tech(theme="twitter") + 

      coord_polar(theta = "y",start=0)+

      labs(title="Top5 Internet Companies", 

      subtitle="Market value of Internet Co in 2015",

      caption = "http://www.sohu.com/")+

      guides(fill=guide_legend(title=NULL))+

      theme(

           panel.grid=element_blank(),

           panel.background=element_blank(),

           axis.text = element_blank(),

           axis.ticks = element_blank(),

           axis.title = element_blank(),

           axis.line=element_blank(),

           legend.position=c(0.1,0.1)

           )


接下来用国内BAT三巨头的连续7年市值数据制作堆积的粗边面积图:

数据来源于http://www.14du.com/  截止2015年,单位:亿美元

导入数据:

mynewdata <- read.table("clipboard", header = T, sep = '\t')

使用reshape2包进行转置塑型:

library("reshape2")

newmydata <- melt(mynewdata, id.vars = c("Year"),variable.name = "Name", value.name = "Value")


粗边面积图:

Airbnb风格:

ggplot(newmydata,aes(Year,Value,group=Name,fill=Name))+

      geom_area(position="stack")+

      geom_line(col="grey60",size=2,position="stack")+

      theme_tech(theme="airbnb") + 

      scale_fill_tech(theme="airbnb") +

      labs(title="Three Big Giant of Internet Companies in China", 

      subtitle="Market value of Internet Co in China",

      caption = "http://www.sohu.com/")+

      guides(fill=guide_legend(title=NULL))+

      theme(

           panel.grid=element_blank(),

           panel.background=element_blank(),

           axis.title = element_blank(),

           legend.position=c(0.2,0.6)

           )


Esty风格:


ggplot(newmydata,aes(Year,Value,group=Name,fill=Name))+

      geom_area(position="stack")+

      geom_line(col="grey60",size=2,position="stack")+

      theme_tech(theme="etsy") + 

      scale_fill_tech(theme="etsy") +

      labs(title="Three Big Giant of Internet Companies in China", 

      subtitle="Market value of Internet Co in China",

      caption = "http://www.sohu.com/")+

      guides(fill=guide_legend(title=NULL))+

      theme(

           panel.grid=element_blank(),

           panel.background=element_blank(),

           axis.title = element_blank(),

           legend.position=c(0.2,0.6)

           )



Fackbook风格:


ggplot(newmydata,aes(Year,Value,group=Name,fill=Name))+

      geom_area(position="stack")+

      geom_line(col="grey60",size=2,position="stack")+

      theme_tech(theme="facebook") +

      scale_fill_tech(theme="facebook") +

      labs(title="Three Big Giant of Internet Companies in China", 

      subtitle="Market value of Internet Co in China",

      caption = "http://www.sohu.com/")+

      guides(fill=guide_legend(title=NULL))+

      theme(

           panel.grid=element_blank(),

           panel.background=element_blank(),

           axis.title = element_blank(),

           legend.position=c(0.2,0.6)

           )


Google风格:

ggplot(newmydata,aes(Year,Value,group=Name,fill=Name))+

      geom_area(position="stack")+

      geom_line(col="grey60",size=2,position="stack")+

      theme_tech(theme="google") + 

      scale_fill_tech(theme="google") + 

      labs(title="Three Big Giant of Internet Companies in China", 

      subtitle="Market value of Internet Co in China",

      caption = "http://www.sohu.com/")+

      guides(fill=guide_legend(title=NULL))+

      theme(

           panel.grid=element_blank(),

           panel.background=element_blank(),

           axis.title = element_blank(),

           legend.position=c(0.2,0.6)

           )




Twitter风格:

ggplot(newmydata,aes(Year,Value,group=Name,fill=Name))+

      geom_area(position="stack")+

      geom_line(col="orange",size=2,position="stack")+

      theme_tech(theme="twitter") + 

      scale_fill_tech(theme="twitter") + 

      labs(title="Three Big Giant of Internet Companies in China", 

      subtitle="Market value of Internet Co in China",

      caption = "http://www.sohu.com/")+

      guides(fill=guide_legend(title=NULL))+

      theme(

           panel.grid=element_blank(),

           panel.background=element_blank(),

           axis.title = element_blank(),

           legend.position=c(0.2,0.6)

           )




真是不好意思,没有刹住车,糊里糊涂的就写了这么多,也没有对代码做简化了,其实核心代码我就写了三个,其他都是Ctrl+C、Ctrl+V不停地狂点鼠标。

这个ggtech包前天刚分享过的,配色上很惊艳,很有科技范,非常适合用在商业数据分析中,说不定还能给你的领导带来惊喜呢,还等什么呢,赶快来试一试吧!



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

0 个评论

要回复文章请先登录注册