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

如何在R语言ggplot2包的图例上方添加文本?

在ggplot2图例上方添加文本的方法

问题背景

以下代码可以生成指定的ggplot2图表:

p <- ggplot(data=dat,
    aes(x=X, y=Y, ymin=lo, ymax=hi,
        shape=task, colour=task)) +
    geom_point(size=3, position=position_dodge(0.5)) +
    geom_errorbar(width=0.12, linewidth=0.5, position=position_dodge(0.5)) +
    scale_shape_manual(values=c(16,17),
        labels=c('A', 'B')) +
    scale_colour_brewer(palette='Dark2',
        labels=c('A', 'B')) +
    scale_x_discrete('X',
        labels=c(1, 2, 3, 4)) +
    scale_y_continuous('Y', limits=c(6.0, 8.0),
        breaks=c(6.0, 6.5, 7.0, 7.5, 8.0)) +
    guides(colour=guide_legend(title='Task:'),
        shape=guide_legend(title='Task:')) +
    ggtitle('Title') +
    theme(plot.title=element_text(hjust=0.5),
        axis.text=element_text(size=12),
        axis.title=element_text(size=14),
        legend.text=element_text(size=10)) +
    annotate(geom='text', x=1, y=8, label='1.0', color='black') +
    annotate(geom='text', x=2, y=8, label='0.93', color='black') +
    annotate(geom='text', x=3, y=8, label='0.95', color='black')
p

对应图表:
Fig-1

需求是:如何在图表中图例的上方位置添加文本?

解决方案

这里提供两种实用方法,直接套用就能实现:

方法1:用grid包精准定位文本

这种方法适合需要精确控制文本位置的场景:

library(ggplot2)
library(grid)

# 保留原图表代码,新增固定图例位置的设置
p <- ggplot(data=dat,
    aes(x=X, y=Y, ymin=lo, ymax=hi,
        shape=task, colour=task)) +
    geom_point(size=3, position=position_dodge(0.5)) +
    geom_errorbar(width=0.12, linewidth=0.5, position=position_dodge(0.5)) +
    scale_shape_manual(values=c(16,17),
        labels=c('A', 'B')) +
    scale_colour_brewer(palette='Dark2',
        labels=c('A', 'B')) +
    scale_x_discrete('X',
        labels=c(1, 2, 3, 4)) +
    scale_y_continuous('Y', limits=c(6.0, 8.0),
        breaks=c(6.0, 6.5, 7.0, 7.5, 8.0)) +
    guides(colour=guide_legend(title='Task:'),
        shape=guide_legend(title='Task:')) +
    ggtitle('Title') +
    theme(plot.title=element_text(hjust=0.5),
        axis.text=element_text(size=12),
        axis.title=element_text(size=14),
        legend.text=element_text(size=10),
        legend.justification = c(1, 1), # 把图例固定在右上角
        legend.position = c(1, 1)) +
    annotate(geom='text', x=1, y=8, label='1.0', color='black') +
    annotate(geom='text', x=2, y=8, label='0.93', color='black') +
    annotate(geom='text', x=3, y=8, label='0.95', color='black')

# 创建要添加的文本,y=unit(1.1, "npc")表示在图例顶部再往上10%的位置
top_text <- textGrob("你要加的文本内容", x = unit(0.5, "npc"), y = unit(1.1, "npc"), 
                     gp = gpar(fontsize = 12, color = "black"))

# 将文本添加到图表
p <- p + annotation_custom(top_text, xmin = Inf, xmax = Inf, ymin = Inf, ymax = Inf)

# 绘制最终图表
grid.draw(p)

方法2:用cowplot包快速加文本

如果不想折腾坐标,用cowplot更简单:

library(ggplot2)
library(cowplot)

# 原图表代码完全不变
p <- ggplot(data=dat,
    aes(x=X, y=Y, ymin=lo, ymax=hi,
        shape=task, colour=task)) +
    geom_point(size=3, position=position_dodge(0.5)) +
    geom_errorbar(width=0.12, linewidth=0.5, position=position_dodge(0.5)) +
    scale_shape_manual(values=c(16,17),
        labels=c('A', 'B')) +
    scale_colour_brewer(palette='Dark2',
        labels=c('A', 'B')) +
    scale_x_discrete('X',
        labels=c(1, 2, 3, 4)) +
    scale_y_continuous('Y', limits=c(6.0, 8.0),
        breaks=c(6.0, 6.5, 7.0, 7.5, 8.0)) +
    guides(colour=guide_legend(title='Task:'),
        shape=guide_legend(title='Task:')) +
    ggtitle('Title') +
    theme(plot.title=element_text(hjust=0.5),
        axis.text=element_text(size=12),
        axis.title=element_text(size=14),
        legend.text=element_text(size=10)) +
    annotate(geom='text', x=1, y=8, label='1.0', color='black') +
    annotate(geom='text', x=2, y=8, label='0.93', color='black') +
    annotate(geom='text', x=3, y=8, label='0.95', color='black')

# 在图例上方加文本,x和y是0-1的比例值(0是左下角,1是右上角),根据你的图例位置微调
ggdraw(p) + draw_plot_label("你要加的文本内容", x = 0.9, y = 0.95, size = 12)

小提示

  • 方法1里固定图例位置是为了让文本精准对齐,如果你的图例在其他位置,调整legend.position的数值即可;
  • 方法2的x、y参数可以慢慢试,比如图例在右侧中间,就把y改成0.5左右;
  • 两种方法都能调整文本的字体大小、颜色,方法1用gpar(),方法2直接加color参数就行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 23:42:32