问题描述
我正在尝试叠加ggplot2中制作的两个图形,其中第一个包含使用2级多项式模型的调整线,另一个包含使用gamm函数的模型的调整线,如下面的图片.
我要完成的工作是将两个图形中的每个信息叠加在一个面上,也就是说,示例如下例所示:
但是,当尝试完成我之前描述的内容时,我执行了以下计算例程:
数据: https://drive.google.com/file/d/1QaBqcA7Y2F_eh6BOWP6Rlywzv6B4Mutl/view?usp = sharing
########################################################################################################################################################################################################################################库(ggplot2)库(样条)图书馆(NLME)图书馆(格子)图书馆(latticeExtra)要求(gamm4)库(RColorBrewer)库(ggpmisc)图书馆(dplyr)#######################################################################################################################################################################################################################################dados1 = read.table("CapilaridadeSemTempo0.csv",标头= T,sep =;",dec =,")dados2<-重塑(cbind(id = 1:nrow(dados1),dados1),变化= 5:45,v.names ="massaseca",timevar ="Tempo",times = as.numeric(gsub("X",,",tail(names(dados1),-3))),方向=长",sep =")dados = dados2 [order(dados2 $ id),]dados = na.omit(dados)dados $ Teor<-factor(dados $ Teor)dados $ Fator<-factor(dados $ Fator)#######################################################################################################################################################################################################################################dados [dados $ Teor == 0&dados $ Fator =="Am","Trat"] ="Am1:0%";dados [dados $ Teor == 6&dados $ Fator =="Am","Trat"] ="Am2:6%";dados [dados $ Teor == 8&dados $ Fator =="Am","Trat"] ="Am3:8%",dados [dados $ Teor == 10&dados $ Fator =="Am","Trat"] ="Am4:10%";dados [dados $ Teor == 12&dados $ Fator =="Am","Trat"] ="Am5:12%",dados [dados $ Teor == 0&dados $ Fator =="As","Trat"] ="As1:0%",dados [dados $ Teor == 6&dados $ Fator =="As","Trat"] ="As2:6%"dados [dados $ Teor == 8&dados $ Fator =="As","Trat"] ="As3:8%",dados [dados $ Teor == 10&dados $ Fator =="As","Trat"] ="As4:10%",dados [dados $ Teor == 12&dados $ Fator =="As","Trat"] ="As5:12%",#######################################################################################################################################################################################################################################dados $ Trat<-因子(dados $ Trat)############################################################################################################################################################################################ 模型 #################################################################################################################################################################################################fit4.gamm<-gamm(massaseca_factor(Trat)+ s(Tempo,k = 10,bs ="ps'',m = 2,by = factor(Trat)),随机=列表(id = pdSymm(〜Tempo)),数据= dados)########################################################################################################################################################################################### Ajustequadrático###########################################################################################################x11()我的公式<-y〜xylim_sup<-1.1 *最大(dados $ massaseca)ylim_inf<-分钟(dados $ massaseca)shape_brks<-唯一(dados $ Trat)shape_vals<-rep(1,10)label_y_npc<-rep(0.9,10)label_x_npc<-rep(0.91,10)p1 =数据百分比%>%group_by(Tempo,Fator,Trat)%&%summarise(massaseca =平均值(massaseca,na.rm = TRUE),. groups ='drop')%&%ggplot(aes(x = Tempo,y = massaseca,shape = Trat,color = Trat))+geom_point()+stat_smooth(method ="lm",se = FALSE,公式= y〜poly(x,2,raw = TRUE),线型= 1size = 1.1)+ scale_shape_manual(name ="Trat",休息= shape_brks,值= shape_vals)+coord_cartesian(xlim = c(0,1420))+stat_poly_eq(公式= y〜poly(x,2,raw = TRUE),eq.with.lhs ="italic(hat(y))〜`=`〜",aes(label = paste(.. eq.label ..,sep ="* plain(\",\)〜"))),label.x.npc = label_x_npc,y = 0.8,角度= 90,label.y.npc = label_y_npc,parse = TRUE,大小= 4)+ylim(ylim_inf,ylim_sup)+实验室(标题=",x =时间(分钟)",y =重量(mg)",color ="Trat")+主题(legend.position ="none",axis.title = element_text(size = 23,color ="black")),axis.text = element_text(大小= 18,颜色=黑色"),文字= element_text(大小= 20,颜色=黑色"))+facet_wrap(〜Trat,ncol = 5,nrow = 2)############################################################################################################## ggplot 2-ajuste gamm #######################################################################################################x11()p2 = ggplot(dados,aes(x = Tempo,y = fit(fit4.gamm $ lme),group = id))+facet_wrap(〜Trat,ncol = 5,nrow = 2)+xlab(时间(分钟)")+geom_point(大小= 3,颜色=#969696")+geom_line(aes(x = Tempo,y = fitted(fit4.gamm $ lme)))+ylab(体重(mg)")+主题(legend.title = element_text(size = 20),legend.text = element_text(大小= 20),strip.text.x = element_text(size = 22,color ="black"),axis.text.x = element_text(大小= 18,颜色=黑色"),axis.text.y = element_text(大小= 18,颜色=黑色"),axis.title = element_text(大小= 22),axis.text = element_text(大小= 25))p1 + p2
我遇到以下错误:
Erro:无法将"p2"添加到ggplot对象.运行`rlang :: last_error()`以查看错误发生的位置.
众所周知,ggplot2具有分层定义的结构,因此该解决方案的结构是按照期望的特殊性逐步构建语法的结构.合并.
解决方案:
my.formula<-y〜xylim_sup<-1.1 *最大(dados $ massaseca)ylim_inf<-分钟(dados $ massaseca)shape_brks<-唯一(dados $ Trat)shape_vals<-rep(1,10)label_y_npc<-rep(1.0,10)label_x_npc<-rep(0.91,10)dados%&%;%group_by(Tempo,Fator,Trat)%&%summarise(massaseca =平均值(massaseca,na.rm = TRUE),. groups ='drop')%&%ggplot(aes(x = Tempo,y = massaseca,color = Trat))+geom_point(数据=数据表,aes(x = Tempo,y =适合(fit4.gamm $ lme),group = id),color =#969696")+geom_line(data = dados,aes(x = Tempo,y = fitted(fit4.gamm $ lme),group = id),col =黑色")+stat_smooth(method ="lm",se = FALSE,公式= y〜poly(x,2,raw = TRUE),线型= 1大小= 0.7)+ scale_shape_manual(name ="Trat",休息= shape_brks,值= shape_vals)+geom_point()+coord_cartesian(xlim = c(0,1420))+stat_poly_eq(公式= y〜poly(x,2,raw = TRUE),eq.with.lhs ="italic(hat(y))〜`=`〜",aes(label = paste(.. eq.label ..,sep ="* plain(\",\)〜"))),label.x.npc = label_x_npc,y = 1.0,角度= 90,label.y.npc = label_y_npc,parse = TRUE,大小= 4.3)+ylim(ylim_inf,ylim_sup)+实验室(标题=",x =时间(分钟)",y =重量(mg)",color ="Trat")+theme(legend.position ="none",legend.title = element_text(size = 20),legend.text = element_text(大小= 20),strip.text.x = element_text(size = 22,color ="black"),axis.text.x = element_text(大小= 18,颜色=黑色"),axis.text.y = element_text(大小= 18,颜色=黑色"),axis.title = element_text(大小= 22),axis.text = element_text(大小= 25),文字= element_text(size = 20,color ="black"))+facet_wrap(〜Trat,ncol = 5,nrow = 2)
I am trying to superimpose two graphs made in ggplot2, in which the first contains the adjusted line using a polynomial model of degree 2 and the other contains the adjusted lines of a model using the gamm function, as can be seen in the images below.
What I am trying to accomplish is to overlay each of the information in both graphs on a single face, that is, an example would be as in the following assembly:
However, when trying to accomplish what I described earlier, I performed the following computational routine:
Data: https://drive.google.com/file/d/1QaBqcA7Y2F_eh6BOWP6Rlywzv6B4Mutl/view?usp=sharing
################################################################################
################################################################################
################################################################################
library(ggplot2)
library(splines)
library(nlme)
library(lattice)
library(latticeExtra)
require(gamm4)
library(RColorBrewer)
library(ggpmisc)
library(dplyr)
################################################################################
################################################################################
################################################################################
dados1 = read.table("CapilaridadeSemTempo0.csv", header = T, sep=";", dec=",")
dados2 <- reshape(cbind(id=1:nrow(dados1), dados1),
varying=5:45,
v.names="massaseca",
timevar="Tempo",
times=as.numeric(gsub("X", "", tail(names(dados1), -3))),
direction="long", sep="")
dados=dados2[order(dados2$id), ]
dados = na.omit(dados)
dados$Teor <- factor(dados$Teor)
dados$Fator <- factor(dados$Fator)
################################################################################
################################################################################
################################################################################
dados[dados$Teor == 0 & dados$Fator == "Am", "Trat"] = "Am1:0%"
dados[dados$Teor == 6 & dados$Fator == "Am", "Trat"] = "Am2:6%"
dados[dados$Teor == 8 & dados$Fator == "Am", "Trat"] = "Am3:8%"
dados[dados$Teor == 10 & dados$Fator == "Am", "Trat"] = "Am4:10%"
dados[dados$Teor == 12 & dados$Fator == "Am", "Trat"] = "Am5:12%"
dados[dados$Teor == 0 & dados$Fator == "As", "Trat"] = "As1:0%"
dados[dados$Teor == 6 & dados$Fator == "As", "Trat"] = "As2:6%"
dados[dados$Teor == 8 & dados$Fator == "As", "Trat"] = "As3:8%"
dados[dados$Teor == 10 & dados$Fator == "As", "Trat"] = "As4:10%"
dados[dados$Teor == 12 & dados$Fator == "As", "Trat"] = "As5:12%"
################################################################################
################################################################################
################################################################################
dados$Trat <- factor(dados$Trat)
################################################################################
################################################################################
################################## Model #######################################
################################################################################
################################################################################
fit4.gamm <- gamm(massaseca~factor(Trat)+s(Tempo,k=10,bs="ps",m=2,
by=factor(Trat)),
random=list(id=pdSymm(~Tempo)),data=dados)
################################################################################
################################################################################
################################## Ajuste quadrático ###########################
################################################################################
x11()
my.formula <- y ~ x
ylim_sup <- 1.1 * max(dados$massaseca)
ylim_inf <- min(dados$massaseca)
shape_brks <- unique(dados$Trat)
shape_vals <- rep(1, 10)
label_y_npc <- rep(0.9, 10)
label_x_npc <- rep(0.91, 10)
p1 = dados %>%
group_by(Tempo, Fator, Trat) %>%
summarise(massaseca = mean(massaseca, na.rm = TRUE),.groups = 'drop') %>%
ggplot(aes(x = Tempo, y = massaseca, shape = Trat,color = Trat)) +
geom_point() +
stat_smooth(method = "lm", se = FALSE,
formula = y ~ poly(x, 2, raw = TRUE),
linetype = 1,
size = 1.1) + scale_shape_manual(name = "Trat",
breaks = shape_brks,
values = shape_vals) +
coord_cartesian(xlim=c(0,1420)) +
stat_poly_eq(formula = y ~ poly(x, 2, raw = TRUE),
eq.with.lhs = "italic(hat(y))~`=`~",
aes(label = paste(..eq.label.., sep = "*plain(\", \")~")),
label.x.npc = label_x_npc,
y = 0.8, angle=90,
label.y.npc = label_y_npc,
parse = TRUE, size = 4) +
ylim(ylim_inf, ylim_sup) +
labs(title = "",
x = "Time (Minutes)",
y = "Weight (mg)",
color = "Trat") +
theme(legend.position = "none",axis.title = element_text(size = 23,color="black"),
axis.text = element_text(size = 18,color="black"),
text = element_text(size = 20,color="black")) +
facet_wrap(~Trat,ncol=5,nrow=2)
#################################################################################
################################# ggplot 2 - ajuste gamm ########################
#################################################################################
x11()
p2 = ggplot(dados, aes(x = Tempo, y = fitted(fit4.gamm$lme), group=id)) +
facet_wrap(~Trat,ncol=5,nrow=2) +
xlab("Time (Minutes)") +
geom_point(size=3, color="#969696") +
geom_line(aes(x=Tempo,y=fitted(fit4.gamm$lme))) +
ylab("Weight (mg)") +
theme(legend.title = element_text(size = 20),
legend.text = element_text(size = 20),
strip.text.x = element_text(size = 22,color="black"),
axis.text.x = element_text(size = 18, colour = "black"),
axis.text.y = element_text(size = 18, colour = "black"),
axis.title = element_text(size = 22),
axis.text = element_text(size = 25))
p1+p2
where I came across the following error:
Erro: Can't add `p2` to a ggplot object.
Run `rlang::last_error()` to see where the error occurred.
It is known that ggplot2 has its structure defined in layers, so the solution was structured to organize the syntax step by step in such a way that the desired specificities were incorporated.
Solution:
my.formula <- y ~ x
ylim_sup <- 1.1 * max(dados$massaseca)
ylim_inf <- min(dados$massaseca)
shape_brks <- unique(dados$Trat)
shape_vals <- rep(1, 10)
label_y_npc <- rep(1.0, 10)
label_x_npc <- rep(0.91, 10)
dados %>%
group_by(Tempo, Fator, Trat) %>%
summarise(massaseca = mean(massaseca, na.rm = TRUE),.groups = 'drop') %>%
ggplot(aes(x = Tempo, y = massaseca,color = Trat)) +
geom_point(data= dados, aes(x=Tempo,y=fitted(fit4.gamm$lme), group=id), color="#969696") +
geom_line(data= dados, aes(x=Tempo,y=fitted(fit4.gamm$lme), group=id),
col = "black") +
stat_smooth(method = "lm", se = FALSE,
formula = y ~ poly(x, 2, raw = TRUE),
linetype = 1,
size = 0.7) + scale_shape_manual(name = "Trat",
breaks = shape_brks,
values = shape_vals) +
geom_point() +
coord_cartesian(xlim=c(0,1420)) +
stat_poly_eq(formula = y ~ poly(x, 2, raw = TRUE),
eq.with.lhs = "italic(hat(y))~`=`~",
aes(label = paste(..eq.label.., sep = "*plain(\", \")~")),
label.x.npc = label_x_npc,
y = 1.0, angle=90,
label.y.npc = label_y_npc,
parse = TRUE, size = 4.3) +
ylim(ylim_inf, ylim_sup) +
labs(title = "",
x = "Time (Minutes)",
y = "Weight (mg)",
color = "Trat") +
theme(legend.position="none",legend.title = element_text(size = 20),
legend.text = element_text(size = 20),
strip.text.x = element_text(size = 22,color="black"),
axis.text.x = element_text(size = 18, colour = "black"),
axis.text.y = element_text(size = 18, colour = "black"),
axis.title = element_text(size = 22),
axis.text = element_text(size = 25),
text = element_text(size = 20,color="black"))+
facet_wrap(~Trat,ncol=5,nrow=2)
这篇关于考虑拟合线的ggplot2中的图形叠加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!