By Gengxin, 30 June, 2025

  1. 输入数据格式

  1. 运行代码
#加载软件包
library(ggplot2)
library(ggpubr)

# 读取数据
df <- read.delim("E:/Retrogene课题/小孢子叶球注射过表达基因结果/小孢子叶球定量实验/TG12501.txt", header = TRUE)
df$Group <- factor(df$Group, levels = c("12501_CK_1", "12501_OE_1", "12501_CK_2", "12501_OE_2", "12501_CK_3", "12501_OE_3")) #设置图片中数据的前后位置

# 绘制带P值的箱型图
ggplot(df, aes(x = Group, y = Value, fill = Group)) +
  geom_boxplot(width = 0.5, outlier.shape = 21, outlier.fill = "red") +
  geom_jitter(width = 0.1, size = 2, alpha = 0.7) +
  stat_compare_means(method = "t.test", label = "p.signif", 
                     comparisons = list(c("12501_CK_1", "12501_OE_1"),c("12501_CK_2", "12501_OE_2"),c("12501_CK_3", "12501_OE_3"))) +
  labs(title = "qPCR Expression Analysis",
       x = "Group",
       y = expression(2^{-Delta*Delta*Ct})) +
  theme_classic() +
  theme(
    axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)
  )
  1. 结果文件示例

调整字体颜色

theme_classic() +
  theme(
    axis.text.x = element_text(
      angle = 45,
      vjust = 1,
      hjust = 1,
      color = "darkred",   # 字体颜色
      size = 12,           # 字体大小
      face = "bold",       # 字体加粗
      family = "Arial"     # 字体类型(系统字体)
    )
  )

调整箱型图颜色

ggplot(df, aes(x = Group, y = Value, fill = Group)) +
  geom_boxplot(width = 0.5, outlier.shape = 21, outlier.fill = "red") +
  geom_jitter(width = 0.1, size = 2, alpha = 0.7) +
  stat_compare_means(method = "t.test", label = "p.signif", 
                     comparisons = list(c("6810_CK_1", "6810_OE_1"),
                                        c("6810_CK_2", "6810_OE_2"))) +
  labs(title = "qPCR Expression Analysis",
       x = "Group",
       y = expression(2^{-Delta*Delta*Ct})) +
  theme_classic() +
  theme(
    axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)
  ) +
  scale_fill_manual(values = c(
    "6810_CK_1" = "#1f77b4",  # 蓝色
    "6810_OE_1" = "#ff7f0e",  # 橙色
    "6810_CK_2" = "#2ca02c",  # 绿色
    "6810_OE_2" = "#d62728"   # 红色
  ))

Nature风格

theme_classic(base_size = 12, base_family = "Arial") +
  theme(
    axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1, size = 11, color = "black"),
    axis.text.y = element_text(size = 11, color = "black"),
    axis.title.y = element_text(size = 12),
    plot.title = element_text(size = 14, face = "bold", hjust = 0.5),
    legend.position = "none",              # Nature 通常不显示 fill 图例
    axis.line = element_line(size = 0.4),
    axis.ticks = element_line(size = 0.3)
  )

原文链接:R语言绘制箱型图