2011-09-06 7 views
20

私はggplot2でインタラクションプロットを作ろうとしています。私のコードは以下の通りです:ggplot2のインタラクションプロット

library(ggplot2) 
p <- qplot(as.factor(dose), len, data=ToothGrowth, geom = "boxplot", color = supp) + theme_bw() 
p <- p + labs(x="Dose", y="Response") 
p <- p + stat_summary(fun.y = mean, geom = "point", color = "blue") 
p <- p + stat_summary(fun.y = mean, geom = "line", aes(group = 1)) 
p <- p + opts(axis.title.x = theme_text(size = 12, hjust = 0.54, vjust = 0)) 
p <- p + opts(axis.title.y = theme_text(size = 12, angle = 90, vjust = 0.25)) 
print(p) 

することができます私はプロット用量SUPPのレベルの組み合わせではなく、私がここにきてるだけ用量レベルの手段よりも、意味は?あなたの助けを前にありがとう。

enter image description here

答えて

30

あなたが自分自身のデータフレームに値を事前計算することができます使用してggplotではなくqplotは、より複雑なプロットのためのグラフ構築たくさん明確になることを

toothInt <- ddply(ToothGrowth,.(dose,supp),summarise, val = mean(len)) 

ggplot(ToothGrowth, aes(x = factor(dose), y = len, colour = supp)) + 
    geom_boxplot() + 
    geom_point(data = toothInt, aes(y = val)) + 
    geom_line(data = toothInt, aes(y = val, group = supp)) + 
    theme_bw() 

enter image description here

注意これらのように(IMHO)。

+0

ありがとうございました。これは私が探していたものです。再度、感謝します。 – MYaseen208

9

あなたが適切なグループ(supp)で、あなたの要約を計算することができます。

p <- qplot(as.factor(dose), len, data=ToothGrowth, geom = "boxplot", color = supp) + theme_bw() 
p <- p + labs(x="Dose", y="Response") 
p <- p + stat_summary(fun.y = mean, geom = "point", color = "blue", aes(group=supp)) 
p <- p + stat_summary(fun.y = mean, geom = "line", aes(group = supp)) 
p <- p + opts(axis.title.x = theme_text(size = 12, hjust = 0.54, vjust = 0)) 
p <- p + opts(axis.title.y = theme_text(size = 12, angle = 90, vjust = 0.25)) 
print(p) 

それともggplot構文(と一つの式に組み合わせること)への変換を

ggplot(ToothGrowth, aes(as.factor(dose), len, colour=supp)) + 
    geom_boxplot() + 
    stat_summary(aes(group=supp), fun.y = mean, geom="point", colour="blue") + 
    stat_summary(aes(group=supp), fun.y = mean, geom="line") + 
    scale_x_discrete("Dose") + 
    scale_y_continuous("Response") + 
    theme_bw() + 
    opts(axis.title.x = theme_text(size = 12, hjust = 0.54, vjust = 0), 
    axis.title.y = theme_text(size = 12, angle = 90, vjust = 0.25)) 

EDIT:

これを0.9.3で動作させるには、効果的に0123になります。

library("plyr") 
summ <- ddply(ToothGrowth, .(supp, dose), summarise, len = mean(len)) 

ggplot(ToothGrowth, aes(as.factor(dose), len, colour=supp)) + 
    geom_boxplot() + 
    geom_point(data = summ, aes(group=supp), colour="blue", 
      position = position_dodge(width=0.75)) + 
    geom_line(data = summ, aes(group=supp), 
      position = position_dodge(width=0.75)) + 
    scale_x_discrete("Dose") + 
    scale_y_continuous("Response") + 
    theme_bw() + 
    theme(axis.title.x = element_text(size = 12, hjust = 0.54, vjust = 0), 
     axis.title.y = element_text(size = 12, angle = 90, vjust = 0.25)) 

enter image description here

+0

あなたの返信にBrian Diggsを感謝します。どうやってドットポイントの色を変えるのですか? – MYaseen208

+0

ポイントについては、色のデフォルトマッピングが "青"にオーバーライドされているので、その 'stat_summary'呼び出しから' color = "blue" 'を削除するだけです。デフォルトで' supp 'に基づいてマッピングされます変数。 –

+0

ありがとうBrain Diggs。 – MYaseen208

7

あなたはより一般的なアプローチを必要とするかもしれないと思われる場合は、パッケージHandyStuff(github.com/bryanhanson/HandyStuff)で関数rxnNormを試みることができます。免責事項:私は著者です。免責事項#2:ボックスプロットオプションは正しく動作しませんが、その他のオプションはすべて問題ありません。

p <- rxnNorm(data = ToothGrowth, res = "len", fac1 = "dose", fac2 = "supp", freckles = TRUE, method = "iqr", fac2cols = c("red", "green")) 
print(p) 

rxnNorm Demo

1

もっと簡単な方法:ここで

はToothGrowthデータを使用した例です。 ddplyなし。直接ggplot2で

ggplot(ToothGrowth, aes(x = factor(dose) , y=len , group = supp, color = supp)) + 
    geom_boxplot() + 
    geom_smooth(method = lm, se=F) + 
    xlab("dose") + 
    ylab("len") 
+0

これは、線を正しく描画しますが、ボックスプロットは描画しません。 – hpesoj626