ggplot2包的学习就要告一段落了~
图表字体样式
library(ggplot2)
library(gcookbook)
p=ggplot(heightweight,aes(x=ageYear,y=heightIn))+geom_point()
p+ggtitle("Age and Height\n of Schoolchildren")
p+theme(axis.title.x=element_text(size=16,lineheight = .9,face="bold.italic",colour="red"))
\n换行。
图表主题样式
p+theme_bw()
p+theme_grey()
p+theme(panel.grid.major = element_line(colour="red"),
panel.grid.minor=element_line(colour="red",linetype = "dashed",size=0.2),
panel.background=element_rect(fill="lightblue"),
panel.border=element_rect(colour="blue",fill=NA,size=2))
注:默认为灰色图,panel.grid.major为主要网格线,panel.grid.minor为次要网格线。
p+ggtitle("Title")+
theme(axis.title.x = element_text(colour="red",size=14,face="bold"),
axis.text.x=element_text(colour="blue"),
axis.title.y=element_text(colour="red",size=14,angle=90),
axis.text.y=element_text(colour="red",size=20,face="bold"))
axis.text.x/y表示坐标轴上刻度的样式。
图例主题样式
p=ggplot(heightweight,aes(x=ageYear,y=heightIn,colour=sex))+geom_point()
p+theme(
legend.background=element_rect(fill="grey85",colour="red",size=1),
legend.title=element_text(colour="blue",face="bold",size=14),
legend.text=element_text(colour="red"),
legend.key=element_rect(colour="green",fill="black",size=0.25))
legend.background表示图例背景、图框;legend.title小标题;legend.text里面种类标注;legend.key表示种类图例背景、图框。
种类标注样式
p=ggplot(heightweight,aes(x=ageYear,y=heightIn,colour=sex))+geom_point()
p+facet_grid(sex~.)+theme(
strip.background=element_rect(fill="pink"),
strip.text.y=element_text(size=14,angle=-90,face="bold"))
strip.background表示种类得颜色(那个粉色的条条),strip.text.y表示条条里字体的样式,当图为垂直排列时,用strip.text.x;
隐藏网格线
p+theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank())
p+theme(panel.grid.major.x = element_blank(),panel.grid.minor.x = element_blank())
隐藏所有网格线或是隐藏横向网格线。
自定义图表样式
mytheme=theme_bw+
theme(text=element_text(colour="red"),
axis.title=element_text(size=rel(1.25)),
plot.title=element_text(size=rel(3)))
p+mytheme
创建自己的样式,随时可用。rel(1.25)表示为原来的1.25倍大小。
参考资料:
1.gcookbook