查看原文
其他

R语言可视化——面积(区域)图及其美化

2016-09-26 小魔方 数据小魔方

今天要跟大家分享的是面积图,也就是经常提到的区域图。


本例选择自制数据集:


head(data5)




ggplot(data,aes(x,y))+geom_area(fill="steelblue")




ggplot(data5,aes(Year,Sale,fill=Fac))+geom_area()





默认情况下,加入分类变量之后的面积图的位置调整参数为堆积:我们可以通过添加位置参数position进行确认:


ggplot(data5,aes(Year,Sale,fill=Fac))+geom_area(position="stack")




面积图图层位置调整与之前介绍过的一样,都可以通过调整position内的参数完成:


ggplot(data5,aes(Year,Sale,fill=Fac))+geom_area(position="identity",alpha=0.1)

 



#不做任何位置变换,但是由于面积图区域图层间相互遮挡,造成阅读不便。


当然,面积图也不适合使用dodge参数的,dodge参数要求横坐标为离散刻度(即不可以相互遮挡)


ggplot(data5,aes(Year,Sale,fill=Fac))+geom_area(position="dodge") 



ggplot(data5,aes(Year,Sale,fill=Fac))+geom_area(position="fill")   #百分比堆积




分面表达:


ggplot(data5,aes(Year,Sale,fill=Fac))+geom_area(position="stack")+ facet_grid(.~Fac)




ggplot(data5,aes(Year,Sale,fill=Fac))+geom_area(position="stack")+ facet_wrap(~Fac)




注意仔细体会两种分面形式的差别:



美化:


堆积


ggplot(data5,aes(Year,Sale,fill=Fac))+geom_area(position="stack")+ggtitle("Area Plot")+theme_wsj()+scale_fill_wsj()+guides(fill=guide_legend(title=NULL))  




ggplot(data5,aes(Year,Sale,fill=Fac))+geom_area(position="stack")+ggtitle("Area Plot")+theme_economist()+scale_fill_economist()+guides(fill=guide_legend(title=NULL))  





百分比堆积:


ggplot(data5,aes(Year,Sale,fill=Fac))+geom_area(position="fill")+ggtitle("Area Plot")+theme_wsj()+scale_fill_wsj()+guides(fill=guide_legend(title=NULL))  




ggplot(data5,aes(Year,Sale,fill=Fac))+geom_area(position="fill")+ggtitle("Area Plot")+theme_economist()+scale_fill_economist()+guides(fill=guide_legend(title=NULL))  





分面:


ggplot(data5,aes(Year,Sale,fill=Fac))+geom_area(position="stack")+ggtitle("Area Plot")+theme_wsj()+scale_fill_wsj()+guides(fill=guide_legend(title=NULL)) +facet_grid(.~Fac) 




ggplot(data5,aes(Year,Sale,fill=Fac))+geom_area(position="stack")+ggtitle("Area Plot")+theme_economist()+scale_fill_economist()+guides(fill=guide_legend(title=NULL))  +facet_grid(.~Fac) 




魔方学院QQ群:


QQ群:

微信群:




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

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