2017-11-22 7 views
-1

グループ化された棒グラフを改善しようとしていて、各バーに標準偏差を追加しようとしていますが、苦労しているようです。さらに、「コントラスト」グループを左に、「コントラスト」グループを右に、「コントロール」を左に、「抗cd47」をそれぞれのグループ内に置いておきたいと思います。ここで標準偏差バーをggplotスタックバープロットに追加し、データの順序を再編成する方法は?

は私のプロットである:ここでは

Plot

は私のコードです:

library(plyr) 
library(tidyverse) 
library(readxl) 

t2<- read_excel("t2quant.xlsx") 
summary(t2) 

head(t2, n=12) 


ggplot(data=t2, aes(x = Time, y = T2Value)) + 
    scale_fill_manual(values=c('red', 'black'))+ 
    theme(legend.position="top") + 
    theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
     panel.background = element_blank(), axis.line = element_line(colour = "black"))+ 
    geom_bar(aes(fill = Group), 
    width = 0.7, position = position_dodge(width=0.7), stat="summary", fun.y = "mean")+ 
    scale_y_continuous(expand=c(0,0), limits = c(0,20)) 

、ここではdputから、私のデータである。

structure(list(Time = c("Pre_Contrast", "Pre_Contrast", "Pre_Contrast", 
"Pre_Contrast", "Pre_Contrast", "Pre_Contrast", "Post_Contrast", 
"Post_Contrast", "Post_Contrast", "Post_Contrast", "Post_Contrast", 
"Post_Contrast"), Group = c("Control", "Control", "Control", 
"Anti_CD47", "Anti_CD47", "Anti_CD47", "Control", "Control", 
"Control", "Anti_CD47", "Anti_CD47", "Anti_CD47"), T2Value = c(14, 
10.4, 11, 16.7, 15, 12, 6, 5.8, 6.5, 3.5, 3.7, 2.6)), .Names = c("Time", 
"Group", "T2Value"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-12L)) 

は、いくつかの方法があります私は標準偏差をmutateなどのデータセットに追加できますか?あるいは、私のExcelシートのデータを完全に水平に再配置する必要がありますか?

プレコントラスト|コントロール|データ1 |データ2 |データ3 |平均| SD |

助けていただけたら幸いです!

答えて

0
library(dplyr) 
library(tidyr) 

t2new <- t2 %>% 
    group_by(Time, Group) %>% 
    summarise(mean= mean(T2Value), sd = sd(T2Value)) 

ggplot(t2new, aes(Time, mean, fill=Group)) + 
    geom_bar(stat="identity", width = 0.7, position = position_dodge(width=0.7)) + 
    geom_errorbar(aes(ymin=mean-1.96*sd, max=mean+1.96*sd), width=.2, position=position_dodge(width=0.7)) 

enter image description here