可以将 stat_poly_eq() 添加到 plotly plot 中吗?
can stat_poly_eq() be added to plotly plot?
可以轻松地将 stat_poly_eq()
包含在 ggplot
中,但是有什么方法可以将其包含在 plotly
图中?
当我尝试使用 ggplotly
时,它不会呈现 stat_poly_eq()
。
或者在 plotly
的情况下我应该使用不同的函数吗?
您可以使用 ggplotly 更改包含 stat_poly_eq
的 ggplot。新的 plotly 对象仍然得到了多项式回归。
工作示例:
library(ggplot2)
library(ggpmisc)
library(plotly)
set.seed(4321)
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))
# give a name to a formula
formula <- y ~ poly(x, 3, raw = TRUE)
# no weights
p<-ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula, parse = TRUE)
ggplotly(p)
唯一的区别是,您没有得到 R^2
的文本框。我们对此无能为力,因为它尚未实现(11.2020),如错误消息所示。
如果这仍然不适合你。您可以手动包含回归并将其添加到 plotly
。这取自现有的解决方案。检查 here 是否给予学分。这样修改起来更容易,你可以添加所有你需要的信息。
library(plotly)
library(dplyr)
data(cars, package = "datasets")
qfit1 <- lm(dist ~ poly(speed,2), data = cars)
cars %>%
plot_ly() %>%
add_lines(x = ~speed, y = fitted(qfit1)) %>%
add_trace(x=~speed, y=~dist)
为您的 R 平方添加文本可以是:
add_annotations(text= sprintf("R^2: %f", summary(qfit1)[8]), showarrow=FALSE, xref="paper", yref="paper", x=0.05,y=0.9)
有关更多选项和自定义,请查看文档 add_annotaions。
可以轻松地将 stat_poly_eq()
包含在 ggplot
中,但是有什么方法可以将其包含在 plotly
图中?
当我尝试使用 ggplotly
时,它不会呈现 stat_poly_eq()
。
或者在 plotly
的情况下我应该使用不同的函数吗?
您可以使用 ggplotly 更改包含 stat_poly_eq
的 ggplot。新的 plotly 对象仍然得到了多项式回归。
工作示例:
library(ggplot2)
library(ggpmisc)
library(plotly)
set.seed(4321)
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))
# give a name to a formula
formula <- y ~ poly(x, 3, raw = TRUE)
# no weights
p<-ggplot(my.data, aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", formula = formula) +
stat_poly_eq(formula = formula, parse = TRUE)
ggplotly(p)
唯一的区别是,您没有得到 R^2
的文本框。我们对此无能为力,因为它尚未实现(11.2020),如错误消息所示。
如果这仍然不适合你。您可以手动包含回归并将其添加到 plotly
。这取自现有的解决方案。检查 here 是否给予学分。这样修改起来更容易,你可以添加所有你需要的信息。
library(plotly)
library(dplyr)
data(cars, package = "datasets")
qfit1 <- lm(dist ~ poly(speed,2), data = cars)
cars %>%
plot_ly() %>%
add_lines(x = ~speed, y = fitted(qfit1)) %>%
add_trace(x=~speed, y=~dist)
为您的 R 平方添加文本可以是:
add_annotations(text= sprintf("R^2: %f", summary(qfit1)[8]), showarrow=FALSE, xref="paper", yref="paper", x=0.05,y=0.9)
有关更多选项和自定义,请查看文档 add_annotaions。