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

如何在R的ggplot2中为X轴添加类Excel效果的数据表格

问题描述

我正在用R语言的ggplot2包绘制柱状图与折线图的组合图表,想要实现类似Excel的效果——在X轴位置添加数据表格。以下是我的数据及已完成的绘图代码:

graf4 <- data.frame(anoobito = c(2006:2023),
                    perc_mif = c(37.5, 40.9, 65.3, 76.4, 84.4, 85.0, 86.0, 89.1, 95.1, 92.6, 93.8, 95.8, 95.0, 94.6, 92.4, 91.6, 95.8, 98.0),
                    rmm = c(52.4, 69.5, 60.7, 72.3, 74.5, 69.8, 63.7, 75.5, 71.2, 74.0, 74.6, 82.9, 60.6, 81.0, 114.0, 156.0, 77.4, 70.3))

ggplot(data = graf4, aes(x = anoobito)) +
  geom_bar(aes(y = rmm, fill = "RMM"), stat = "identity", width = 0.8) +
  geom_line(aes(y = perc_mif * (160 / 100), color = "% Inv. MIF"), size = 1) +  # Color for line
  geom_text(aes(y = rmm, label = rmm), color = "white", vjust = 8, hjust = 0.5, size = 6) +  # Labels for rmm inside bars
  geom_text(aes(y = perc_mif * (160 / 100), label = perc_mif), color = "black", vjust = -0.5, size = 6) +  # Labels for perc_mif
  scale_y_continuous(
    expand = expansion(mult = c(0, 0.1)),
    name = "RMM/100.00 NV",
    limits = c(0, 160),
    breaks = seq(0, 160, by = 20),
    sec.axis = sec_axis(~ . * (100 / 160), name = "% investigação MIF", breaks = seq(0, 100, by = 10))
  ) +
  scale_x_continuous(breaks = seq(2006, 2023, by = 1)) +
  scale_fill_manual(name = " ", values = "#375f8c") +  # Legend for bars
  scale_color_manual(name = " ", values = "#60cef2") +  # Legend for line
  labs(x = " ") +
  theme_minimal(base_size = 20) +
  theme(axis.line.y = element_blank(),
        axis.text.x = element_text(colour = "black"),
        axis.text.y = element_text(colour = "black"),
        legend.position = "bottom",
        panel.grid = element_blank())

预期效果:在图表X轴下方添加包含年份、perc_mif和rmm三列的数据表格,表格与X轴刻度对齐,整体风格与图表统一。


解决方案

可以通过patchwork包将ggplot图表与数据表格(用gt包绘制)垂直拼接,同时调整图表的X轴文本为空白,让表格替代X轴的位置,实现类似Excel的效果。

步骤1:安装并加载所需包

install.packages(c("ggplot2", "patchwork", "gt"))
library(ggplot2)
library(patchwork)
library(gt)

步骤2:修改原图表,隐藏X轴元素并调整边距

调整原绘图代码,去掉X轴的刻度文本和刻度线,同时缩小图表底部边距,让表格和图表紧密衔接:

p <- ggplot(data = graf4, aes(x = anoobito)) +
  geom_bar(aes(y = rmm, fill = "RMM"), stat = "identity", width = 0.8) +
  geom_line(aes(y = perc_mif * (160 / 100), color = "% Inv. MIF"), size = 1) +
  geom_text(aes(y = rmm, label = rmm), color = "white", vjust = 8, hjust = 0.5, size = 6) +
  geom_text(aes(y = perc_mif * (160 / 100), label = perc_mif), color = "black", vjust = -0.5, size = 6) +
  scale_y_continuous(
    expand = expansion(mult = c(0, 0.1)),
    name = "RMM/100.00 NV",
    limits = c(0, 160),
    breaks = seq(0, 160, by = 20),
    sec.axis = sec_axis(~ . * (100 / 160), name = "% investigação MIF", breaks = seq(0, 100, by = 10))
  ) +
  scale_x_continuous(breaks = seq(2006, 2023, by = 1)) +
  scale_fill_manual(name = " ", values = "#375f8c") +
  scale_color_manual(name = " ", values = "#60cef2") +
  labs(x = "") +
  theme_minimal(base_size = 20) +
  theme(
    axis.line.y = element_blank(),
    axis.text.x = element_blank(),  # 隐藏X轴刻度文本
    axis.ticks.x = element_blank(), # 隐藏X轴刻度线
    axis.text.y = element_text(colour = "black"),
    legend.position = "bottom",
    panel.grid = element_blank(),
    plot.margin = margin(t = 10, r = 10, b = 0, l = 10) # 底部边距设为0,衔接表格
  )

步骤3:用gt包绘制匹配风格的数据表格

将graf4数据转换成表格,调整样式使其与图表风格统一:

table_plot <- graf4 %>%
  gt() %>%
  cols_label(
    anoobito = "年份",
    perc_mif = "% Inv. MIF",
    rmm = "RMM/100.00 NV"
  ) %>%
  tab_options(
    table.font.size = 16,
    table.border.top.width = px(2), # 表格顶部加边框,和图表分隔
    table.border.top.color = "black",
    column_labels.font.weight = "bold",
    data_row.padding = px(3),
    table.margin = margin(t = 0, r = 10, b = 10, l = 10)
  ) %>%
  opt_table_font(font = list("sans-serif")) # 匹配图表的无衬线字体

步骤4:拼接图表与表格

使用patchwork的/运算符垂直拼接图表和表格,设置两者的高度比例:

p + table_plot +
  plot_layout(heights = c(4, 1)) # 图表高度占4份,表格占1份

效果说明

最终生成的可视化内容会在原柱线图下方显示对齐的数据表格,表格列标题清晰,整体风格与原图表一致,完全符合你想要的Excel式组合效果。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 19:14:55