[R语言] ggplot2 火山图

作者: 半为花间酒 | 来源:发表于2020-03-30 22:11 被阅读0次

代码主要供个人存档用,
格式整理的比较清楚可以直接运行,
原始数据恕不提供,如有疑问欢迎交流。

library(ggplot2)
library(extrafont)
# 注册字体
loadfonts(device="win")

# 读取数据文件
data <- readr::read_delim("data.txt", delim = "\t")

# 数据预处理
filter_data <- data %>%
  # 去重
  na.omit() %>%
  # 去无限值
  filter(!is.infinite(log2FC)) %>%
  # 标注因子
  mutate(Gene_type = case_when(
    log2FC >= 1 & q_value < 0.05 ~ "Up",
    log2FC <= -1 & q_value < 0.05 ~ "Down",
    TRUE ~ "Stream")
  )

# 筛选要标注的感兴趣基因
for_label <- filter_data %>%
  filter((log2FC < -5.5 & -log10(q_value) > 2) |
    (log2FC > 2.7 & -log10(q_value) > 3.5) |
    (log2FC > 2 & -log10(q_value) > 2.1 & -log10(q_value) < 2.4) |
    (log2FC > 3 & -log10(q_value) > 1 & -log10(q_value) < 1.5))

# 画图
volcano <- ggplot(
    data = filter_data, 
    aes(x = log2FC, y = -log10(q_value))
    ) + 
  geom_point(
    shape = 21,
    # 选择标色的列别
    aes(fill = Gene_type),
    color = "white",
    na.rm = T,
    position = "jitter"
    ) +
  scale_fill_manual(
    # 指定标色
    values =
      c("steelblue", "grey60", "orangered")
    ) +
  labs(title = "Volcanoplot"
    ) +
  # 表达式改变x轴标签
  xlab(expression(log[2]("Fold Change"))
    ) +
  # 表达式改变y轴标签
  ylab(expression(-log[10]("q.value"))
    ) +
  # 增加y辅助线
  geom_hline(
    yintercept = -log10(0.05),
    linetype = "dashed",
    size = 1,
    color = "grey50"
    ) +
  # 增加x辅助线
  geom_vline(
    xintercept = c(-1, 1),
    linetype = "dashed",
    size = 1,
    color = "grey50"
  ) +
  # 限制x轴范围
  scale_x_continuous(
  # 使数据紧贴坐标轴
    expand = c(0, 0),
    limits = c(-11, 11)
  ) +
  # 限制y轴范围
  scale_y_continuous(
    expand = c(0, 0),
    limits = c(0, 4)
  ) +
  # 去除背景
  theme_bw() + 
  theme(
    # 去除网格线
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(), 
    # x,y坐标轴标签名和文本名的设置,包括size color face
    axis.title.x = element_text(size = 18),
    axis.title.y = element_text(size = 18),
    axis.text.x = element_text(size = 15),
    axis.text.y = element_text(size = 15),
    # 主标题的文本设置
    plot.title = element_text(
      hjust = 0.5,
      size = 20,
      face = "bold"
    ),
    # 对图例字号调整
    legend.text = element_text(size = 15),
    # 去除图例标题
    legend.title = element_blank(), 
    # 修改图例位置
    legend.position = c(0.1, 0.45), 
    # 去除图例背景,主图背景已经去除故也可不加
    legend.background = element_blank(),
    # 去除图例圆圈背景
    legend.key = element_rect(fill = "NA"),
    # 设置全部文本字体,也可以单独设置
    text = element_text(family="Arial")
  ) +
  # 修改图例圈圈大小,参数需要和映射的差异因子对应
  guides(fill = guide_legend(
    override.aes =
      list(size = 3)
    )
  ) +
  # 主图加上基因标记
  geom_point(
    data = for_label, shape = 1, size = 2
    ) +
  ggrepel::geom_label_repel(
    # 根据for_label的结果加上名字
    aes(label = c(
      "Gene_219", "Gene_615", "Gene_627",
      "Gene_5196", "Gene_6666", "Gene_8399"
    )),
    data = for_label,
    color = "grey10",
    size = 4.5
  ); volcano
image.png

相关文章

网友评论

    本文标题:[R语言] ggplot2 火山图

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