簡體   English   中英

R:如何在ggplot中翻轉堆疊的並排條形圖

[英]R: how to flip stacked side-by-side barplots in ggplot

> 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)對並排欄進行不同的顏色編碼(例如,“ Population”使用不同的顏色,當然還有相應的圖例集(因此共有4個圖例)

3)擺脫“ ATM”和“人口”標簽

我們也可以從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"))

在此處輸入圖片說明

進行堆疊和閃避條形圖不在ggplot()的功能范圍內,請參見此處 ,通常使用構面是最好的選擇。

這是一種接近事實的技巧。

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()同時按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"))

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM