I'd like to extract the mean and SD forTratamentos
level ("T-0","T-2","T-3","T-5") in each response variable ("GE", "PC", "IVE", "CE", "AM","EE","FB","PB") using a loop, but doesn't work:
# Data set
ds.1.1 <- read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/exp_ivan_1.csv")
ds.1.2 <- ds.1.1%>%dplyr::select(!EM)
str(ds.1.2)
#'data.frame': 16 obs. of 9 variables:
# $ Tratamentos: chr "T-0" "T-0" "T-0" "T-0" ...
# $ GE : num 0.77 0.77 0.79 0.79 0.77 0.67 0.67 0.67 0.69 0.71 ...
# $ PC : num 0.56 0.56 0.54 0.58 0.48 0.48 0.5 0.5 0.4 0.48 ...
# $ IVE : num 5.67 5.47 5.49 5.8 5.04 ...
# $ CE : num 50.3 51.1 50.8 49.7 58.2 ...
# $ AM : num 0.724 0.691 NA NA 0.571 ...
# $ EE : num 0.0205 0.0196 NA NA 0.0254 ...
# $ FB : num 0.294 0.287 NA NA 0.291 ...
# $ PB : num 0.105 0.106 NA NA 0.11 ...
# Mean and sd
vars<-colnames(ds.1.2)[-1]
for (j in 2:length(vars)){
# Aggregate mean
df_err<-ds.1.2%>%group_by(Tratamentos)%>%
summarize(vars[j] = mean(vars[j], na.rm = TRUE))
df_err<-as.data.frame(df_err)
print(df_err)
# Aggregate standart error
df_sd_err<-ds.1.2%>%group_by(Tratamentos)%>%
summarize(sd = stats::sd(vars[j], na.rm = TRUE)/sqrt(length(vars[j])-1))
df_sd_err<-as.data.frame(df_sd_err)
print(df_sd_err)
# The errorbars overlapped, so use position_dodge to move them horizontally
pd <- position_dodge(0.1)#position_jitter(height = 0.001) # move them .05 to the left and right
# Use 95% confidence interval instead of SEM
plots_pull[[j]] <-ggplot(data=df_err, aes(x=Tratamentos, y=vars[j])) +
geom_errorbar(data=df_err,mapping=aes(ymin=vars[j]-df_sd_err$sd, ymax=vars[j]+df_sd_err$sd),position=pd, width=0.2) +
geom_line(aes(linetype = Tratamentos), position = pd) +
scale_colour_grey(start = 0, end = .5) +
theme_bw() + theme(
panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),
axis.text.x=element_text(angle = 0, hjust = 0),
axis.text.y=element_text(angle = 0, hjust = 0),
text=element_text(size=18, family="serif"),legend.position="none")
}
ggarrange(plots_pull[[1]], plots_pull[[2]], plots_pull[[3]],plots_pull[[4]], plots_pull[[5]], plots_pull[[6]], plots_pull[[7]], plots_pull[[7]] + rremove("x.text"),
font.label = list(size = 14, color = "black", face = "plain", family = "serif"),
ncol = 2, nrow = 4,
align = "hv",
common.legend = TRUE,
legend="bottom")
}
#
#Error: unexpected '=' in:
#" df_err<-ds.1.2%>%group_by(Tratamentos)%>%
# summarize(vars[j] ="
But if a make each one manually is OK:
## Aggregate mean
df_err<-ds.1.2%>%group_by(Tratamentos)%>%
summarize(GE = mean(GE, na.rm = TRUE))
df_err<-as.data.frame(df_err)
print(df_err)
# Tratamentos GE
#1 T-0 0.780
#2 T-2 0.695
#3 T-3 0.710
#4 T-5 0.685
# Aggregate standart error
df_sd_err<-ds.1.2%>%group_by(Tratamentos)%>%
summarize(sd = stats::sd(GE, na.rm = TRUE)/sqrt(length(GE)-1))
df_sd_err<-as.data.frame(df_sd_err)
print(df_sd_err)
# Tratamentos sd
#1 T-0 0.006666667
#2 T-2 0.028867513
#3 T-3 0.016329932
#4 T-5 0.011055416
Please, do any ideas for my loop finally work?
Thanks in advance!!