美文网首页R学习与可视化R绘图
ggplot2回顾(11): 主题设置

ggplot2回顾(11): 主题设置

作者: TOP生物信息 | 来源:发表于2019-12-01 16:10 被阅读0次

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就没有意义

相关文章

  • ggplot2回顾(11): 主题设置

    1. 内置主题 1.1 theme_gray() 默认主题:灰色背景,白色网格线 1.2 theme_bw() 黑...

  • ggplot添加theme

    1.利用ggplot2自带的主题设置 2.library(ggthemes)利用这个扩展包 ??ggthemes获...

  • ggplot2的主题设置

    ggplot2画图的时候有几个默认主题,画图的时候我们可以自己挑选一个喜欢的内置主题,也可以自己设置。其中内置主题...

  • ggplot2主题

    本文有两个部分: ggplot2 自带主题 主题包ggthemesggthemr ggplot2 自带主题 拓展主...

  • R可视化——ggplot2绘图过程中如何实现轴截断?

    绘制基础图形 1、加载绘图包——这里主要用到基础绘图包ggplot2和主题设置包ggprism。 2、数据——使用...

  • ggplot2画图之优化主题风格(BBC---bbplot)

    最近在看文章图片的时候,发现每张图片的背景主题都非常的清爽,不像ggplot2的主题设置,后来在看一些文章时发现,...

  • ggplot2学习(四)

    ggplot2基础(4)——主题 ggplot2基础(1)[https://www.jianshu.com/p/0...

  • 2020-05-18 GGplot2主题和拓展包

    ggplot2 Theme调整主题格式速查!!! 71个注册的ggplot2拓展包https://exts.ggp...

  • Notepad++配置

    设置 > 语言格式设置 Obsidian黑曜石主题+consolas 11号字体,绝对推荐,勾上“使用全局字体”“...

  • ggplot2学习(五)

    ggplot2基础(5)——配色与图例 参考《R数据可视化手册》、《R绘图:一文了解ggplot2颜色的设置[ht...

网友评论

    本文标题:ggplot2回顾(11): 主题设置

    本文链接:https://www.haomeiwen.com/subject/ezbzwctx.html