如何使用 ggpmisc 的 stat_poly_eq 在方程式中显示不同的 y 标签
How to display different y labels in the equations using stat_poly_eq of ggpmisc
我正在尝试使用 ggpmisc
的 stat_poly_eq
函数在绘图上显示方程式。
我的问题是如何通过参考 key
参数将等式中的 y= ...
更改为 y1=...
和 y2=...
。
我试图在映射中添加 eq.with.lhs
参数,但它无法识别该参数。
我试图将一个向量传递给 eq.with.lhs
参数,但它与每个方程中的两个元素重叠......
你有更好的主意吗?
在最后一个案例中,我可以在自己计算方程系数后使用geom_text
,但它似乎是一种不太有效的解决问题的方法。
这是我的问题的代表。
data <- data.frame(x = rnorm(20)) %>%
mutate(y1 = 1.2*x + rnorm(20, sd=0.2),
y2 = 0.9*x + rnorm(20, sd=0.3)) %>%
gather(value = value, key = key, -x)
ggplot(data, aes(x = x, y = value)) +
geom_point(aes(shape = key, colour = key)) +
stat_poly_eq(aes(label = ..eq.label.., colour = key),
formula = y ~ poly(x, 1, raw = TRUE),
eq.x.rhs = "x",
# eq.with.lhs = c(paste0(expression(y[1]), "~`=`~"),
# paste0(expression(y[2]), "~`=`~")),
eq.with.lhs = paste0(expression(y[ind]), "~`=`~"),
parse = TRUE) +
ylab(NULL)
我不太确定是否可以通过 ggpmisc
做到这一点,但是您可以在构建绘图后更改数据,如下所示:
library(tidyverse)
library(ggpmisc)
data <- data.frame(x = rnorm(20)) %>%
mutate(y1 = 1.2*x + rnorm(20, sd=0.2),
y2 = 0.9*x + rnorm(20, sd=0.3)) %>%
gather(value = value, key = key, -x)
p <- ggplot(data, aes(x = x, y = value)) +
geom_point(aes(shape = key, colour = key)) +
stat_poly_eq(aes(label = ..eq.label.., colour = key),
formula = y ~ poly(x, 1, raw = TRUE),
eq.x.rhs = "x",
eq.with.lhs = paste0(expression(y), "~`=`~"),
parse = TRUE) +
ylab(NULL)
temp <- ggplot_build(p)
temp$data[[2]]$label <- temp$data[[2]]$label %>%
fct_relabel(~ str_replace(.x, "y", paste0("y[", 1:2, "]")))
grid::grid.newpage()
grid::grid.draw(ggplot_gtable(temp))
我正在尝试使用 ggpmisc
的 stat_poly_eq
函数在绘图上显示方程式。
我的问题是如何通过参考 key
参数将等式中的 y= ...
更改为 y1=...
和 y2=...
。
我试图在映射中添加 eq.with.lhs
参数,但它无法识别该参数。
我试图将一个向量传递给 eq.with.lhs
参数,但它与每个方程中的两个元素重叠......
你有更好的主意吗?
在最后一个案例中,我可以在自己计算方程系数后使用geom_text
,但它似乎是一种不太有效的解决问题的方法。
这是我的问题的代表。
data <- data.frame(x = rnorm(20)) %>%
mutate(y1 = 1.2*x + rnorm(20, sd=0.2),
y2 = 0.9*x + rnorm(20, sd=0.3)) %>%
gather(value = value, key = key, -x)
ggplot(data, aes(x = x, y = value)) +
geom_point(aes(shape = key, colour = key)) +
stat_poly_eq(aes(label = ..eq.label.., colour = key),
formula = y ~ poly(x, 1, raw = TRUE),
eq.x.rhs = "x",
# eq.with.lhs = c(paste0(expression(y[1]), "~`=`~"),
# paste0(expression(y[2]), "~`=`~")),
eq.with.lhs = paste0(expression(y[ind]), "~`=`~"),
parse = TRUE) +
ylab(NULL)
我不太确定是否可以通过 ggpmisc
做到这一点,但是您可以在构建绘图后更改数据,如下所示:
library(tidyverse)
library(ggpmisc)
data <- data.frame(x = rnorm(20)) %>%
mutate(y1 = 1.2*x + rnorm(20, sd=0.2),
y2 = 0.9*x + rnorm(20, sd=0.3)) %>%
gather(value = value, key = key, -x)
p <- ggplot(data, aes(x = x, y = value)) +
geom_point(aes(shape = key, colour = key)) +
stat_poly_eq(aes(label = ..eq.label.., colour = key),
formula = y ~ poly(x, 1, raw = TRUE),
eq.x.rhs = "x",
eq.with.lhs = paste0(expression(y), "~`=`~"),
parse = TRUE) +
ylab(NULL)
temp <- ggplot_build(p)
temp$data[[2]]$label <- temp$data[[2]]$label %>%
fct_relabel(~ str_replace(.x, "y", paste0("y[", 1:2, "]")))
grid::grid.newpage()
grid::grid.draw(ggplot_gtable(temp))