我正在尝试将mgcv :: gam结果的p值和R2添加到具有构面的ggplot中。示例数据帧和代码如下。有没有办法成功将p值和R2粘贴到ggplots上?
DF <- data.frame(Site = rep(LETTERS[20:24],each = 4),Region = rep(LETTERS[14:18],time = rep(LETTERS[1:10],each = 10),group = rep(LETTERS[1:4],value1 = runif(n = 1000,min = 10,max = 15),value2 = runif(n = 1000,min = 100,max = 150))
DF$time <- as.numeric(DF$time)
GAMFORMULA <- y ~ s(x,bs="cr",k=3)
plot1 <- ggplot(data=DF,aes(x=time,y=value2)) +
geom_point(col="gray",alpha=0.8,name="") +
geom_line(col="gray",name="",aes(group=group)) +
geom_smooth(se=T,col="darkorange",fill="orange",method="gam",formula=GAMFORMULA) +
theme_bw() +
theme(strip.text.x = element_text(size=10),strip.text.y = element_text(size=10,face="bold",angle=0),strip.background = element_rect(colour="black",fill="gray90"),axis.text.x = element_text(size=10),# remove x-axis text
axis.text.y = element_text(size=10),# remove y-axis text
axis.ticks = element_blank(),# remove axis ticks
axis.title.x = element_text(size=18),# remove x-axis labels
axis.title.y = element_text(size=25),# remove y-axis labels
panel.background = element_blank(),panel.grid.major = element_blank(),#remove major-grid labels
panel.grid.minor = element_blank(),#remove minor-grid labels
plot.background = element_blank()) +
labs(y="Value",x="Time",title = "") +
stat_fit_glance(method = "gam",method.args = list(formula = GAMFORMULA),aes(label = sprintf('R^2~"="~%.3f~~italic(p)~"="~%.2f',stat(..r.squared..),stat(..p.value..))),parse = TRUE)
plot1 + facet_wrap(Site~group,scales="free_y",ncol=3)
Error in sprintf("R^2~\"=\"~%.3f~~italic(p)~\"=\"~%.2f",r.squared,p.value) :
object 'r.squared' not found