Overlay the legend of the estimated lines using the stat_poly_eq function

Asked

Viewed 28 times

2

I adjusted different models considering the response variable (massaseca) as a function of (time) for each treatment level (content) using the ggplot2 package combined with the stat_poly_eq function.

However, as can be seen in the following graphic, the captions of the estimated lines are superimposed. I would like them to be stacked in the left corner separately. When using the stat_regline_equation function (label.y = 380, label.x = 1000) it is possible to move the legend, however they are still overlaid.

dice: https://drive.google.com/file/d/1Y-GsNNcYINqtO-hcJfNRgaj545JZXZIS/view?usp=sharing

library(ggplot2)
library(ggpubr)
library(ggpmisc)

my.formula <- y ~ x
ggplot(dadosnew, aes(x = Tempo, y = massaseca, group = interaction(Fator,Trat),
                     color=interaction(Fator,Trat))) +
  stat_summary(geom = "point", fun = mean) + 
  stat_smooth(method = "lm", se=FALSE,  formula=y ~ poly(x, 1, raw=TRUE)) +
  stat_poly_eq(formula = my.formula,eq.with.lhs = "As-italic(hat(y))~`=`~",
               aes(label = paste(..eq.label.., ..rr.label.., sep = "*plain(\",\")~")),
               parse = TRUE, size = 5, label.y = 35)+ 
  labs(title = "",
       x = "Time (Minutes)",
       y = "Weight (mg)") + theme_bw() +
  theme(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(~Fator)

1 answer

2


The method for not overlapping equations is to use the arguments

  • label.x.npc for alignment on the x-axis;
  • label.y.npc for alignment on the y-axis.

The latter is what needs to be viewed carefully. After several attempts, the following values are found:

  • ylim_sup how much to increase the axis of y;
  • xlim_sup how much to increase the x axis;
  • label_y_npc the distance between the equations.

And the final code is then the following.

my.formula <- y ~ x
ylim_sup <- 1.1 * max(dadosnew$massaseca)
ylim_inf <- min(dadosnew$massaseca)

label_y_npc <- rep(seq(0.99, by = -0.05, length.out = 5), 2)

dadosnew %>%
  group_by(Tempo, Fator, Trat) %>%
  summarise(massaseca = mean(massaseca, na.rm = TRUE), .groups = 'drop') %>%
  ggplot(aes(x = Tempo, y = massaseca, color = Trat, group = Trat)) +
  geom_point() + 
  stat_smooth(method = "lm", se = FALSE,  formula = y ~ poly(x, 1, raw=TRUE)) +
  stat_poly_eq(formula = my.formula, 
               eq.with.lhs = "italic(hat(y))~`=`~",
               aes(label = paste(..eq.label.., ..rr.label.., sep = "*plain(\", \")~")),
               label.x.npc = "left", 
               label.y.npc = label_y_npc,
               parse = TRUE) + 
  ylim(ylim_inf, ylim_sup) +
  labs(title = "",
       x = "Time (Minutes)",
       y = "Weight (mg)",
       color = "Trat") + 
  theme_bw() +
  theme(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(~Fator)

inserir a descrição da imagem aqui

  • Thanks @Ruibarradas!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.