问题
I adjusted different models considering the response variable (massaseca) as a function of (tempo) for each treatment level (teor) using the ggplot2 function combined with the stat_poly_eq function.
However, as can be seen in the graph below, the legends of the estimated lines are overlapping. I would like these to be stacked in the left corner. When using the stat_regline_equation function (label.y = 380, label.x = 1000) it is possible to move the legend, however, they are still superimposed.
data: 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:
In this case, it is necessary to change to geom_text_npc()
, which also makes the position of the equations relative to the plotting area (given using numbers in [0..1]), so avoids problems if one changes the scale limits. (This approach is shown in the vignette of the package, using and example with facets but fewer groups.)
library(ggplot2)
library(ggpubr)
library(ggpmisc)
my.formula <- y ~ x
ggplot(dadosnew, aes(x = Tempo, y = massaseca,
color=interaction(Fator,Trat))) +
stat_summary(geom = "point", fun = mean) +
stat_smooth(method = "lm", se=FALSE, formula=my.formula) +
stat_poly_eq(geom = "text_npc",
formula = my.formula,eq.with.lhs = "As-italic(hat(y))~`=`~",
aes(label = paste(..eq.label.., ..rr.label.., sep = "*plain(\",\")~")),
parse = TRUE, size = 4,
label.x = 0.33,
label.y = c(0.95, 0.90, 0.85, 0.80, 0.75,
0.95, 0.90, 0.85, 0.80, 0.75),
hjust = "left", vjust = "center") +
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)
By the way, I would have used smaller text for axis labels. I also tidied up a little the code, in particular, the idea of saving the formula to a variable is to make sure that the same formula is used in both stats.
来源:https://stackoverflow.com/questions/65764572/overlay-of-the-legend-of-the-estimated-lines-using-the-function-stat-poly-eq