[R] geom_smooth with sd

sibyiie@stoeckii m@iii@g oii gmx@ch sibyiie@stoeckii m@iii@g oii gmx@ch
Sun Aug 11 18:10:12 CEST 2024


Dear community

 

Using after_stat() I was able to visualise ggplot with standard deviations
instead of a confidence interval as seen in the R help.

 

p1<-ggplot(data = MS1, aes(x= Jahr, y= QI_A,color=Bio, linetype=Bio)) + 

                geom_smooth(aes(fill=Bio,
ymax=after_stat(y+se*sqrt(length(y))), ymin=after_stat(y-se*sqrt(y))) ,
method = "lm" , formula = y ~ x + I(x^2),linewidth=1) +

                theme(panel.background = element_blank())+

                theme(axis.line = element_line(colour = "black"))+

  theme(axis.text=element_text(size=18))+

  theme(axis.title=element_text(size=20))+

                ylab("Anteil BFF an LN [%]") +xlab("Jahr")+

  scale_color_manual(values=c("red","darkgreen"), labels=c("�LN", "BIO"))+

  scale_fill_manual(values=c("red","darkgreen"), labels= c("�LN", "BIO"))+

                theme(legend.title = element_blank())+

  theme(legend.text=element_text(size=20))+

  scale_linetype_manual(values=c("dashed", "solid"), labels=c("�LN", "BIO"))

p1<-p1 + expand_limits(y=c(0, 30))

 

When comparing the plots to the simple statistics the standard deviation do
not match. I assume it is because of the na.rm=TRUE which does not match
length(y) in the  after_stat code. However I was not able to adapt the code
using NA values?

 

Simple statistics

> mean(MS2020[MS2020$Bio=="1",]$QI_A, na.rm=TRUE)

[1] 26.81225

> sd(MS2020[MS2020$Bio=="1",]$QI_A, na.rm=TRUE)

[1] 21.12419

> mean(MS2020[MS2020$Bio=="0",]$QI_A, na.rm=TRUE)

[1] 15.86196

> sd(MS2020[MS2020$Bio=="0",]$QI_A, na.rm=TRUE)

[1] 15.00405

 

Kind regards

Sibylle


	[[alternative HTML version deleted]]



More information about the R-help mailing list