2017-07-29 164 views
1

我策劃這個數據集 'DS'GGPLOT多面條形圖:每個填充類別有多個「閃避」條形圖?

ds <- structure(list(FN = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 
          3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 
          1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 
          2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("FN=1", "FN=2", "FN=3", 
          "FN=4"), class = "factor"), fraction = structure(c(1L, 1L, 1L, 
          1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
          1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
          2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("serum", 
          "plasma"), class = "factor"), demographics = structure(c(1L, 
          1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
          2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
          1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("not adjusted", 
          "adjusted"), class = "factor"), freq = c(132, 47, 14, 30, 29, 
        19, 25, 14, 9, 5, 4, 4, 24, 21, 15, 6, 6, 5, 4, 4, 2, 2, 2, 2, 
        35, 28, 25, 68, 24, 11, 33, 15, 10, 12, 11, 8, 24, 16, 15, 13, 
        10, 6, 7, 6, 5, 4, 3, 3)), .Names = c("FN", "fraction", "demographics", 
      "freq"), class = "data.frame", row.names = c(NA, -48L)) 

使用此ggplot碼

ggplot(ds, aes(x=fraction, y=freq, fill=factor(demographics, c("adjusted", "not adjusted")))) + 
    geom_bar(position=position_dodge(width=0.9), stat="identity", color="black") + 
    facet_grid(FN~., switch="y") + 
    scale_fill_manual("", values=c("not adjusted"="#e41a1c", "adjusted"="#377eb8"), guide=guide_legend(reverse = TRUE)) + 
    theme_bw() + 
    theme(legend.title=element_blank(), legend.position="bottom") + 
    theme(axis.title.x=element_text(face="bold")) + 
    theme(axis.title.y=element_blank()) + 
    theme(axis.ticks=element_blank()) + 
    theme(panel.background=element_blank()) + 
    theme(panel.grid=element_blank()) + 
    theme(strip.text.y = element_text(angle=180)) + coord_flip() 

返回這個結果

enter image description here

正如你所看到的,每個x變量「分數」(血清/血漿)和填充類別「人口統計」(未調整/調整)是三個條形圖。不過,我希望這些三個酒吧每個填充類別人口統計圖並排繪製。這甚至有可能嗎?

你的幫助是極大的讚賞!

回答

2

添加一個單獨的分組變量

library("tidyverse") 
ds %>% mutate(row = row_number()) %>% 
ggplot(aes(x=fraction, y=freq, fill=factor(demographics, c("adjusted", "not adjusted")), group = row)) + 
    geom_bar(position=position_dodge(width=0.9), stat="identity", color="black") + 
    facet_grid(FN~., switch="y") + 
    scale_fill_manual("", values=c("not adjusted"="#e41a1c", "adjusted"="#377eb8"), guide=guide_legend(reverse = TRUE)) + 
    theme_bw() + 
    theme(legend.title=element_blank(), legend.position="bottom") + 
    theme(axis.title.x=element_text(face="bold")) + 
    theme(axis.title.y=element_blank()) + 
    theme(axis.ticks=element_blank()) + 
    theme(panel.background=element_blank()) + 
    theme(panel.grid=element_blank()) + 
    theme(strip.text.y = element_text(angle=180)) + 
    coord_flip() 

enter image description here

+0

感謝您的快速和準確的答案。還有一個問題,你知道我怎麼能改變酒吧的順序,以便'紅色'(未調整)在'藍色'之前或之上繪製? –

+0

把你想要的第一個放在'scale_fill_manual'中,所以'scale_fill_manual(「」,values = c(「adjusted」=「#377eb8」,「not adjusted」=「#e41a1c」)...' –

+0

聽起來似乎是合理的,然而,這對我不起作用,也許是由於翻轉了這個陰謀? –

1

眼下列跨demographics躲開了,但如果你添加更多的aestetic(內aes()功能)group=freq ggplot將分別躲閃每個酒吧。因此,你的代碼應該閱讀:

ggplot(ds, aes(x=fraction, y=freq, group=freq, fill=factor(demographics, c("adjusted", "not adjusted")))) + 
    geom_bar(position=position_dodge(width=0.9), stat="identity", color="black") + 
    facet_grid(FN~., switch="y") + 
    scale_fill_manual("", values=c("not adjusted"="#e41a1c", "adjusted"="#377eb8"), guide=guide_legend(reverse = TRUE)) + 
    theme_bw() + 
    theme(legend.title=element_blank(), legend.position="bottom") + 
    theme(axis.title.x=element_text(face="bold")) + 
    theme(axis.title.y=element_blank()) + 
    theme(axis.ticks=element_blank()) + 
    theme(panel.background=element_blank()) + 
    theme(panel.grid=element_blank()) + 
    theme(strip.text.y = element_text(angle=180)) + coord_flip() 

這將是這樣的: chart

+1

這適用於當前數據集,但如果來自同一組的兩行具有相同的freq值 –