主要内容:
定义轴标签,图例,主标题名称,按组分面
移除所有标签
自定义字体,颜色,主题
自定义顺序、更改起始刻度
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
len supp dose
1 4.2 VC 0.5
2 11.5 VC 0.5
3 7.3 VC 0.5
4 5.8 VC 0.5
5 6.4 VC 0.5
6 10.0 VC 0.5
library(ggplot2)
p <- ggplot(ToothGrowth,aes(dose,len,fill=dose)) +
geom_bar(stat="identity",width = 0.5)+
ggtitle("Plot of length by dose")+
theme(plot.title = element_text(hjust = 0.5))+
#设置主标题居中
xlab("supp")+ylab("len")+labs(fill = "lenge")
![](https://img.haomeiwen.com/i16360488/aa8ad6723e39aa86.png)
#自定义x,y标题字体,刻度字体,图例字体
p1 <- p + theme(plot.title = element_text(color="red", family = "Times",
size=14, face="bold.italic"),
axis.title.x = element_text(color="blue",family = "Times",
size=14, face="bold"),
axis.title.y = element_text(color="#993333", family = "Times",
size=14, face="bold"))+
theme(axis.text.x=element_text(family = "Times",
face = "plain",colour = "black",size=10))+
theme(axis.text.y=element_text(family = "Times",
face = "plain",colour = "black",size=10))+
theme(legend.text=element_text(face="plain",family = "Times",
colour = "black",size = 10))
p1
![](https://img.haomeiwen.com/i16360488/b0a12c1860d1393d.png)
#移除x,y,标题,图例名称
p2 <- p1 + theme(plot.title = element_blank(),
axis.title.x = element_blank(),axis.title.y = element_blank())+
theme(legend.position="top")+
theme(legend.text=element_text(face="plain",family = "Times",
colour = "black",size = 10))+
theme(legend.title = element_blank())
p2
![](https://img.haomeiwen.com/i16360488/6512c3e93192ff6e.png)
p3 <- p2 +scale_x_discrete(limits=c("2", "0.5", "1"))+ #更改柱子顺序
theme(legend.position='none')+ #移除图例
scale_fill_brewer(palette="Dark2")+ #使用调色板填充
scale_color_brewer(palette="Set2")
p3
![](https://img.haomeiwen.com/i16360488/2d2aedf7cd82a8fa.png)
#设置主题
p + theme_test()+ theme(plot.title = element_text(hjust = 0.5))
#设置分面
p + theme_test()+ theme(plot.title = element_text(hjust = 0.5))+
facet_grid(. ~ supp)
![](https://img.haomeiwen.com/i16360488/7fdb6c016c088ef3.png)
#定义X轴文本斜体,并且从0刻度开始
p + theme(axis.text.x = element_text(angle = 30,hjust=1,vjust=1))+
expand_limits(x=0,y=0)+scale_y_continuous(expand=c(0,0))
![](https://img.haomeiwen.com/i16360488/f26185750cda6c0f.png)
网友评论