在 geom_tile() 中向 x 轴添加组标签

发布于 2025-01-13 20:51:52 字数 1195 浏览 4 评论 0原文

我希望向 geom_tile() x 轴上的变量添加一个总体分组标签。

这与我正在寻找的结果类似:

最好的办法(不使用 geom_text() 并在 x 轴变量上方手动添加组 1、组 2 和组 3)就是这样,它只是创建一个新的包含两部分信息的变量:

set.seed(1)
df <- expand.grid(group = c("Group 1", "Group 2", "Group 3"), 
                  xVar = c("A", "B"), 
                  yVar = c("x","y","z")) %>% 
  mutate(fillValue = factor(sample(c(0,1), 18, TRUE))) %>%
  mutate(group_and_xVar = paste(group, xVar, sep = "\n"))


ggplot(df, aes(x = group_and_xVar, y = yVar, fill = fillValue))+
  geom_tile(colour = "black") +
  scale_x_discrete(position = "top", expand = c(0,0))+
  scale_y_discrete(expand = c(0,0))+
  scale_fill_manual(values=c("#FF4933", "#72AF4A"))+
  labs(x = "", y = "", fill = "")+
  theme_bw()+
  theme(axis.text.x = element_text(angle = 0, vjust = .05),
        legend.position = "none")

在此处输入图像描述

这不是我想要的。我希望每个组只在绘图上出现一次,并且代码要进行软编码(适用于将来可能添加的新数据或组)。我也尝试过使用facet_grid/facet_wrap,但我没有运气。

另外调整单元格的高度以使其看起来更像第一张图像将是一个额外的好处:)

I'm looking to add an overarching grouping label to variables on the x-axis of geom_tile().

This is similar to the result I'm looking for:
enter image description here

However, the best I can come up with (WITHOUT using geom_text() and manually adding group 1, group 2 and group 3 above the x-axis variables) is this, which just creates a new variable containing both pieces on information:

set.seed(1)
df <- expand.grid(group = c("Group 1", "Group 2", "Group 3"), 
                  xVar = c("A", "B"), 
                  yVar = c("x","y","z")) %>% 
  mutate(fillValue = factor(sample(c(0,1), 18, TRUE))) %>%
  mutate(group_and_xVar = paste(group, xVar, sep = "\n"))


ggplot(df, aes(x = group_and_xVar, y = yVar, fill = fillValue))+
  geom_tile(colour = "black") +
  scale_x_discrete(position = "top", expand = c(0,0))+
  scale_y_discrete(expand = c(0,0))+
  scale_fill_manual(values=c("#FF4933", "#72AF4A"))+
  labs(x = "", y = "", fill = "")+
  theme_bw()+
  theme(axis.text.x = element_text(angle = 0, vjust = .05),
        legend.position = "none")

enter image description here

This is not what I want. I'd like to have each group only appear once on the plot and for the code to be soft coded (adaptable to new data or groups that might be added in the future). I've also attempted using facet_grid/facet_wrap but I haven't had luck with those.

Also adjusting the height of the cells to look more like the first image would be a bonus :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

◇流星雨 2025-01-20 20:51:52

受此帖子启发的解决方案:

ggplot(df, aes(x = xVar, y = yVar, fill = fillValue)) +
    geom_tile(colour = "black") +
    scale_x_discrete(position = "top", expand = c(0,0)) +
    scale_y_discrete(expand = c(0,0)) +
    labs(x = "", y = "", fill = "") +
    facet_grid(~group) +
    coord_fixed(ratio=0.5) +
    theme(legend.position = "none",
          panel.spacing = unit(0, "lines"), 
          strip.background = element_blank(),
          strip.placement = "outside")

输入图像描述这里

A solution inspired by this post:

ggplot(df, aes(x = xVar, y = yVar, fill = fillValue)) +
    geom_tile(colour = "black") +
    scale_x_discrete(position = "top", expand = c(0,0)) +
    scale_y_discrete(expand = c(0,0)) +
    labs(x = "", y = "", fill = "") +
    facet_grid(~group) +
    coord_fixed(ratio=0.5) +
    theme(legend.position = "none",
          panel.spacing = unit(0, "lines"), 
          strip.background = element_blank(),
          strip.placement = "outside")

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文