csv - how to calculate S.D. per group in a dataframe in R and plotting it groupwise -


i have dataframe as:

t_id   s1    s2 1      21    26 1      20    25 1      21    22 2      20    53 2      41    62 2      30    23 

i have plotted s1 , s2 in same graph per t_id(t_id "1" have 1 color, t_id "2" have 1 color on..). want plot standard deviation per t_ids in same graph. can't figure out how this..

the answer providing uses 3 packages: tidyr, dplyr , ggplot2. it's bit hacky, think gives desired output requires data right format , calculating desired values to define aesthetics prior plotting - typical thing ggplot2. if else has easier way of doing this, i'd love see it, right best come given data provided.

first, data right format (assuming data called "df"), calculate mean , standard deviation per time point (t1, t2) , group (s1, s2), plot barplot errorbars representing mean +/- sd.

require(tidyr) require(dplyr) require(ggplot2)  df2 <- df %>% gather(group, measurement, s1:s2) df3 <- df2 %>% group_by(t_id, group) %>% mutate(sd = sd(measurement), m = mean(measurement)) gg1 <- ggplot(df3, aes(x=group, y=measurement, fill=factor(t_id))) gg1 + geom_bar(width=0.4, position=position_dodge(width=0.5), stat="identity")+geom_errorbar(aes(ymin=m-sd, ymax=m+sd), position=position_dodge(width=0.5), width=0.4, size=0.1) 

gives following

enter image description here

edit following ops specifications

first attempt didn't work.

df4 <- df %>% group_by(t_id) %>% mutate(sd1 = sd(s1)) %>% mutate(sd2 = sd(s2)) %>% mutate(mean_s1 = mean(s1)) %>% mutate(mean_s2 = mean(s2))  df4 source: local data frame [6 x 7] groups: t_id    t_id s1 s2        sd1       sd2  mean_s1  mean_s2 1    1 21 26  0.5773503  2.081666 20.66667 24.33333 2    1 20 25  0.5773503  2.081666 20.66667 24.33333 3    1 21 22  0.5773503  2.081666 20.66667 24.33333 4    2 20 53 10.5039675 20.420578 30.33333 46.00000 5    2 41 62 10.5039675 20.420578 30.33333 46.00000 6    2 30 23 10.5039675 20.420578 30.33333 46.00000  gg2 <- ggplot(df4, aes(x=s1, y=s2, fill=factor(t_id))) gg2 + geom_point(aes(col=factor(t_id)))+geom_errorbar(aes(ymin=mean_s1-sd1, ymax=mean_s1+sd2))+geom_errorbarh(aes(xmin=mean_s2-sd2, xmax=mean_s2+s2))  ### doesn't work...too many error bars mapping on place  #create new data-frame plotting coordinates geom_errobar; tried because in menu said provide new df geom_errorbar() overide plotting aesthetics,  df2 <- df %>% group_by(t_id) %>% summarise(mean_s1=mean(s1), sd_s1=sd(s1), mean_s2=mean(s2), sd_s2=sd(s2)) gg2 <- ggplot(df, aes(x=s1, y=s2, group=factor(t_id), colour=factor(t_id))) gg2 + geom_point()+geom_errorbar(aes(ymax=mean_s1+sd_s1, ymin=mean_s1-sd_s1), data=df2) error in eval(expr, envir, enclos) : object 's1' not found  # doesn't work 

second attempt.

edit - possible solution ops question

df4 <- df %>% group_by(t_id) %>% mutate(sd1 = sd(s1)) %>% mutate(sd2 = sd(s2)) %>% mutate(mean_s1 = mean(s1)) %>% mutate(mean_s2 = mean(s2))      df4     source: local data frame [6 x 7]     groups: t_id        t_id s1 s2        sd1       sd2  mean_s1  mean_s2     1    1 21 26  0.5773503  2.081666 20.66667 24.33333     2    1 20 25  0.5773503  2.081666 20.66667 24.33333     3    1 21 22  0.5773503  2.081666 20.66667 24.33333     4    2 20 53 10.5039675 20.420578 30.33333 46.00000     5    2 41 62 10.5039675 20.420578 30.33333 46.00000     6    2 30 23 10.5039675 20.420578 30.33333 46.00000 gg2 <- ggplot(df4, aes(x=s1, y=s2, fill=factor(t_id))) gg2 + geom_point(aes(col=factor(t_id)))+ +     geom_errorbar(aes(x=mean_s2, y=mean_s1, ymin=mean_s1-sd1,ymax=mean_s1+sd2, colour=factor(t_id)))+geom_errorbarh(aes(x=mean_s2, y=mean_s1, xmin=mean_s1-sd1, xmax=mean_s1+sd2, colour=factor(t_id))) 

provides plot below, errorbars plotted according longitude , lattitude. gather real data errorbars more aesthetically pleasing eye. scatter xy errobars


Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -