查看原文
其他

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

2016-11-03 小魔方 数据小魔方

前天刚恢复状态的小魔方,今天又粗现啦~


很多小伙伴儿问我为啥最近老是偷懒,其实是因为上周期中课程论文缠身(毕竟拖延症嘛总得拖到deadline否则绝不开工~_~)……


不过最近也思考了些东西,关于以后公众号运营方向以及推送内容的。


总觉得以前自己写的东西太过琐碎,纠结于步骤和技巧,而没有任何体系上和理念上的输出;


而再加上自己文风粗陋(毕竟每一篇的错别字用十个手指头都数不完呀)、排版拙劣,还有那么多小伙伴儿不离不弃,肯于点赞,实在是感到惭愧(我一般都很少看自己写的东西的不忍直视哈哈)~


可能之前限于能力和实力,一直在跟着别人的步调走,始终在模仿;再加上自己过于追求数量,在质量上用心不多。


不过现在自我感觉好一些了,开始慢慢的思考一些深度的技巧运用,偶尔也会有一些纯理念的总结。


以后的更新频率打算保持在一周2~3篇左右,但是每一篇的内容会向着综合性、应用性方向转变。当然,关于内容排版和文章封面图都会多花一些心思,尽可能的提升一下阅读体验。


下面是今天的干货~



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


因为基础的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+V、Ctrl+V不停地狂点鼠标。


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


魔方学院QQ群:




QQ群:

微信群:


您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存