有没有一种方法可以使用 ggpmisc::stat_poly_eq 将观察数量添加到绘图标签
Is there a way to add number of observations to plot label using ggpmisc::stat_poly_eq
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x = x, y = y,
group = c("A", "B"),
y2 = y * c(0.5,2),
w = sqrt(x))
formula <- y ~ poly(x, 3, raw = TRUE)
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula, parse = TRUE)
像这样:
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
ggpmisc::stat_poly_eq(aes(label = paste(stat(rr.label), paste("N ~`=`~", nrow(my.data)), sep = "*\", \"*")), formula = formula, parse=T)
您可以使用 aes
添加其他文本。由于字符串已被解析,因此您必须使用 ~`=`~
.
转义等号
编辑:有分面
您可以创建一个额外的未使用的组行计数映射,用作粘贴语句中的变量来代替 nrow(my.data)
。
ggplot(my.data %>% group_by(group) %>% mutate(n = n()), aes(x, y, n = n)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
facet_grid(vars(group)) +
ggpmisc::stat_poly_eq(aes(label = paste(stat(rr.label), paste("N ~`=`~", n), sep = "*\", \"*")),
formula = formula, parse=T)
我发现了这种方法:
library(gginnards)
ggplot(my.data, aes(x, y)) +
+ geom_point() +
+ geom_smooth(method = "lm", formula = formula) +
+ stat_poly_eq(formula = formula, geom = "debug",
+ summary.fun = colnames)
哪个给了我这个列表:
Input 'data' to 'draw_panel()':
[1] "npcx" "npcy" "label" "eq.label"
[5] "rr.label" "adj.rr.label" "AIC.label" "BIC.label"
[9] "f.value.label" "p.value.label" "n.label" "grp.label"
[13] "r.squared" "adj.r.squared" "p.value" "n"
[17] "x" "y" "PANEL" "group"
结合本站信息:https://www.rdocumentation.org/packages/ggpmisc/versions/0.4.3/topics/stat_poly_eq
你应该能够解决你的大部分问题。
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x = x, y = y,
group = c("A", "B"),
y2 = y * c(0.5,2),
w = sqrt(x))
formula <- y ~ poly(x, 3, raw = TRUE)
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula, parse = TRUE)
像这样:
ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
ggpmisc::stat_poly_eq(aes(label = paste(stat(rr.label), paste("N ~`=`~", nrow(my.data)), sep = "*\", \"*")), formula = formula, parse=T)
您可以使用 aes
添加其他文本。由于字符串已被解析,因此您必须使用 ~`=`~
.
编辑:有分面
您可以创建一个额外的未使用的组行计数映射,用作粘贴语句中的变量来代替 nrow(my.data)
。
ggplot(my.data %>% group_by(group) %>% mutate(n = n()), aes(x, y, n = n)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
facet_grid(vars(group)) +
ggpmisc::stat_poly_eq(aes(label = paste(stat(rr.label), paste("N ~`=`~", n), sep = "*\", \"*")),
formula = formula, parse=T)
我发现了这种方法:
library(gginnards)
ggplot(my.data, aes(x, y)) +
+ geom_point() +
+ geom_smooth(method = "lm", formula = formula) +
+ stat_poly_eq(formula = formula, geom = "debug",
+ summary.fun = colnames)
哪个给了我这个列表:
Input 'data' to 'draw_panel()':
[1] "npcx" "npcy" "label" "eq.label"
[5] "rr.label" "adj.rr.label" "AIC.label" "BIC.label"
[9] "f.value.label" "p.value.label" "n.label" "grp.label"
[13] "r.squared" "adj.r.squared" "p.value" "n"
[17] "x" "y" "PANEL" "group"
结合本站信息:https://www.rdocumentation.org/packages/ggpmisc/versions/0.4.3/topics/stat_poly_eq
你应该能够解决你的大部分问题。