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

ggplot中fill参数添加图例标题无效,求排查解决方法

ggplot图例标题不显示的解决方法

问题情况

用ggplot绘制分组柱状图时,尝试通过labs(fill = "Fantasy Type")添加图例标题,但完全无效。试过设置fill和colour参数指定图例标题,都没显示,当前绘图效果如下:
当前绘图效果

用到的代码:

img_summary %>% 
  ggplot(aes(x = fantasy_group, y = mean_think, fill = fantasy_type)) +
  geom_col(colour = "black", width = .7, position = position_dodge(.7)) +
  geom_errorbar(aes(ymin = mean_think - 1*se_think,
                    ymax = mean_think + 1*se_think), 
                    position = position_dodge(.7), width = .25) +
  scale_x_discrete(labels = c("Dominant", "Intimacy", "Promiscuous", "Submissive")) +
  scale_y_continuous(breaks = seq(0,100, by = 10), limits = c(0,100), expand = c(0,0)) +
  scale_fill_manual(values = c('#3B3B3B', '#656465', '#A0A0A0', '#D3D3D3'),
                    labels = c("Being dominant", "Being loved", "Impersonal sex", "Being submissive")) +
  labs(x = "Fantasy Group", y = "Mean Time Thinking (0-100)", title = "Figure 1: Mean percentage of time thinking of each fantasy type by fantasy group", fill = "Fantasy Type") +
  theme_apa()

问题原因

核心问题出在theme_apa()这个主题上——多数APA风格主题(比如papaja包提供的版本)默认会隐藏图例标题,或者其内置的主题设置覆盖了labs()里的图例标题配置。

解决方法

给你三个可行的修复方案:

方案1:手动强制显示图例标题

在theme_apa()之后添加theme()设置,强制显示图例标题:

img_summary %>% 
  ggplot(aes(x = fantasy_group, y = mean_think, fill = fantasy_type)) +
  geom_col(colour = "black", width = .7, position = position_dodge(.7)) +
  geom_errorbar(aes(ymin = mean_think - 1*se_think,
                    ymax = mean_think + 1*se_think), 
                    position = position_dodge(.7), width = .25) +
  scale_x_discrete(labels = c("Dominant", "Intimacy", "Promiscuous", "Submissive")) +
  scale_y_continuous(breaks = seq(0,100, by = 10), limits = c(0,100), expand = c(0,0)) +
  scale_fill_manual(values = c('#3B3B3B', '#656465', '#A0A0A0', '#D3D3D3'),
                    labels = c("Being dominant", "Being loved", "Impersonal sex", "Being submissive")) +
  labs(x = "Fantasy Group", y = "Mean Time Thinking (0-100)", title = "Figure 1: Mean percentage of time thinking of each fantasy type by fantasy group", fill = "Fantasy Type") +
  theme_apa() +
  theme(legend.title = element_text()) # 强制显示图例标题

方案2:直接在theme_apa里启用图例标题

如果用的是papaja包的theme_apa(),可以直接传入参数开启图例标题:

theme_apa(legend.title = TRUE)

把原来的theme_apa()替换成上面这行即可。

方案3:调整代码顺序

ggplot的图层设置是后执行的会覆盖先执行的,把labs()放在theme_apa()之后,让图例标题设置覆盖主题的默认隐藏:

img_summary %>% 
  ggplot(aes(x = fantasy_group, y = mean_think, fill = fantasy_type)) +
  geom_col(colour = "black", width = .7, position = position_dodge(.7)) +
  geom_errorbar(aes(ymin = mean_think - 1*se_think,
                    ymax = mean_think + 1*se_think), 
                    position = position_dodge(.7), width = .25) +
  scale_x_discrete(labels = c("Dominant", "Intimacy", "Promiscuous", "Submissive")) +
  scale_y_continuous(breaks = seq(0,100, by = 10), limits = c(0,100), expand = c(0,0)) +
  scale_fill_manual(values = c('#3B3B3B', '#656465', '#A0A0A0', '#D3D3D3'),
                    labels = c("Being dominant", "Being loved", "Impersonal sex", "Being submissive")) +
  theme_apa() +
  labs(x = "Fantasy Group", y = "Mean Time Thinking (0-100)", title = "Figure 1: Mean percentage of time thinking of each fantasy type by fantasy group", fill = "Fantasy Type") # 把labs移到theme之后

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 17:55:58