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

如何在ggplot2中反转森林图中数据点的排列顺序

解决ggplot2森林图中数据点排列顺序问题

要实现每组内Category 1(灰色)的点显示在Category 2(黑色)上方,同时保持图例顺序不变,可以通过以下步骤调整:

  1. 反转Category因子的水平顺序:让Category 2成为第一个水平,这样ggplot会先绘制Category 2的点(位于下方),再绘制Category 1的点(位于上方)。
  2. 使用命名向量定义颜色和形状:确保每个Category对应正确的样式,不受因子水平顺序影响。
  3. 在刻度设置中指定breaks:强制图例保持原始的Category 1在前的顺序。

以下是修改后的完整代码:

# Load necessary packages
library(ggplot2)
library(gridExtra)

# Create data - 反转Category的因子水平
data <- data.frame(
  Group = factor(c('Group A', 'Group A', 'Group B', 'Group B'),
                 levels = c('Group A', 'Group B')),
  Category = factor(c('Category 1', 'Category 2', 'Category 1', 'Category 2'),
                    levels = c('Category 2', 'Category 1')), # 反转水平顺序
  OR = c(1.2, 1.5, 1.1, 1.3),
  CI_Lower = c(0.9, 1.2, 0.8, 1.0),
  CI_Upper = c(1.6, 1.8, 1.4, 1.6)
)

# 使用命名向量定义颜色和形状,确保映射正确
colors <- c('Category 1' = '#A9A9A9', 'Category 2' = 'black')
shapes <- c('Category 1' = 24, 'Category 2' = 19)

# Set window font
windowsFonts(A = windowsFont("Times New Roman"))

# Create plot
forest_plot <- ggplot(data, aes(x = OR, y = Group, color = Category, shape = Category)) +
  geom_point(size = 3, position = position_dodge(width = 0.5), aes(fill = Category)) +
  geom_errorbarh(aes(xmin = CI_Lower, xmax = CI_Upper, color = Category), height = 0, size = 0.8, position = position_dodge(width = 0.5)) +
  # 指定breaks保持图例顺序,同时用命名向量确保样式正确
  scale_color_manual(values = colors, breaks = c('Category 1', 'Category 2')) +
  scale_fill_manual(values = colors, breaks = c('Category 1', 'Category 2')) +
  scale_shape_manual(values = shapes, breaks = c('Category 1', 'Category 2')) +
  geom_vline(xintercept = 1, linetype = "dashed", color = "gray") +
  labs(x = "Odds Ratio", y = "") +
  xlim(0.5, 3) +
  geom_text(aes(label = ifelse(OR == 1.5, paste0(OR, "***"), OR)), vjust = -0.5, hjust = 1.5, size = 5, position = position_dodge(width = 0.5), family = "A", show.legend = FALSE) +
  theme_minimal(base_family = "A") +
  theme(plot.background = element_rect(fill = "white", color = "white"),
        panel.background = element_rect(fill = "white", color = "black"),
        axis.text = element_text(size = 12, family = "A"),
        axis.title = element_text(size = 12, family = "A"),
        legend.title = element_text(size = 10, family = "A"),
        legend.text = element_text(size = 10, family = "A"),
        legend.position = c(0.99, 0.99),
        legend.justification = c(1, 1),
        legend.background = element_rect(fill = "white", color = "white"),
        legend.box.background = element_rect(fill = "white", color = "white"),
        panel.grid = element_blank(),
        panel.border = element_rect(color = "black", fill = NA),
        plot.margin = unit(c(0.1, 0.1, 0.1, 0.1), "cm")) +
  scale_y_discrete(labels = c("Group A\nCategory", "Group B\nCategory")) +
  labs(y = NULL) +
  theme(axis.text.y = element_text(margin = margin(r = 10)))

print(forest_plot)

关键修改说明:

  • 反转Category因子水平:通过levels = c('Category 2', 'Category 1')改变绘图顺序,让Category 1的点覆盖在Category 2上方。
  • 命名向量映射:确保colors和shapes直接绑定到对应的Category,避免因因子水平顺序变化导致样式错乱。
  • 指定breaks参数:在scale_*_manual中设置breaks = c('Category 1', 'Category 2'),强制图例保持原始顺序。

这样调整后,每组内Category 1的点会显示在Category 2上方,同时图例依然保持Category 1在前的顺序。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 10:55:08