【问题标题】:R: how to flip stacked side-by-side barplots in ggplotR:如何在ggplot中翻转堆叠的并排条形图
【发布时间】:2018-04-04 03:18:13
【问题描述】:
> dput(gene_cancer2_f)
structure(list(Gender = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Female", "Male"
), class = "factor"), Gene = c("ATM", "ATM", "ATM", "ATM", "Population", 
"Population", "Population", "Population", "ATM", "ATM", "ATM", 
"ATM", "Population", "Population", "Population", "Population"
), Cancer = structure(c(2L, 3L, 5L, 12L, 2L, 3L, 5L, 12L, 2L, 
3L, 5L, 12L, 2L, 3L, 5L, 12L), .Label = c("Brain", "Breast", 
"Colorectal", "Endometrial", "Gastric", "Hepatobiliary", "Kidney", 
"Leukemia", "Melanoma", "Osteosarcoma", "Ovarian", "Pancreatic", 
"Prostate", "Soft Tissue Sarcoma", "Thyroid", "Urinary Bladder"
), class = "factor"), Type = c("RiskToAge70", "RiskToAge70", 
"RiskToAge70", "RiskToAge70", "RiskToAge70", "RiskToAge70", "RiskToAge70", 
"RiskToAge70", "RiskToAge85", "RiskToAge85", "RiskToAge85", "RiskToAge85", 
"RiskToAge85", "RiskToAge85", "RiskToAge85", "RiskToAge85"), 
    Risk = c(21.59862, 3.27479, 1.10073, 1.70754, 8.85253, 1.66318, 
    0.23228, 0.44844, 39.61044, 7.07614, 2.50735, 4.46698, 13.66465, 
    3.59502, 0.52923, 1.17365)), row.names = c(NA, -16L), .Names = c("Gender", 
"Gene", "Cancer", "Type", "Risk"), class = "data.frame")


ggplot(gene_cancer2_f, aes(x = Gene, y = Risk, fill = Type)) +
            geom_bar(stat = 'identity', position = 'stack') + 
            facet_grid(~ Cancer) +
            scale_y_continuous(limits = c(0, 100)) +
            labs(x = "Cancer", y = "Risk %") + 
            guides(fill = guide_legend(title = NULL)) +
            theme_minimal() + 
            theme(plot.title = element_text(size = 12, hjust = 0.5),
                  legend.position = "bottom") +
            scale_fill_manual(values = c("dodgerblue4", "sandybrown"),
                              breaks=c("RiskToAge70", "RiskToAge85"),
                       labels=c(paste("Risk to Age 70", " "), "Risk to Age 85"))

这是我现在的图表:

但这是我希望图表的样子:

我愿意

1) 翻转我的图表,使条形图是水平的(我试过 coord_flip,但没用)

2) 对并排的条进行不同的颜色编码(例如,“人口”的不同颜色,当然还有相应的一组图例(总共 4 个图例)

3) 摆脱“ATM”和“人口”标签

【问题讨论】:

  • @Nate 是的,我试过了。它没有工作
  • 抱歉没有看完你的评论,所以我删除了,现在换个角度工作
  • 试试+ facet_wrap(~ Cancer, ncol = 1) + coord_flip() 而不是facet_grid(~ Cancer)

标签: r ggplot2


【解决方案1】:

我们也可以使用来自ggstance 包的geom_barh

library(ggstance)

ggplot(gene_cancer2_f, aes(x = Risk, y = forcats::fct_reorder(Gene, Risk),
                           group = Gene,
                           fill = Type)) +
  facet_grid(Cancer ~ ., switch = 'y') +
  geom_barh(aes(fill = interaction(Gene, Type)), 
            stat = 'identity', position = 'stackv') +
  scale_x_continuous(limits = c(0, 100)) +
  labs(y = "Cancer", x = "Risk %") + 
  theme_minimal() + 
  theme(
    strip.text.y = element_text(angle = 180),
    axis.text.y=element_blank(),
    axis.ticks.y=element_blank(),
    plot.title = element_text(size = 12, hjust = 0.5),
    legend.position = "bottom") +
  scale_fill_manual("", 
                    values = c("dodgerblue4", "darkgreen",
                               "cornflowerblue", "darkseagreen"),
                    labels = c("ATM Risk to age 70", "ATM Risk to age 85", 
                               "Population Risk to age 70", "Population Risk to age 85"))

【讨论】:

  • 我认为包装中有一个选项position_dodgev(height = ...),但我自己从未尝试过
  • 谢谢。还有一个问题......我已经用另一个 data.frame 更新了我的原始帖子(在###EDIT 之后)。我尝试在您的答案中运行代码,但不知何故绿色和蓝色被交换了?你知道为什么吗?
  • 因为现在ggplot 将在GENE 中的PTEN 之前绘制Population。在您之前的数据中,首先绘制了ATM。您可以通过设置factor/level来控制订单。在此处了解更多信息r4ds.had.co.nz/factors.html
  • 知道了。非常感谢!
  • 没问题。干杯!
【解决方案2】:

制作堆栈和闪避条形图超出了 ggplot() 的功能,请参阅 here,通常使用构面是最佳选择。

这是一个接近的技巧。

ggplot(gene_cancer2_f, aes(x = paste(Cancer, Gene), y = Risk, group = Gene, fill = Gene)) +
    geom_col(position = "stack", aes(alpha = Type)) + 
    scale_y_continuous(limits = c(0, 100)) +
    scale_x_discrete(labels = c("Breast", "", "Colo","", "Gas","", "Pan", "")) +
    scale_alpha_manual(values = c(1, .7)) +
    labs(x = "Cancer", y = "Risk %") + 
    coord_flip() +
    theme(axis.ticks.y = element_blank(),
          axis.text.y = element_text(vjust = -2),
          legend.position = "bottom",
          legend.box = "horizontal")

【讨论】:

  • 鉴于愿望表达是跨年龄和类型,我建议您考虑在因素上使用interaction()
【解决方案3】:

您可以使用interaction()GeneType 着色。此代码接近您第二个示例中的输出,但在此过程中使用了大量操作,因此您应该仔细检查可视化是否符合您的预期。

ggplot(gene_cancer2_f, aes(Gene, Risk)) + 
  geom_col(aes(fill = interaction(Gene, Type, lex.order = TRUE)), 
           position = position_stack(reverse = TRUE)) + 
  facet_grid(Cancer~., switch = "y") + 
  coord_flip() + 
  theme(axis.text.y = element_blank(), 
        axis.ticks.y = element_blank()) + 
  labs(x = "Cancer", 
       y = "Risk of Cancer %") + 
  scale_fill_manual(name = "Risk", 
                    values = c("dodgerblue4", "dodgerblue", 
                               "darkseagreen4", "darkseagreen1"), 
                    labels = c("To age 70 carrier", "To age 85 carrier", 
                               "To age 70 non-carrier", "To age 85 non-carrier"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 2021-12-15
    • 2015-06-25
    • 2021-12-08
    • 1970-01-01
    • 2021-11-16
    相关资源
    最近更新 更多