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

如何为ggplot柱状图添加第二组X轴标签并设置Fill填充?

问题与解决方案

我需要制作带双X轴标签、分面(facet wrap)和不同填充颜色的图表,数据包含x1、x2、Fill、Sex、y字段。现有代码无法实现将x2设为第二组X轴标签,也无法用Fill作为填充参数,以下是修正方案:

现有代码(修正语法错误后)

data$x1 <- as.factor(data$x1)
data$x2 <- as.factor(data$x2)
data$Fill <- as.factor(data$Fill)

ggplot(data, aes(x=x1, y=y, fill=x2))+
  geom_col(position=position_dodge())+
  theme(legend.position = "right")+
  facet_wrap(~Sex, nrow=1, strip.position = "top")+
  theme(strip.placement = "outside")+
  scale_x_discrete(breaks = c(11, 12, 22),labels = c("11", "12", "22"))+
  ylab("y") 

注:原代码中+n为输入错误,已删除并补全语法。


核心问题修正

1. 用Fill作为填充参数

直接在aes中将fill映射改为Fill即可(注意列名大小写需与数据完全一致):

aes(x=x1, y=y, fill=Fill)

2. 添加第二X轴标签(分组标签)

ggplot不原生支持离散双X轴,可通过两种方式模拟分组标签效果:

方式一:换行标签合并双轴

如果x1与x2是一一对应关系,将X轴标签设置为x2+换行+x1的形式:

library(dplyr)

# 构建x1到x2的映射关系
x_label_map <- data %>% distinct(x1, x2)
# 生成带换行的复合标签
x_labels <- paste(x_label_map$x2, x_label_map$x1, sep = "\n")
names(x_labels) <- x_label_map$x1

ggplot(data, aes(x=x1, y=y, fill=Fill))+
  geom_col(position=position_dodge(width=0.8))+
  facet_wrap(~Sex, nrow=1, strip.position = "top")+
  scale_x_discrete(labels = x_labels)+
  ylab("y")+
  theme(
    legend.position = "right",
    strip.placement = "outside",
    axis.text.x = element_text(lineheight = 1.2) # 调整行高优化显示
  )

方式二:用ggtext添加独立分组标签

如果需要更清晰的双轴视觉层级,使用ggtext包添加顶部分组标签:

library(ggplot2)
library(ggtext)
library(dplyr)

# 获取x轴离散值的位置与对应x2值
x_positions <- seq_along(unique(data$x1))
x2_labels <- data %>% distinct(x1, x2) %>% arrange(x1) %>% pull(x2)

ggplot(data, aes(x=x1, y=y, fill=Fill))+
  geom_col(position=position_dodge(width=0.8))+
  facet_wrap(~Sex, nrow=1, strip.position = "top")+
  scale_x_discrete(labels = unique(data$x1))+
  # 添加x2分组标签
  annotate(
    "richtext",
    x = x_positions,
    y = -Inf,
    label = x2_labels,
    vjust = 1.5,
    size = 4,
    fill = NA,
    label.color = NA
  )+
  ylab("y")+
  theme(
    legend.position = "right",
    strip.placement = "outside",
    axis.text.x = element_text(margin = margin(t=10)) # 给分组标签预留空间
  )

完整可运行示例(含模拟数据)

library(ggplot2)
library(dplyr)

# 模拟匹配你的数据格式
data <- tibble(
  x1 = factor(c(11,11,12,12,22,22,11,11,12,12,22,22)),
  x2 = factor(c("Group1","Group1","Group1","Group1","Group2","Group2","Group1","Group1","Group1","Group1","Group2","Group2")),
  Fill = factor(c("TypeX","TypeY","TypeX","TypeY","TypeX","TypeY","TypeX","TypeY","TypeX","TypeY","TypeX","TypeY")),
  Sex = factor(c("Male","Male","Male","Male","Male","Male","Female","Female","Female","Female","Female","Female")),
  y = c(3,4,2,5,6,3,2,3,1,4,5,2)
)

# 构建复合标签
x_label_map <- data %>% distinct(x1, x2)
x_labels <- paste(x_label_map$x2, x_label_map$x1, sep = "\n")
names(x_labels) <- x_label_map$x1

ggplot(data, aes(x=x1, y=y, fill=Fill))+
  geom_col(position=position_dodge(width=0.8), color="black")+
  facet_wrap(~Sex, nrow=1, strip.position = "top")+
  scale_x_discrete(labels = x_labels)+
  scale_fill_brewer(palette = "Set2")+ # 可选:设置填充色板
  ylab("y")+
  theme(
    legend.position = "right",
    strip.placement = "outside",
    axis.text.x = element_text(lineheight = 1.3, size=10),
    axis.title.x = element_blank()
  )

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 10:57:51