ggplot2添加图层,一个条形图在另一个上面导致意外的条形行为

时间:2017-04-30 15:25:15

标签: r ggplot2

我试图将一个地块放在另一个地块上(即下面的地块1和2);但对于我来说,在第三个情节中得到一些意想不到的行为(见图)。我想第二个情节出现在第一个情节的顶部。下面提供R代码和示例数据。任何帮助深表感谢。

 #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

1 个答案:

答案 0 :(得分:1)

您应该添加stat_summary,因为这是您在第二个图中使用的stat

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