https://blog.csdn.net/qq_36654309/article/details/83961025接着簇状条形图讲起。
当变量较多时,上面的画法就有些拥挤。
解决的方法主要有:
横排分面
柱形分面(横排):
ggplot(mydata,aes(Conpany,Sale,fill=Year))+
geom_bar(stat="identity",position="dodge")+
theme_wsj()+
scale_fill_wsj("rgby", "")+
guides(fill=guide_legend(title=NULL))+
ggtitle("The Financial Performance of Five Giant")+
theme(axis.title = element_blank(),legend.position='none')+
facet_grid(.~Year) #分面相关设置:facets表示形式为:~变量(~单元格)
ggplot(mydata,aes(Conpany,Sale,fill=Year))+
geom_bar(stat="identity",position="dodge")+
facet_grid(.~Year)+
theme_economist(base_size=14)+
scale_fill_economist()+
guides(fill=guide_legend(title=NULL))+
ggtitle("The Financial Performance of Five Giant")+
theme(
axis.title = element_blank(), #轴标题
legend.position='none',
axis.text.x=element_text(size=7.5)
)
条形分面(横排):
ggplot(mydata,aes(Conpany,Sale,fill=Year))+
geom_bar(stat="identity",position="dodge")+
theme_wsj()+
scale_fill_wsj("rgby", "")+
guides(fill=guide_legend(title=NULL))+
facet_grid(.~Year)+
coord_flip()+ #坐标系相关设置
ggtitle("The Financial Performance of Five Giant")+
theme(
axis.title = element_blank(),
legend.position='none',
axis.text.y=element_text(size=10),
axis.text.x=element_text(size=7.5),
axis.ticks.x=element_blank(),
axis.ticks.y=element_line(),
axis.ticks.length=unit(0.5,'cm'),
axis.line.y=element_line(),
axis.line.x=element_blank(),
panel.grid.major.x=element_line(linetype="dashed",colour="grey60"),
panel.grid.major.y=element_blank()
)
ggplot(mydata,aes(Conpany,Sale,fill=Year))+
geom_bar(stat="identity",position="dodge")+
theme_economist(base_size=14)+
facet_grid(.~Year)+
coord_flip()+
scale_fill_economist()+
theme(axis.ticks.length=unit(0.5,'cm'))+
guides(fill=guide_legend(title=NULL))+
ggtitle("The Financial Performance of Five Giant")+
theme(
axis.title = element_blank(),
legend.position='none',
axis.text.y=element_text(size=10),
axis.text.x=element_text(size=7.5),
axis.line.x=element_blank(),
axis.line.y=element_line(),
axis.ticks.x=element_blank(),
axis.ticks.y=element_line(),
axis.ticks.length=unit(0.5,'cm'),
panel.grid.major.x=element_line(linetype="solid",colour="white",size=1.1),
panel.grid.major.y=element_blank()
)
#竖排分面:
#柱形分面(竖排):
#左
p1<-ggplot(mydata,aes(Conpany,Sale,fill=Year))+
geom_bar(stat="identity",position="dodge")+
theme_wsj()+
scale_fill_wsj("rgby", "")+
theme(axis.ticks.length=unit(0.3,'cm'))+
guides(fill=guide_legend(title=NULL))+
ggtitle("The Financial Performance of Five Giant")+
theme(axis.title = element_blank(),legend.position='none')+
facet_grid(Year~.)
#右
p2<-ggplot(mydata,aes(Conpany,Sale,fill=Year))+
geom_bar(stat="identity",position="dodge")+
theme_economist(base_size=14)+
facet_grid(Year~.)+
scale_fill_economist()+
theme(axis.ticks.length=unit(0.3,'cm'))+
guides(fill=guide_legend(title=NULL))+
ggtitle("The Financial Performance of Five Giant")+
theme(
axis.title = element_blank(),
legend.position='none',
axis.text.y=element_text(size=7.5),
axis.text.x=element_text(size=7.5,face="bold"),
strip.text=element_text(size=10)
)
#使用multiplot函数进行版面合并:
library(grid)
require(grid)
install.packages("Rmisc")
library(lattice)
library(plyr)
library("Rmisc")
multiplot(p1,p2,cols=2)
ggplot(mydata,aes(Conpany,Sale,fill=Year))+
geom_bar(stat="identity",position="dodge")+
theme_wsj()+
facet_grid(Year~.)+
coord_flip()+
scale_fill_wsj("rgby", "")+
theme(axis.ticks.length=unit(0.5,'cm'))+
guides(fill=guide_legend(title=NULL))+
ggtitle("The Financial Performance of Five Giant")+
theme(
axis.title = element_blank(),
legend.position='none',
panel.grid.major.x=element_line(linetype="dashed",colour="grey60"),
panel.grid.major.y=element_blank(),
axis.ticks.x=element_blank(),
axis.ticks.y=element_line(),
axis.ticks.length=unit(0.3,'cm'),
axis.line.x=element_blank(),
axis.line.y=element_line(),
axis.text.x=element_text(size=10),
)
ggplot(mydata,aes(Conpany,Sale,fill=Year))+
geom_bar(stat="identity",position="dodge")+
facet_grid(Year~.)+
coord_flip()+
theme_economist(base_size=14)+
scale_fill_economist()+
guides(fill=guide_legend(title=NULL))+
ggtitle("The Financial Performance of Five Giant")+
theme(
axis.title = element_blank(),
legend.position='none',
panel.grid.major.x=element_line(linetype=1,colour="white",size=1.1),
panel.grid.major.y=element_blank(),
axis.ticks.x=element_blank(),
axis.ticks.y=element_line(),
axis.ticks.length=unit(0.2,'cm'),
axis.line.x=element_blank(),
axis.line.y=element_line(),
axis.text.x=element_text(size=10),
strip.text=element_text(size=10),
axis.text.y=element_text(size=10)
)
ggplot(mydata,aes(Conpany,Sale,fill=Year,label =Sale))+
geom_bar(stat="identity",position="dodge")+
theme_wsj()+
scale_fill_wsj("rgby", "")+
guides(fill=guide_legend(title=NULL))+
ggtitle("The Financial Performance of Five Giant")+
geom_text(aes(y = Sale + 0.05), position =position_dodge(0.9),fontface="bold", vjust =1,colour="white",size=2)+
theme(
axis.title = element_blank(),
axis.ticks.length=unit(0.3,'cm'),
axis.text.x=element_text(size=10),
axis.text.y=element_text(size=10)
)
#横向分面柱图数据标签问题:
ggplot(mydata,aes(Conpany,Sale,fill=Year,label =Sale))+
geom_bar(stat="identity",position="dodge")+
theme_wsj()+
facet_grid(.~Year)+
scale_fill_wsj("rgby", "")+
guides(fill=guide_legend(title=NULL))+
ggtitle("The Financial Performance of Five Giant")+
geom_text(aes(y = Sale + 0.05), position =position_dodge(0.9),fontface="bold", vjust =1,colour="white",size=2)+
theme(
axis.title = element_blank(),
legend.position='none',
axis.ticks.length=unit(0.3,'cm'),
axis.line.x=element_line(),
axis.text.x=element_text(size=5),
axis.text.y=element_text(size=7.5)
)
ggplot(mydata,aes(Conpany,Sale,fill=Year,label =Sale))+
geom_bar(stat="identity",position="dodge")+
coord_flip()+facet_grid(.~Year)+
theme_wsj()+scale_fill_wsj("rgby", "")+
guides(fill=guide_legend(title=NULL))+
ggtitle("The Financial Performance of Five Giant")+
geom_text(aes(y = Sale + 0.05), position = position_dodge(0.9),hjust=1.2,vjust=0.5,colour="white",size=4)+
theme(
axis.title = element_blank(),
legend.position='none',
panel.grid.major.x=element_line(linetype="dashed",colour="grey60"),
panel.grid.major.y=element_blank(),
axis.ticks.x=element_blank(),
axis.ticks.y=element_line(),
axis.ticks.length=unit(0.3,'cm'),
axis.line.x=element_blank(),
axis.line.y=element_line(),
axis.text.x=element_text(size=8),
axis.text.y=element_text(size=10)
)
#竖向分面条形图数据标签问题:
ggplot(mydata,aes(Conpany,Sale,fill=Year,label =Sale))+
geom_bar(stat="identity",position="dodge")+
theme_wsj()+scale_fill_wsj("rgby", "")+
facet_grid(Year~.)+coord_flip()+
guides(fill=guide_legend(title=NULL))+
ggtitle("The Financial Performance of Five Giant")+
geom_text(aes(y = Sale + 0.05), position = position_dodge(0.9),hjust=1.1,colour="white",size=3.5)+
theme(
axis.title = element_blank(),
legend.position='none',
panel.grid.major.x=element_line(linetype="dashed",colour="grey60"),
panel.grid.major.y=element_blank(),
axis.ticks.x=element_blank(),
axis.ticks.y=element_line(),
axis.ticks.length=unit(0.25,'cm'),
axis.line.x=element_blank(),
axis.line.y=element_line(),
axis.text.y=element_text(size=10),
axis.text.x=element_text(size=10)
)