2017-05-15 8 views
3

データを変更せずに積み重ねられたバープロットを作成したい。つまり、私はすでにプロットするパーセンテージを計算しています。 ggplot2のマニュアルによると、 "geom_colはstat_identityを使用しています。データはそのままです"。しかし、プロットのパーセンテージがサンプルデータのパーセンテージと異なるため、動作していないように見えます。データが変更されていない重ねられたオーバーレイバー

hereからサンプルデータをダウンロードします。

次のようにコードがある:(サンプルデータとの両方の画像からパーセントを比較)私はオプション「stat_identity」を使用した場合、データが変更されないまま、他の側では

ggplot(data=df, aes(x = Pathway, y = value, fill = variable)) + 
     scale_fill_manual(values=c("#005588", "#E69F00")) +                
     #stat_identity(geom="bar", width=0.5) +                              
     geom_col(width=0.5) + 
     #geom_bar(stat="identity", width=0.5) + 
     facet_grid(. ~ Timepoint) + 
     coord_flip() + 
     theme_bw() 

geom_col changes data and rows order

を、しかし、棒グラフはもう積み重ねられません。

stat_identity do not touch the data but looses stacked bars.

「geom_col」オプションが機能していないか、私が何か間違ったことをやっていますか?別のプロット方法を使用すべきですか?どんな助けもありがとうございます。

dput:

structure(list(Pathway = c("Antigen Presentation Pathway", "Graft-versus- Host Disease Signaling", 
"T Helper Cell Differentiation", "Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells", 
"Communication between Innate and Adaptive Immune Cells", "Antigen Presentation Pathway", 
"Graft-versus-Host Disease Signaling", "T Helper Cell Differentiation", 
"Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells", 
"Communication between Innate and Adaptive Immune Cells", "Antigen Presentation Pathway", 
"Graft-versus-Host Disease Signaling", "T Helper Cell Differentiation", 
"Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells", 
"Communication between Innate and Adaptive Immune Cells", "Antigen Presentation Pathway", 
"Graft-versus-Host Disease Signaling", "T Helper Cell Differentiation", 
"Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells", 
"Communication between Innate and Adaptive Immune Cells", "Antigen Presentation Pathway", 
"Graft-versus-Host Disease Signaling", "T Helper Cell Differentiation", 
"Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells", 
"Communication between Innate and Adaptive Immune Cells", "Antigen Presentation Pathway", 
"Graft-versus-Host Disease Signaling", "T Helper Cell Differentiation", 
"Cytotoxic T Lymphocyte-mediated Apoptosis of Target Cells", 
"Communication between Innate and Adaptive Immune Cells"), Timepoint = c("15DPI", 
"15DPI", "15DPI", "15DPI", "15DPI", "30DPI", "30DPI", "30DPI", 
"30DPI", "30DPI", "45DPI", "45DPI", "45DPI", "45DPI", "45DPI", 
"15DPI", "15DPI", "15DPI", "15DPI", "15DPI", "30DPI", "30DPI", 
"30DPI", "30DPI", "30DPI", "45DPI", "45DPI", "45DPI", "45DPI", 
"45DPI"), variable = c("Targets", "Targets", "Targets", "Targets", 
"Targets", "Targets", "Targets", "Targets", "Targets", "Targets", 
"Targets", "Targets", "Targets", "Targets", "Targets", "DEGs", 
"DEGs", "DEGs", "DEGs", "DEGs", "DEGs", "DEGs", "DEGs", "DEGs", 
"DEGs", "DEGs", "DEGs", "DEGs", "DEGs", "DEGs"), value = c(2.63157894736842, 
4.16666666666667, 1.36986301369863, 3.125, 1.12359550561798, 
7.89473684210526, 18.75, 8.21917808219178, 18.75, 7.86516853932584, 
15.7894736842105, 16.6666666666667, 10.958904109589, 9.375, 8.98876404494382, 
44.7368421052632, 35.4166666666667, 43.8356164383562, 37.5, 31.4606741573034, 
47.3684210526316, 43.75, 42.4657534246575, 37.5, 33.7078651685393, 
52.6315789473684, 39.5833333333333, 39.7260273972603, 31.25, 31.4606741573034)), .Names = c("Pathway", "Timepoint", "variable", 
"value"), class = "data.frame", row.names = c(NA, -30L)) 
+0

あなたの予想される出力は何ですか?あなたが投稿したコードが私のために働いているようです –

+1

バーを積み重ねることは、「そのままのままにする」とは異なります。あなたはデフォルトの 'position = 'stack''を使い、あなたが使用しているカスタム' position 'identity'を使いません。 – Gregor

+0

@Mike H 2番目のプロットのように、サンプルデータの元のパーセント値が必要ですが、最初のプロットのように積み重ねられます。 – fred

答えて

4

は、上記のコメントで、あなたとグレゴールによって議論を考えると、それはあなたがお互いに積層されたプロットを望んでいないように聞こえるが、むしろ重ね。私はこれがあなたのために働くべきだと考え:

ggplot(data=df, aes(x = Pathway, y = value, fill = variable)) + 
    scale_fill_manual(values=c("#005588", "#E69F00")) +                
    geom_col(width = 0.5, alpha = 0.5, position = "identity") + 
    facet_grid(. ~ Timepoint) + 
    coord_flip() + 
    theme_bw() 

enter image description here

私はバーが累積しないことを確認するposition = "identity"を使用しています。 alpha = 0.5でバーを透明にする必要がありました。


別のオプションあなたは彼らが代わりに積層並んでプロットしたい場合position = "dodge"を使用することです:

ggplot(data=df, aes(x = Pathway, y = value, fill = variable)) + 
    scale_fill_manual(values=c("#005588", "#E69F00")) +                
    geom_col(width=0.5, position = "dodge") + 
    facet_grid(. ~ Timepoint) + 
    coord_flip() + 
    theme_bw() 

enter image description here

関連する問題