ggplot2:在每个面上添加回归线方程时 x 轴出现问题
ggplot2: Problem with x axis when adding regression line equation on each facet
基于这里的例子
Adding Regression Line Equation and R2 on graph,我正在努力将模型的回归线方程包含在每个方面。但是,我不明白为什么要更改 x 轴的限制。
library(ggplot2)
library(reshape2)
df <- data.frame(year = seq(1979,2010), M02 = runif(32,-4,6),
M06 = runif(32, -2.4, 5.1), M07 = runif(32, -2, 7.1))
df <- melt(df, id = c("year"))
ggplot(data = df, mapping = aes(x = year, y = value)) +
geom_point() +
scale_x_continuous() +
stat_smooth_func(geom = 'text', method = 'lm', hjust = 0, parse = T) +
geom_smooth(method = 'lm', se = T) +
facet_wrap(~ variable) # as you can see, the scale_x_axis goes back to 1800
如果我在 x 上包含限制,
scale_x_continuous(limits = c(1979,2010))
不再显示回归系数。我在这里做错了什么?
stat_smooth_func 可在此处获取: https://gist.github.com/kdauria/524eade46135f6348140
可能有人会提出更好的解决方案,但作为替代方案,您可以更改 stat_smooth_func 并且可以像这样制作最后一行
data.frame(x=1979, y=ypos, label=func_string)
而不是
data.frame(x=xpos, y=ypos, label=func_string)
所以,剧情会像下面这样
您可以使用 ggpmisc
包中的 stat_poly_eq
函数。
library(reshape2)
library(ggplot2)
library(ggpmisc)
#> For news about 'ggpmisc', please, see https://www.r4photobiology.info/
#> For on-line documentation see https://docs.r4photobiology.info/ggpmisc/
df <- data.frame(year = seq(1979,2010), M02 = runif(32,-4,6),
M06 = runif(32, -2.4, 5.1), M07 = runif(32, -2, 7.1))
df <- melt(df, id = c("year"))
formula1 <- y ~ x
ggplot(data = df, mapping = aes(x = year, y = value)) +
geom_point() +
scale_x_continuous() +
geom_smooth(method = 'lm', se = TRUE) +
stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~~")),
label.x = "left", label.y = "top",
formula = formula1, parse = TRUE, size = 3) +
facet_wrap(~ variable)
ggplot(data = df, mapping = aes(x = year, y = value)) +
geom_point() +
scale_x_continuous() +
geom_smooth(method = 'lm', se = TRUE) +
stat_poly_eq(aes(label = paste(..eq.label.., sep = "~~~")),
label.x = "left", label.y = 0.15,
eq.with.lhs = "italic(hat(y))~`=`~",
eq.x.rhs = "~italic(x)",
formula = formula1, parse = TRUE, size = 4) +
stat_poly_eq(aes(label = paste(..rr.label.., sep = "~~~")),
label.x = "left", label.y = "bottom",
formula = formula1, parse = TRUE, size = 4) +
facet_wrap(~ variable)
由 reprex package (v0.2.1.9000)
创建于 2019-01-10
基于这里的例子 Adding Regression Line Equation and R2 on graph,我正在努力将模型的回归线方程包含在每个方面。但是,我不明白为什么要更改 x 轴的限制。
library(ggplot2)
library(reshape2)
df <- data.frame(year = seq(1979,2010), M02 = runif(32,-4,6),
M06 = runif(32, -2.4, 5.1), M07 = runif(32, -2, 7.1))
df <- melt(df, id = c("year"))
ggplot(data = df, mapping = aes(x = year, y = value)) +
geom_point() +
scale_x_continuous() +
stat_smooth_func(geom = 'text', method = 'lm', hjust = 0, parse = T) +
geom_smooth(method = 'lm', se = T) +
facet_wrap(~ variable) # as you can see, the scale_x_axis goes back to 1800
如果我在 x 上包含限制,
scale_x_continuous(limits = c(1979,2010))
不再显示回归系数。我在这里做错了什么?
stat_smooth_func 可在此处获取: https://gist.github.com/kdauria/524eade46135f6348140
可能有人会提出更好的解决方案,但作为替代方案,您可以更改 stat_smooth_func 并且可以像这样制作最后一行
data.frame(x=1979, y=ypos, label=func_string)
而不是
data.frame(x=xpos, y=ypos, label=func_string)
所以,剧情会像下面这样
您可以使用 ggpmisc
包中的 stat_poly_eq
函数。
library(reshape2)
library(ggplot2)
library(ggpmisc)
#> For news about 'ggpmisc', please, see https://www.r4photobiology.info/
#> For on-line documentation see https://docs.r4photobiology.info/ggpmisc/
df <- data.frame(year = seq(1979,2010), M02 = runif(32,-4,6),
M06 = runif(32, -2.4, 5.1), M07 = runif(32, -2, 7.1))
df <- melt(df, id = c("year"))
formula1 <- y ~ x
ggplot(data = df, mapping = aes(x = year, y = value)) +
geom_point() +
scale_x_continuous() +
geom_smooth(method = 'lm', se = TRUE) +
stat_poly_eq(aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~~")),
label.x = "left", label.y = "top",
formula = formula1, parse = TRUE, size = 3) +
facet_wrap(~ variable)
ggplot(data = df, mapping = aes(x = year, y = value)) +
geom_point() +
scale_x_continuous() +
geom_smooth(method = 'lm', se = TRUE) +
stat_poly_eq(aes(label = paste(..eq.label.., sep = "~~~")),
label.x = "left", label.y = 0.15,
eq.with.lhs = "italic(hat(y))~`=`~",
eq.x.rhs = "~italic(x)",
formula = formula1, parse = TRUE, size = 4) +
stat_poly_eq(aes(label = paste(..rr.label.., sep = "~~~")),
label.x = "left", label.y = "bottom",
formula = formula1, parse = TRUE, size = 4) +
facet_wrap(~ variable)
由 reprex package (v0.2.1.9000)
创建于 2019-01-10