使用函数 stat_poly_eq 叠加估计线的图例
overlay of the legend of the estimated lines using the function stat_poly_eq
我使用 ggplot2 函数结合 stat_poly_eq 函数调整了不同的模型,将响应变量 (massaseca) 作为每个治疗水平 (teor) 的 (tempo) 的函数。
但是,如下图所示,估计线的图例是重叠的。我想把这些堆放在左角。使用stat_regline_equation函数时(label.y = 380,label.x = 1000)可以移动图例,但是,它们仍然叠加。
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)
在这种情况下,需要改为geom_text_npc()
,这也使得方程的位置相对于绘图区域(使用[0..1]中的数字给出),因此避免了问题如果改变规模限制。 (此方法显示在包的小插图中,使用和示例带有 facets 但组较少。)
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)
顺便说一下,我会为轴标签使用较小的文本。我还整理了一些代码,特别是将公式保存到变量的想法是确保在 stats.
中使用相同的公式
我使用 ggplot2 函数结合 stat_poly_eq 函数调整了不同的模型,将响应变量 (massaseca) 作为每个治疗水平 (teor) 的 (tempo) 的函数。
但是,如下图所示,估计线的图例是重叠的。我想把这些堆放在左角。使用stat_regline_equation函数时(label.y = 380,label.x = 1000)可以移动图例,但是,它们仍然叠加。
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)
在这种情况下,需要改为geom_text_npc()
,这也使得方程的位置相对于绘图区域(使用[0..1]中的数字给出),因此避免了问题如果改变规模限制。 (此方法显示在包的小插图中,使用和示例带有 facets 但组较少。)
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)
顺便说一下,我会为轴标签使用较小的文本。我还整理了一些代码,特别是将公式保存到变量的想法是确保在 stats.
中使用相同的公式