2017-04-30 9 views
0

私は別のプロットを別のプロットに配置しようとしています。私のために、3番目のプロット(図参照)の予期せぬ挙動を得る。 2番目のプロットを最初のプロットの上に表示したい。 Rコードとサンプルデータは以下のとおりです。どんな助けも大歓迎です。ggplot2レイヤーを追加すると、別のバーの上に1つの棒グラフが表示され、予期しないバーの動作が発生する

#Example data for plot 1 
GroupM <- c("one", "one","one", "one", "two", "two", "two", "two", "three", "three","three", "three", "four", "four", "four", "four") 
Outcome_MeasureM <- c("Money", "Money", "Money", "Money", "Money", "Money", "Money", "Money", "Money", "Money", "Money", "Money", "Money", "Money", "Money", "Money") 
FrequencyM <- c(-0.49, -0.51, -0.49, -0.51, -1.4, -1.3,-1.4, -1.3, 1.3, 1.35, 1.3, 1.35, 0.5, 0.56, 0.5, 0.56) 
pilot1dataM <- data.frame(GroupM, Outcome_MeasureM, FrequencyM) 

#Plot 1 
plot1 <- ggplot(data=pilot1dataM, stat="identity", aes(GroupM, FrequencyM, fill = Outcome_MeasureM)) + stat_summary(fun.y = mean, geom = "bar", position = "dodge", alpha=0.4) + 
    stat_summary(fun.data = mean_cl_boot, geom = "errorbar", position=position_dodge(width=0.90), width = 0.2) + 
    labs(x = "Group \n(n)", y = "Z-Transformed Money and Experience", fill = "") + 
    scale_fill_manual(values = "#27ae60") + 
    scale_y_continuous(breaks = seq(-1, 1, by = 0.1), labels= c("-1", "-.9" ,"-.8", "-.7", "-.6", "-.5", "-.4", "-.3", "-.2", "-.1","0", ".1", ".2", ".3", ".4", ".5", ".6", ".7", ".8", ".9", "1"))+ coord_cartesian(ylim=c(-1.4,1.4)) + 
    theme(legend.text=element_text(size=10, face="plain"), legend.title=element_text(size=2), legend.key.size=unit(4, "mm"), legend.position="bottom") 
plot1 

#Example data for plot 2 
GroupE <- c("one", "one", "one", "one", "two", "two", "two", "two", "three", "three", "three", "three", "four", "four", "four", "four") 
Outcome_MeasureE <- c("Exp1", "Exp1", "Exp2", "Exp2", "Exp1", "Exp1", "Exp2", "Exp2","Exp1", "Exp1", "Exp2", "Exp2", "Exp1", "Exp1", "Exp2", "Exp2") 
FrequencyE <- c(0.1, 0.12, 0.02, 0.05, -0.4, -0.35, -0.7, -0.67, 0.02, 0.04, 0.35, 0.37, 0.7, 0.72, 0.8, 0.85) 
pilot1dataE <- data.frame(GroupE, Outcome_MeasureE, FrequencyE) 


# Plot 2 
plot2 <- ggplot(pilot1dataE, aes(GroupE, FrequencyE, fill =  Outcome_MeasureE)) + stat_summary(fun.y = mean, geom = "bar", position = "dodge") + 
    stat_summary(fun.data = mean_cl_boot, geom = "errorbar", position=position_dodge(width=0.90), width = 0.2) + 
    labs(x = "Group \n(n)", y = "Z-Transformed Money and Experience", fill = "") + 
    scale_fill_manual(values = c("#27ae60","#3498db","#666699")) + 
    scale_y_continuous(breaks = seq(-1, 1, by = 0.1), labels= c("-1", "-.9" ,"-.8", "-.7", "-.6", "-.5", "-.4", "-.3", "-.2", "-.1","0", ".1", ".2", ".3", ".4", ".5", ".6", ".7", ".8", ".9", "1"))+  coord_cartesian(ylim=c(-1.4,1.4)) + 
    theme(legend.text=element_text(size=10, face="plain"), legend.title=element_text(size=2), legend.key.size=unit(4, "mm"), legend.position="bottom") 
plot2 

#Trying to add the two plots on top of each other 
TwoPlots <- plot1 + geom_bar(data=pilot1dataE, stat="identity") 
TwoPlots 

enter image description here

+0

何期待される行動? –

+0

こんにちはMike Hさん、Iveを明確にするために:最初のプロットの上に2番目のプロットを表示したいと思います。 – Gorp

答えて

1

これはstatあなたが2番目のプロットに使用されましたので、あなたはstat_summaryを追加しておく必要があります。ここでは

woPlots <- plot1 + 
    stat_summary(data=pilot1dataE, aes(GroupE, FrequencyE, fill = Outcome_MeasureE),fun.y = mean, geom = "bar", position = "dodge") + 
    stat_summary(data=pilot1dataE, aes(GroupE, FrequencyE, fill = Outcome_MeasureE),fun.data = mean_cl_boot, geom = "errorbar", position=position_dodge(width=0.90), width = 0.2) + 
    scale_fill_manual(values = c("#27ae60","#3498db","#666699")) 

TwoPlot 

はプロットである:

enter image description here

関連する問題