首页 > > > R语言可视化——面积(区域)图及其美化
今天要跟大家分享的是面积图,也就是经常提到的区域图。
本例选择自制数据集:
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) 
微信:ljty1991
博客主页:raindu's home
个人公众号:数据小魔方(datamofang)
团队公众号:EasyCharts
qq交流群:[魔方学院]553270834
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31537575/viewspace-3138786/ ,如需转载,请注明出处,否则将追究法律责任。