2012-08-07 5 views
24

私は、単一の文化と2つの異なる種の混合文化の間の成長の違いを見るデータがあります。さらに、データを明確にするためのグラフを作成しました。ファセットに異なる線を追加する方法

エラーバーのあるバープロットが必要ですが、データセット全体はもちろん大きくなりますが、このグラフでは、これはバープロットの手段を備えたdata.frameです。このデータに

plant   species  means 
Mixed culture Elytrigia 0.886625 
Monoculture  Elytrigia 1.022667 
Monoculture  Festuca  0.314375 
Mixed culture Festuca  0.078125 

私はplantがy軸上にX軸及びmeansであるggplot2のグラフを作り、私は種を分割するファセットを用います。

これは私のコードです:

limits <- aes(ymax = meansS$means + eS$se, ymin=meansS$means - eS$se) 
    dodge <- position_dodge(width=0.9) 

    myplot <- ggplot(data=meansS, aes(x=plant, y=means, fill=plant)) + facet_grid(. ~ species) 
    myplot <- myplot + geom_bar(position=dodge) + geom_errorbar(limits, position=dodge, width=0.25) 
    myplot <- myplot + scale_fill_manual(values=c("#6495ED","#FF7F50")) 
    myplot <- myplot + labs(x = "Plant treatment", y = "Shoot biomass (gr)") 
    myplot <- myplot + opts(title="Plant competition") 
    myplot <- myplot + opts(legend.position = "none") 
    myplot <- myplot + opts(panel.grid.minor=theme_blank(), panel.grid.major=theme_blank()) 

これまでのところ、それは結構です。しかし、私は2つのファセットに2つの異なる水平線を追加したい。

hline.data <- data.frame(z = c(0.511,0.157), species = c("Elytrigia","Festuca")) 
    myplot <- myplot + geom_hline(aes(yintercept = z), hline.data) 

しかし、私はそれを行う場合、私はプロットは2本の水平線がプロットされている2つの余分なファセットが、存在した取得:そのために、私はこのコードを使用していました。代わりに、横の線を棒でファセットにプロットし、2つの新しいファセットを作ってはなりません。誰でもこれを解決する方法のアイデア。

私はそれは私が今作成グラフ置けば、それは明確になり思う:

enter image description here

+0

の要因でなければなりませんあなたが探しているものabline' 'ですか? – Pop

+0

いいえ与えられたy座標に水平線が必要です。しかし、ファセットごとに値は異なります。例えば、エリトリッジファセットは0.511に水平線を持ち、フェセットファセットは0.157に水平線を持ちます。 – Marinka

+3

データセットをそのままコンソールから切り取って貼り付けるか、 'dput(手段)'を使用して私たちにあなたのデータを与えてください。 –

答えて

45

を変数種は両方のデータセットで同一であることを確認してください。それそれらの上に1の要因であれば、それは他のあまりに

library(ggplot2) 
dummy1 <- expand.grid(X = factor(c("A", "B")), Y = rnorm(10)) 
dummy1$D <- rnorm(nrow(dummy1)) 
dummy2 <- data.frame(X = c("A", "B"), Z = c(1, 0)) 
ggplot(dummy1, aes(x = D, y = Y)) + geom_point() + facet_grid(~X) + 
    geom_hline(data = dummy2, aes(yintercept = Z)) 

enter image description here

dummy2$X <- factor(dummy2$X) 
ggplot(dummy1, aes(x = D, y = Y)) + geom_point() + facet_grid(~X) + 
    geom_hline(data = dummy2, aes(yintercept = Z)) 

enter image description here

+0

ああ、今私は種名の後ろにスペースがあったことを思い出しました。少なくとも私のコードには何も問題はありませんでした。 – Marinka

+0

'geom_abline'ではうまく動作しないようですか、何か混乱しましたか?私の質問を参照してくださいhttp://stackoverflow.com/questions/43696806/r-ggplot-different-geom-ablines-in-facets – Boern

関連する問題