Olá!
Estou com certa dificuldade ao gerar gráficos com modelos distintos de regressão (quadrático, linear) (FIGURA 1), observa-se que os gráficos ficam soltos (escalas "free") ao usar o grid.arrange()
, havendo a necessidade previamente de gerar 2 gráficos, com seus respectivos modelo de regressão, demandando tempo.
FIGURA 1 - USANDO grid.arrange
Normalmente utilizando o ggplot2::ggplot()
, eu faço uso função do pacote ggpmisc::stat_poly_eq()
, pois, ela já organiza as informações, e plota, sem a necessidade de ficar definindo as coordenadas (x,y).
porém eu não consigo listar dois modelos de regressão, no comando stat_poly_eq(formula=)
. Fazendo com que uma grade de gráficos facet_grid()
(Fator1 x Fator2 x ... FatorN), apresente o mesmo modelo de ajuste para todas as combinações (FIGURA 2).
FIGURA 2 - Utilizando ggplot()+facet_grid()
Dúvidas:
1. Como usar aplicar uma escala fixa, para o eixo x, ou y, pelo grid.arrange
?
(neste caso seria para retirar o excesso de informação, como legendas de eixo, e números no eixo.)
2. Como usar dois modelos de ajuste de regressão no ggplot2
utilizando o ggpmisc::stat_poly_eq()
?
(neste caso seria apenas para usar um ggplot()
, evitando ter que gerar 2 gráficos e combiná-los com o grid.arrange
).
3. Como editar manualmente a equação a ser plotada pelo ggpmisc::stat_poly_eq()
?
(neste caso eu poderia gerar usando expression()
os modelos distintos, e adicionar a significância de seus betas).
Base de dados, utilizada:
require(ggplot2)
require(ggpmisc)
require(gridExtra)
base1<-structure(list(FAT1=c(1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,
1L,1L,1L,1L,1L,1L),VAR.Y=c(-0.8,-0.75,-0.473,-1.103,
-0.72,-0.667,-0.453,-0.58,-1.327,-0.94,-0.507,-0.68,-0.66,
-0.517,-0.61,-0.893,-1.007,-0.847,-0.767,-0.753,-0.5,
-0.9,-1.24,-1.5,-0.387,-0.4,-0.673,-0.587,-0.79,-0.6,
-0.453,-0.353,-0.413,-0.84,-1.5,-0.763,-0.453,-0.753,
-0.607,-1.1,-0.647,-0.88,-0.513,-0.717,-0.52,-1.093,-1.36,
-0.507),VAR.X=c(0.6193,0.5696,0.5252,0.5643,0.542,0.5694,
0.6386,0.5671,0.5023,0.5626,0.5039,0.5501,0.5966,0.5771,
0.478,0.5855,0.5473,0.5605,0.6068,0.5402,0.4239,0.5775,
0.5254,0.541,0.6267,0.5054,0.5453,0.5699,0.4933,0.5424,
0.5557,0.6236,0.5589,0.5628,0.5364,0.5947,0.5329,0.5283,
0.5062,0.5492,0.4803,0.5593,0.64,0.5602,0.5339,0.5546,
0.5138,0.5451)),class="data.frame",row.names=c(1L,2L,
3L,4L,5L,6L,13L,14L,15L,16L,17L,18L,25L,26L,27L,
28L,29L,30L,37L,38L,39L,40L,41L,42L,49L,50L,51L,52L,
53L,54L,61L,62L,63L,64L,65L,66L,73L,74L,75L,76L,77L,
78L,85L,86L,87L,88L,89L,90L))
base2<-structure(list(FAT1=c(2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,
2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,
2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,2L,
2L,2L,2L,2L,2L,2L),VAR.Y=c(-0.64,-0.8,-0.46,-0.493,
-0.453,-0.387,-0.493,-0.567,-0.347,-0.6,-0.367,-0.97,
-0.62,-0.587,-0.507,-0.66,-0.413,-0.453,-0.6,-0.62,-0.647,
-1.033,-0.507,-0.55,-0.353,-0.4,-0.32,-0.313,-0.347,-0.367,
-0.487,-0.253,-0.413,-0.553,-0.493,-0.44,-0.767,-0.833,
-0.367,-0.713,-0.36,-0.5,-0.44,-0.293,-0.38,-0.427,-0.473,
-0.773),VAR.X=c(0.8934,0.7384,0.282,0.3243,0.2642,0.3908,
0.7625,0.7539,0.4381,0.7273,0.7282,0.4234,0.8397,0.8045,
0.4524,0.7576,0.7217,0.4341,0.855,0.7121,0.3929,0.7137,
0.7924,0.4006,0.3606,0.5003,0.5113,0.428,0.3586,0.4232,
0.6986,0.5425,0.4975,0.5746,0.4854,0.6243,0.9717,0.6435,
0.8064,0.8789,0.4311,0.4133,0.3477,0.2804,0.3823,0.3729,
0.2647,0.4872)),class="data.frame",row.names=c(7L,8L,
9L,10L,11L,12L,19L,20L,21L,22L,23L,24L,31L,32L,33L,
34L,35L,36L,43L,44L,45L,46L,47L,48L,55L,56L,57L,58L,
59L,60L,67L,68L,69L,70L,71L,72L,79L,80L,81L,82L,83L,
84L,91L,92L,93L,94L,95L,96L))
base3<-rbind(base1,base2)
graf1<-ggplot(base1, aes(y=VAR.Y,x=VAR.X))+facet_grid(.~FAT1)+
geom_point(color="black")+
geom_smooth(method="lm", se=FALSE, span = .8,color="black")+
theme_bw()+lims(y=c(-1.8,0))+
stat_poly_eq(formula = y~I(x)+I(x^2),
eq.with.lhs = "italic(hat(Y))~`=`~",
aes(label = paste(..eq.label.., ..rr.label.., sep = "*plain(\",\")~")), parse = TRUE)
graf2<-ggplot(base2, aes(y=VAR.Y,x=VAR.X))+facet_grid(.~FAT1)+
geom_point(color="black")+
geom_smooth(method="lm", se=FALSE, span = .8,color="black")+
theme_bw()+lims(y=c(-1.8,0))+
stat_poly_eq(formula = y~I(x),
eq.with.lhs = "italic(hat(Y))~`=`~",
aes(label = paste(..eq.label.., ..rr.label.., sep = "*plain(\",\")~")), parse = TRUE)
graf3<-grid.arrange(graf1,graf2,ncol=2)
ggplot(base3, aes(y=VAR.Y,x=VAR.X))+facet_grid(.~FAT1)+
geom_point(color="black")+
geom_smooth(method="lm", se=FALSE, span = .8,color="black")+
theme_bw()+lims(y=c(-1.8,0))+
stat_poly_eq(formula = y~I(x)+I(x^2),
eq.with.lhs = "italic(hat(Y))~`=`~",
aes(label = paste(..eq.label.., ..rr.label.., sep = "*plain(\",\")~")), parse = TRUE)