You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ggplot2绘制的图表中标识三个不同实验时间阶段

实现方案说明

提供两种可读性远高于水平括号的标注方案,你可以根据自己的需求选择:

  • 方案1:用不同透明度的背景色块区分阶段,同时在x轴下方直接标注阶段名称,适合展示完整时间序列的连续变化
  • 方案2:按阶段分面呈现,天然隔离不同阶段的观测值,适合强调阶段间的组分差异

前置说明

你提供的原代码存在一处错误:scale_y_discrete 不适用于连续型的百分比y轴,需要替换为 scale_y_continuous,否则会出现y轴显示异常。

方案1:背景色块+底部标注完整代码

library(ggplot2)
library(dplyr)

# 给数据集添加阶段标签
df <- df %>% 
  mutate(phase = case_when(
    days_incubated == "0" ~ "阶段1:第0天",
    days_incubated %in% c("4","10","17","24","66") ~ "阶段2:第4-66天",
    days_incubated %in% c("81","94","116") ~ "阶段3:第81-116天"
  ))

p1 <- df %>% 
  ggplot(aes(x=days_incubated, y=rel_soil, 
             fill = factor(group2, levels=c("CxHyOz", "CxHy","CxHyNz", "Terpenoids", "mz>137" ,"mz<137"))))+
  # 先加阶段背景色块,放在柱状图下层不遮挡数据
  annotate("rect", xmin = 0.5, xmax = 1.5, ymin = 0, ymax = 100, fill = "#f0f0f0", alpha = 0.3)+
  annotate("rect", xmin = 1.5, xmax = 6.5, ymin = 0, ymax = 100, fill = "#d9d9d9", alpha = 0.3)+
  annotate("rect", xmin = 6.5, xmax = 9.5, ymin = 0, ymax = 100, fill = "#bdbdbd", alpha = 0.3)+
  geom_bar(stat="identity", color="black", width=1)+
  # 添加阶段文字标注,放在x轴下方
  annotate("text", x = 1, y = -5, label = "阶段1", size = 3.5, fontface = "bold")+
  annotate("text", x = 4, y = -5, label = "阶段2", size = 3.5, fontface = "bold")+
  annotate("text", x = 8, y = -5, label = "阶段3", size = 3.5, fontface = "bold")+
  scale_fill_brewer(palette="Dark2") +
  labs(x = "Time (day)", y = "Relative BVOC emission contribution (%)", title = "Relative BVOC emissions", fill = "") +
  scale_x_discrete(expand = c(0,0))+
  # 修正y轴设置,预留下方空间放标注
  scale_y_continuous(expand = c(0,0), limits = c(-8,100))+
  theme_bw() +
  # 调整x轴标题边距,避免和阶段标注重叠
  theme(axis.title.x = element_text(margin = margin(t = 20)))

p1

方案2:分面呈现完整代码

library(ggplot2)
library(dplyr)

# 给数据集添加阶段标签
df <- df %>% 
  mutate(phase = case_when(
    days_incubated == "0" ~ "阶段1:第0天",
    days_incubated %in% c("4","10","17","24","66") ~ "阶段2:第4-66天",
    days_incubated %in% c("81","94","116") ~ "阶段3:第81-116天"
  ))

p2 <- df %>% 
  ggplot(aes(x=days_incubated, y=rel_soil, 
             fill = factor(group2, levels=c("CxHyOz", "CxHy","CxHyNz", "Terpenoids", "mz>137" ,"mz<137"))))+
  geom_bar(stat="identity", color="black", width=1)+
  scale_fill_brewer(palette="Dark2") +
  labs(x = "Time (day)", y = "Relative BVOC emission contribution (%)", title = "Relative BVOC emissions", fill = "") +
  scale_x_discrete(expand = c(0,0))+
  scale_y_continuous(expand = c(0,0))+
  # 按阶段分面,设置x轴自由适配对应阶段的日期
  facet_grid(~phase, scales = "free_x", space = "free_x", switch = "x")+
  theme_bw() +
  # 调整分面标签样式,放在x轴下方作为阶段标注
  theme(strip.placement = "outside",
        strip.background = element_rect(fill = "grey90", color = "black"),
        strip.text = element_text(size = 10, face = "bold"))

p2

内容的提问来源于stack exchange,提问作者Tiptop

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 05:06:01