1. 内置主题
1.1 theme_gray()
默认主题:灰色背景,白色网格线
1.2 theme_bw()
黑白主题:白色背景,灰色网格线
这两种主题都由参数base_size控制字体大小,eg: theme_bw(base_size = 20)
1.3 主题设置
全局性设置
theme_set(theme_bw()) #设置之后,下文的所有图都会启用这种主题
old <- theme_set(theme_bw()) #这一条命令可以返回设置新主题之前的那个主题,同时也设置了新主题
p #会采用新主题,p为图形对象
p+old #采用旧主题
局部性设置
p+theme_gray()
2. 自定义主题
dis1 <- read.table("S20191017P12_dis.txt",header = F)
colnames(dis1) <- c("No.cell","UMI")
dis1$class <- factor((dis1$UMI > 2500)+0)
dis1%>%ggplot(aes(log(No.cell,100),UMI))+geom_point(aes(color=dis1$class))+
labs(title = "主标题",subtitle = "副标题")+
scale_y_continuous("UMI值")+scale_x_continuous("log100(细胞序数)")+
scale_color_hue("class",breaks=c("0","1"),labels=c("low","high"))+
theme(
axis.line.y.left = element_line(colour="lightblue",size=2,linetype = "dashed",lineend = "square"),
axis.ticks = element_line(colour="black",size=1),axis.ticks.length = unit(0.25,"cm"),
axis.text.y.left = element_text(family = NULL, face = NULL, colour = "red", size = 14, hjust = NULL, vjust = NULL, angle = 45),
axis.title.x = element_text(colour = "green"),axis.title.y = element_text(colour = "green"),
legend.background = element_rect(fill = "lightblue",color = "black",size = 1,linetype = "dashed"),
legend.key = element_rect(fill = "white",color = "black",size = 0.5,linetype = "dashed"),
legend.text = element_text(colour = "red", size = 14),
legend.title = element_text(colour = "red", size = 18),
panel.background = element_rect(fill = "black",color = NA),
panel.grid.major = element_blank(),panel.grid.minor = element_blank(),
plot.background = element_rect(fill = "grey80"),
plot.title = element_text(hjust = 0.5,size=20),plot.subtitle = element_text(hjust = 0.5,size = 18)
)
2.1 参数详解1
axis.line
相关的参数共有7个,分别调整:四边整体、上下两边、左右两边、上下左右四条边分开,可以用来控制坐标轴的线条类型
axis.ticks, axis.ticks.length
用来调整坐标轴的刻度线的外形和长度
axis.text
相关的参数共有7个,可以用来控制坐标轴的刻度标签
axis.title.x, axis.title.y
坐标轴标题
legend.background
图例区背景
legend.key
图例背景
legend.text
图例标签
legend.title
图例标题
panel.background
主画板背景
panel.grid.major, panel.grid.minor
控制主次网格线,仍然应该采用element_line(),而element_blank()表示清空
plot.background
控制整个图纸的背景
plot.title, plot.subtitle
主副标题, element_text()类型
dis1%>%ggplot(aes(x=log(No.cell,100),y=UMI,color=class))+geom_point()+
facet_grid(.~dis1$class)+theme(
strip.background = element_rect(fill = "lightblue",color = "black"),
strip.text.x = element_text(size = 20)
)
2.2 参数详解2
strip.background
分面标签背景
strip.text.x
水平条状文本,上图中就是分面标签文本
strip.text.y
竖直条状文本
2.3 主题设置
全局设置
p <- dis1%>%ggplot(aes(x=log(No.cell,100),y=UMI,color=class))+geom_point()+
facet_grid(.~dis1$class)
p #采用默认主题theme_grey()
old_theme <- theme_update(
strip.background = element_rect(fill = "lightblue",color = "black"),
strip.text.x = element_text(size = 20)
)
p #采用更新后的主题
p+old #采用默认主题theme_grey()
theme_set(old_theme)#恢复原来的主题
p #采用默认主题theme_grey()
局部设置
p+theme(...)
3. 总结
共有四种函数,element_text()、element_line()、element_rect()、element_blank(),常见参数如下:
element_text(): family, face, colour, size, hjust, vjust, angle
element_rect(): fill, color,size,linetype #后面三个是矩形边框的属性
element_line(): colour,size,linetype,lineend #最后一个是指线条结束的地方是圆的还是方的
element_blank(): 清空
另外还需要注意,主题元素必须存在才能调整,比如如果本身没有主/副标题,调整plot.title, plot.subtitle就没有意义