我stat_poly_eq()
在包装ggpmisc
中包含了一个统计信息,可以得出以下答案:
library(ggplot2)
library(ggpmisc)
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
my.formula <- y ~ x
p <- ggplot(data = df, aes(x = x, y = y)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = my.formula) +
stat_poly_eq(formula = my.formula,
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
parse = TRUE) +
geom_point()
p
此统计信息可用于任何不缺少项的多项式,并且希望具有足够的灵活性以普遍使用。R ^ 2或调整后的R ^ 2标签可与lm()拟合的任何模型公式一起使用。作为ggplot统计信息,它在组和构面方面的行为均符合预期。
可以通过CRAN获得“ ggpmisc”软件包。
0.2.6版本刚刚被CRAN接受。
它处理@shabbychef和@ MYaseen208的评论。
@ MYaseen208这显示了如何添加帽子。
library(ggplot2)
library(ggpmisc)
df <- data.frame(x = c(1:100))
df$y <- 2 + 3 * df$x + rnorm(100, sd = 40)
my.formula <- y ~ x
p <- ggplot(data = df, aes(x = x, y = y)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = my.formula) +
stat_poly_eq(formula = my.formula,
eq.with.lhs = "italic(hat(y))~`=`~",
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
parse = TRUE) +
geom_point()
p
@shabbychef现在可以将方程式中的变量与轴标签所使用的变量匹配。要更换X有发言权ž和ÿ与^ h,应当使用:
p <- ggplot(data = df, aes(x = x, y = y)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = my.formula) +
stat_poly_eq(formula = my.formula,
eq.with.lhs = "italic(h)~`=`~",
eq.x.rhs = "~italic(z)",
aes(label = ..eq.label..),
parse = TRUE) +
labs(x = expression(italic(z)), y = expression(italic(h))) +
geom_point()
p
通过这些正常的R解析表达式,希腊字母现在也可以在等式的lhs和rhs中使用。
[2017-03-08] @elarry Edit以更精确地解决原始问题,展示如何在方程式标签和R2标签之间添加逗号。
p <- ggplot(data = df, aes(x = x, y = y)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = my.formula) +
stat_poly_eq(formula = my.formula,
eq.with.lhs = "italic(hat(y))~`=`~",
aes(label = paste(..eq.label.., ..rr.label.., sep = "*plain(\",\")~")),
parse = TRUE) +
geom_point()
p
[2019-10-20] @ helen.h我在下面给出stat_poly_eq()
了分组使用的示例。
library(ggpmisc)
df <- data.frame(x = c(1:100))
df$y <- 20 * c(0, 1) + 3 * df$x + rnorm(100, sd = 40)
df$group <- factor(rep(c("A", "B"), 50))
my.formula <- y ~ x
p <- ggplot(data = df, aes(x = x, y = y, colour = group)) +
geom_smooth(method = "lm", se=FALSE, formula = my.formula) +
stat_poly_eq(formula = my.formula,
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
parse = TRUE) +
geom_point()
p
p <- ggplot(data = df, aes(x = x, y = y, linetype = group)) +
geom_smooth(method = "lm", se=FALSE, formula = my.formula) +
stat_poly_eq(formula = my.formula,
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
parse = TRUE) +
geom_point()
p
[2020-01-21] @Herman乍一看可能有点违反直觉,但是在使用分组时要获得一个方程,则需要遵循图形语法。将创建分组的映射限制为单个图层(如下所示),或者保留默认映射,并在不需要分组的层中使用恒定值覆盖它(例如colour = "black"
)。
继续上一个示例。
p <- ggplot(data = df, aes(x = x, y = y)) +
geom_smooth(method = "lm", se=FALSE, formula = my.formula) +
stat_poly_eq(formula = my.formula,
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
parse = TRUE) +
geom_point(aes(colour = group))
p
[2020-01-22]为了完整起见,以小平面为例,说明在这种情况下也满足了图形语法的期望。
library(ggpmisc)
df <- data.frame(x = c(1:100))
df$y <- 20 * c(0, 1) + 3 * df$x + rnorm(100, sd = 40)
df$group <- factor(rep(c("A", "B"), 50))
my.formula <- y ~ x
p <- ggplot(data = df, aes(x = x, y = y)) +
geom_smooth(method = "lm", se=FALSE, formula = my.formula) +
stat_poly_eq(formula = my.formula,
aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),
parse = TRUE) +
geom_point() +
facet_wrap(~group)
p
latticeExtra::lmlineq()
。