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

如何在ggplot柱状图右侧添加含年龄段通过率的图例

问题:在ggplot年龄分布柱状图右侧添加考核通过率图例

现有学生年龄分布数据集(df):

Agen
182500
191200
204500
21800
23120
2450
25+100

已使用以下代码绘制柱状图:

ggplot(df, aes(x=Age, y=n)) + 
      geom_bar(stat="identity") + 
      ggtitle("Student Body by Age at ETH in the assessment year") + 
      scale_y_continuous(labels = function(x) format(x, scientific = FALSE))

需要在图表右侧添加展示考核通过率的图例/表格,通过率数据集如下:

Age% success
18-1980
20-2160
23-2450
25+20

解决方案

可以通过patchwork和gridExtra包将柱状图与通过率表格/小型图例图组合,以下提供两种实现方式:

方式1:添加表格样式的通过率展示

这种方式直接将通过率数据以表格形式嵌入图表右侧,直观清晰:

# 加载依赖包
library(ggplot2)
library(patchwork)
library(gridExtra)

# 定义原始数据
df <- data.frame(
  Age = c("18", "19", "20", "21", "23", "24", "25+"),
  n = c(2500, 1200, 4500, 800, 120, 50, 100)
)

# 定义通过率数据
success_df <- data.frame(
  Age = c("18-19", "20-21", "23-24", "25+"),
  `% success` = c(80, 60, 50, 20)
)

# 绘制原始柱状图(优化主题)
bar_plot <- ggplot(df, aes(x=Age, y=n)) + 
  geom_bar(stat="identity", fill="#69b3a2") + 
  ggtitle("Student Body by Age at ETH in the assessment year") + 
  scale_y_continuous(labels = function(x) format(x, scientific = FALSE)) +
  theme_minimal() +
  theme(plot.margin = margin(10, 20, 10, 10))

# 将通过率数据转为可视化表格对象
success_table <- tableGrob(
  success_df, 
  rows = NULL, # 隐藏行号
  theme = ttheme_minimal(
    core = list(fg_params = list(hjust = 0.5, vjust = 0.5)),
    colhead = list(fg_params = list(fontface = "bold")) # 表头加粗
  )
)

# 组合图表与表格,设置宽度比例
bar_plot + inset_element(success_table, left = 0.7, bottom = 0.2, right = 0.98, top = 0.8) +
  plot_layout(widths = c(3, 1))

方式2:添加图例样式的通过率展示

如果需要更贴近“图例”的视觉效果,可以用小型ggplot生成带颜色块的通过率展示:

# 加载依赖包
library(ggplot2)
library(patchwork)

# 定义数据(同方式1)
df <- data.frame(
  Age = c("18", "19", "20", "21", "23", "24", "25+"),
  n = c(2500, 1200, 4500, 800, 120, 50, 100)
)

success_df <- data.frame(
  Age = c("18-19", "20-21", "23-24", "25+"),
  `% success` = c(80, 60, 50, 20)
)

# 绘制原始柱状图
bar_plot <- ggplot(df, aes(x=Age, y=n)) + 
  geom_bar(stat="identity", fill="#69b3a2") + 
  ggtitle("Student Body by Age at ETH in the assessment year") + 
  scale_y_continuous(labels = function(x) format(x, scientific = FALSE)) +
  theme_minimal()

# 绘制通过率图例小图
legend_plot <- ggplot(success_df, aes(x = 1, y = reorder(Age, -`% success`))) +
  geom_text(aes(label = paste0(`% success`, "%")), hjust = 0, size = 4, color = "black") +
  geom_tile(aes(fill = `% success`), width = 0.2, height = 0.8) +
  scale_fill_gradient(low = "#f8766d", high = "#00ba38", name = "Pass Rate") +
  labs(title = "Assessment Pass Rate", x = "", y = "") +
  theme_void() +
  theme(
    plot.title = element_text(hjust = 0.5, face = "bold"),
    legend.position = "bottom",
    plot.margin = margin(10, 10, 10, 10)
  )

# 组合两个图表,设置宽度比例
bar_plot + legend_plot + plot_layout(widths = c(3, 1))

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 17:48:20