简体   繁体   English

R plotly():将回归线添加到相关散点图

[英]R plotly(): Adding regression line to a correlation scatter plot

I would like to add the regression line to my correlation scatter plot.我想将回归线添加到我的相关散点图中。 Unfortunately this doesn't really work with plot_ly() .不幸的是,这不适用于plot_ly() I've already tried some solutions from other posts in this forum, but it doesn't work.我已经在本论坛的其他帖子中尝试了一些解决方案,但它不起作用。

My data frame looks like the following (only a smart part of it):我的数据框如下所示(只是其中的一部分):

数据框

My code for the plot and the actual plot-output look like the following:我的绘图代码和实际绘图输出如下所示:

CorrelationPlot <- plot_ly(data = df.dataCorrelation, x = ~df.dataCorrelation$prod1, 
                           y = ~df.dataCorrelation$prod2, type = 'scatter', mode = 'markers',
                           marker = list(size = 7, color = "#FF9999", line = list(color = "#CC0000", width = 2))) %>%
                    layout(title = "<b> Correlation Scatter Plot", xaxis = list(title = product1), 
                           yaxis = list(title = product2), showlegend = FALSE)

无线相关散点图

What I want to have is something like this:我想要的是这样的:

带线的相关散点图

which I have produced with the ggscatter() function:我用ggscatter()函数生成的:

library(ggpubr)
  ggscatter(df.dataCorrelation, x = "prod1", y = "prod2", color = "#CC0000", shape = 21, size = 2,
            add = "reg.line", add.params = list(color = "#CC0000", size = 2), conf.int = TRUE, 
            cor.coef = TRUE, cor.method = "pearson", xlab = product1, ylab = product2)
                  

HOW do I get the regression line with plot_ly() ??我如何使用plot_ly()获得回归线?

CODE EDITING:代码编辑:

CorrelationPlot <- plot_ly(data = df.dataCorrelation, x = ~df.dataCorrelation$prod1, 
                           y = ~df.dataCorrelation$prod2, type = 'scatter', mode = 'markers',
                           marker = list(size = 7, color = "#FF9999",
                             line = list(color = "#CC0000", width = 2))) %>%
                   add_trace(x = ~df.dataCorrelation$fitted_values, mode = "lines", type = 'scatter',
                             line = list(color = "black")) %>%
                   layout(title = "<b> Correlation Scatter Plot", xaxis = list(title = product1), 
                           yaxis = list(title = product2), showlegend = FALSE)
  

GIVES:给:

点

How do I get here a line for the regression line?我如何在这里得到一条回归线的线? ? ?

I don't think there's a ready function like ggscatter, most likely you have to do it manually, like first fitting the linear model and adding the values to the data.frame.我不认为有像 ggscatter 这样的现成函数,很可能你必须手动完成,比如首先拟合线性模型并将值添加到 data.frame。

I made a data.frame that's like your data:我做了一个 data.frame 就像你的数据:

set.seed(111)
df.dataCorrelation = data.frame(prod1=runif(50,20,60))
df.dataCorrelation$prod2 = df.dataCorrelation$prod1 + rnorm(50,10,5)

fit = lm(prod2 ~ prod1,data=df.dataCorrelation)
fitdata = data.frame(prod1=20:60)
prediction = predict(fit,fitdata,se.fit=TRUE)
fitdata$fitted = prediction$fit

The upper and lower bounds of the line are simply 1.96* standard error of prediction:这条线的上限和下限只是预测的 1.96* 标准误差:

fitdata$ymin = fitdata$fitted - 1.96*prediction$se.fit
fitdata$ymax = fitdata$fitted + 1.96*prediction$se.fit

We calculate correlation:我们计算相关性:

COR = cor.test(df.dataCorrelation$prod1,df.dataCorrelation$prod2)[c("estimate","p.value")]
COR_text = paste(c("R=","p="),signif(as.numeric(COR,3),3),collapse=" ")

And put it into plotly:并将其放入情节中:

library(plotly)

df.dataCorrelation %>%
plot_ly(x = ~prod1) %>%
add_markers(x=~prod1, y = ~prod2) %>%
add_trace(data=fitdata,x= ~prod1, y = ~fitted, 
mode = "lines",type="scatter",line=list(color="#8d93ab")) %>%
add_ribbons(data=fitdata, ymin = ~ ymin, ymax = ~ ymax,
line=list(color="#F1F3F8E6"),fillcolor ="#F1F3F880" ) %>%
layout(
    showlegend = F,
    annotations = list(x = 50, y = 50,
    text = COR_text,showarrow =FALSE)
)

在此处输入图片说明

Another option is using ggplotly as另一种选择是使用ggplotly作为

library(plotly)
ggplotly(
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length))+
  geom_point(color = "#CC0000", shape = 21, size = 2) +
  geom_smooth(method = 'lm') +
  annotate("text", label=paste0("R = ", round(with(iris, cor.test(Sepal.Length, Petal.Length))$estimate, 2),
                                ", p = ", with(iris, cor.test(Sepal.Length, Petal.Length))$p.value), 
x = min(iris$Sepal.Length) + 1, y = max(iris$Petal.Length) + 1, color="steelblue", size=5)+
  theme_classic()
)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM