繁体   English   中英

如何将散点图 plot 的椭圆和回归线组合在一起,以及如何在同一个 plot 上显示 R 正方形?

[英]How to combine ellipse and regression line together of a scatter plot and also how to show R square on the same plot?

想要将散点 plot 的回归线与椭圆组合,以及如何在单个 plot 中显示 R 平方值。还想删除 plot 的中心蓝点。以下代码和数据链接,我正在使用此输入数据链接想将这两个 plot 与图像中的 R 平方值结合起来

我使用代码得到的输出图像样本回归图

library(readxl)
library(tidyverse)
library(ggplot2)
library(ggpubr)
fir <- read_excel("D:/work/Book1.xlsx", sheet = "tan")
input<- as.data.frame(fir)
data_na =na.omit(input)
head(data_na)
sp <- ggscatter(data_na, x = "v1", y = "v2",
            add = "reg.line",     # Add regressin line
            add.params = list(color = "blue", fill = "lightgray"))
sp 
sd<- dataEllipse(data_na$v1, data_na$v2, levels=c( 0,0.90))# add ellipse
sd 

want to combine/merge (as the data are same for both) this two plots in a single one and need R square value too?
   

我认为直接在ggplot中绘制 plot 会更容易(注意ggscatter只是ggplot的包装器)。 您可以从 dataEllipse 中获取dataEllipse作为 x、y 位置的矩阵,并将它们作为geom_path添加到散点图中以获得所需的结果:

library(ggplot2)
library(ggpubr)
library(car)

ellipse <- dataEllipse(data_na$v1, data_na$v2, 
                       levels = 0.90,
                       draw = FALSE)

ggplot(setNames(data_na, c("x", "y")), aes(x, y)) + 
  geom_point() +
  geom_smooth(formula = y ~ x, method = "lm", se = FALSE, color = "blue") +
  geom_path(data = as.data.frame(ellipse), color = "blue") +
  theme_pubr()


数据

data_na  <- structure(list(v1 = c(176L, 180L, 190L, 118L, 121L, 263L, 202L, 
318L, 282L, 352L, 238L, 325L, 284L, 337L, 368L, 499L, 691L, 374L, 
508L, 371L, 403L, 296L, 244L, 548L, 330L, 630L, 113L, 297L, 219L, 
531L, 454L, 407L, 426L, 454L, 273L, 201L, 318L, 281L, 270L), 
    v2 = c(0.2593678096, 0.4189655053, 0.7775976761, 0.8278505446, 
    0.2388620625, 1.269976995, 0.3011494091, 0.2621149345, 0.3562413688, 
    1.408643646, 1.125793077, 0.1436436738, 0.1076321802, 0.2567930962, 
    0.4841953877, 0.3309999928, 1.340839047, 1.036103417, 0.1997356259, 
    0.3990804449, 0.3864942424, 0.6249310181, 1.36426432, 1.038793083, 
    0.9374712352, 1.242781572, 3.52103437, 3.434022908, 1.356563177, 
    0.4454942428, 0.1573907999, 1.021793082, 0.5268965439, 0.4415632055, 
    1.229494218, 1.432643646, 0.2451838993, 0.4092183845, 1.532045935
    )), row.names = c(NA, 39L), class = "data.frame")

暂无
暂无

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

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