与coord_flip结合使用时,防止facet_wrap在每个面上重复轴标签

问题描述 投票:0回答:1

我想结合facet_wrap和cord_flip,但是当我这样做时,我会在每个方面重复轴类别。

这是一些演示数据。

demo_data = data.frame(locus = c(paste0("locus_", rep(1, 5)),
                                 paste0("locus_", rep(2, 5)),
                                 paste0("locus_", rep(3, 5)),
                                 paste0("locus_", rep(4, 5)),
                                 paste0("locus_", rep(5, 5))),
                       sample = rep(paste0("sample_", seq(1:5))),
                       value = sample(100, 25)) %>%
  mutate(ci_upper = value + 10,
         ci_lower = value - 10)

当我绘制它时,原始x轴上的值为sample_1到sample_5。当我翻转坐标时,样本 1-5 在每个方面都会重复,这使得绘图看起来文本过于混乱。我希望它们只出现在左侧的面上,就像通常在facet_wrap 中发生的那样。

ggplot(demo_data, aes(x = sample, y = value)) +
  geom_point() +
  geom_errorbar(aes(ymin = ci_lower, 
                    ymax = ci_upper)) +
  facet_wrap(~ locus, scales = "free") +
  coord_flip()

有什么想法可以实现这一目标吗?

我尝试在这里搜索,但找不到任何相关内容

r ggplot2 facet-wrap
1个回答
0
投票

此问题是您已将 x 和 y 比例标记为“自由”,因此它们在每个框中可以有不同的值。如果您想要共享轴,则需要非自由秤。尝试一下

ggplot(demo_data, aes(x = sample, y = value)) +
  geom_point() +
  geom_errorbar(aes(ymin = ci_lower, 
                    ymax = ci_upper)) +
  facet_wrap(~ locus, scales = "free_x") + 
  coord_flip()

© www.soinside.com 2019 - 2024. All rights reserved.