2017-07-29 9 views
1

私はあなたがそこに、見ることができるように、この結果にGGPLOTファセットバープロット:フィルカテゴリごとに複数の「ドッジド」バーがありますか?

enter image description here

を返し、この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() 

を使用して 'DS'

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)) 

このデータセットをプロットしていますx変数「分数」(血清/血漿)およびフィルカテゴリ「人口統計」(調整/調整されていない)ごとに3つの棒が積み重ねられています。しかし、私はこれらの3つの棒グラフを1つのカテゴリごとに並べて表示することをお勧めします。これも可能ですか?

ご協力いただきありがとうございます。

答えて

2

は、変数

あなたが( aes()関数内)1以上aesteticを追加する場合、今の列が group=freq ggplotは、別途各バーをかわすます demographics渡ってかわしたが、されている
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

感謝を。しかしもう1つの質問ですが、「赤いもの」(調整されていない)が「青いもの」の前/上にプロットされるように、バーの順番をどのように変更できるか知っていますか? –

+0

'scale_fill_manual'に最初に書いたものを' scale_fill_manual'に入れてください。 'scale_fill_manual(" "、values = c(" adjust "="#377eb8 "、" not adjusted "="#e41a1c ")...' –

+0

サウンドは、 –

1

をグループ化し、個々を追加します。したがって、あなたのコードは次のようになります。

ようになり
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

これは現在のデータセットで動作しますが、同じグループの2つの行が同じfreqの値を持っていれば失敗します。 –

関連する問題