2024-12-11
纯生信适合入门,高阶生信分析和数据挖掘仍需要 R 语言或 Python。为此我们不断完善教程,相继推出了纯生信21天,R 语言21天和单细胞21天。但是,数据处理的核心部分并没有变,数据清洗+数据可视化!前面分享了数据清洗 tidyverse 包中的10个高频操作,接下来,我们重点分享 ggplot2 包中的理论和实操!
# 映射的相关参数,榴莲整理,特此感谢!
aes()函数中常见的映射选项是:
x和y:用于指定x轴和y轴映射的变量
color:映射点或线的颜色
fill:映射填充区域的颜色
linetype:映射图形的线形(1=实线、2=虚线、3=点、4=点破折号、5=长破折号、6=双破折号)
size:点的尺寸和线的宽度
shape:映射点的形状
## 加载包和数据
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
load(file = "test.rds")
## 基本绘图
(g <- ggplot(exprSet1, aes(x = GSM180994, y = GSM180995)))
g + geom_point()
g + geom_line()
g + geom_line() + geom_point()
# 调整几何图形的属性
g + geom_point(color = "firebrick", shape = "diamond", size = 2)
g + geom_point(color = "firebrick", shape = "diamond", size = 2) +
geom_line(color = "firebrick", linetype = "dotted", lwd = .3)
# 替换默认的ggplot2主题
theme_set(theme_bw())
g + geom_point(color = "firebrick")
## 调整坐标轴
# 修改title
ggplot(exprSet1, aes(x = GSM180994, y = GSM180995)) +
geom_point(color = "firebrick") +
labs(x = "Sample No.1", y = "Sample No.2")
ggplot(exprSet1, aes(x = GSM180994, y = GSM180995)) +
geom_point(color = "firebrick") +
labs(x = "Sample NO.1", y = expression(paste("Sample NO.2"^"(Hey, why should we use metric units?!)")))
# 增加轴和轴标题之间的空间
ggplot(exprSet1, aes(x = GSM180994, y = GSM180995)) +
geom_point(color = "firebrick") +
labs(x = "Sample No.1", y = "Sample No.2") +
theme(axis.title.x = element_text(vjust = 0, size = 15),
axis.title.y = element_text(vjust = 2, size = 15))
ggplot(exprSet1, aes(x = GSM180994, y = GSM180995)) +
geom_point(color = "firebrick") +
labs(x = "Sample No.1", y = "Sample No.2") +
theme(axis.title.x = element_text(margin = margin(t = 10), size = 15),
axis.title.y = element_text(margin = margin(r = 10), size = 15))
# 改变轴标题的美学
ggplot(exprSet1, aes(x = GSM180994, y = GSM180995)) +
geom_point(color = "firebrick") +
labs(x = "Sample No.1", y = "Sample No.2") +
theme(axis.title = element_text(size = 15, color = "firebrick",
face = "italic"))
# face参数可用于使字体加粗或倾斜
ggplot(exprSet1, aes(x = GSM180994, y = GSM180995)) +
geom_point(color = "firebrick") +
labs(x = "Sample No.1", y = "Sample No.2") +
theme(axis.title.x = element_text(color = "sienna", size = 15),
axis.title.y = element_text(color = "orangered", size = 15))
# 调整坐标轴文本美学
ggplot(exprSet1, aes(x = GSM180994, y = GSM180995)) +
geom_point(color = "firebrick") +
labs(x = "Sample No.1", y = "Sample No.2") +
theme(axis.text = element_text(color = "dodgerblue", size = 12),
axis.text.x = element_text(face = "italic"))
# 旋转坐标轴文本
ggplot(exprSet1, aes(x = GSM180994, y = GSM180995)) +
geom_point(color = "firebrick") +
labs(x = "Sample No.1", y = "Sample No.2") +
theme(axis.text.x = element_text(angle = 30, vjust = 0, hjust = 1, size = 12))
# 删除坐标轴标题
ggplot(exprSet1, aes(x = GSM180994, y = GSM180995)) +
geom_point(color = "firebrick") +
labs(x = "Sample No.1", y = "Sample No.2") +
labs(x = NULL, y = "")
# 限制坐标轴范围
ggplot(exprSet1, aes(x = GSM180994, y = GSM180995)) +
geom_point(color = "firebrick") +
labs(x = "Sample No.1", y = "Sample No.2") +
ylim(c(0, 15))
# 可以使用scale_y_continuous(limits = c(0,15))或coord_cartesian(ylim = c(0,15))
# 使图表从坐标原点开始
ggplot(exprSet1, aes(x = GSM180994, y = GSM180995)) +
geom_point(color = "firebrick") +
labs(x = "Sample No.1", y = "Sample No.2") +
expand_limits(x = 0, y = 0)
# 相同比例的坐标轴
ggplot(exprSet1, aes(x = GSM180994, y = GSM180995)) +
geom_point(color = "firebrick") +
labs(x = "Sample No.1", y = "Sample No.2") +
xlim(c(0, 15)) + ylim(c(0, 15)) +
coord_fixed()
# 使用函数调整标签
ggplot(exprSet1, aes(x = GSM180994, y = GSM180995)) +
geom_point(color = "firebrick") +
labs(x = "Year", y = NULL) +
scale_y_continuous(label = function(x) {return(paste(x, "Expression"))})
参考资料:
https://mp.weixin.qq.com/s/9UcZRKDe0pfh3ViJ3okC4g
https://zhuanlan.zhihu.com/p/370223674
大神的博客:https://hadley.nz/
https://blog.csdn.net/huangmingleiluo/article/details/104192128
https://blog.csdn.net/weixin_45331620/article/details/106622445