如何将散点图的椭圆和回归线组合在一起,以及如何在同一图上显示 R 方块?
How to combine ellipse and regression line together of a scatter plot and also how to show R square on the same plot?
想要将散点图的回归线与椭圆结合,以及如何在单个图中显示 R 平方值。还想删除 plot.Following 代码和数据 link 的中心蓝点,我正在使用这个
input data link 想将这两个图合并到单个图像中,图像中的 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
中绘制绘图会更容易(请注意 ggscatter
只是 ggplot
的包装)。您可以从 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")
想要将散点图的回归线与椭圆结合,以及如何在单个图中显示 R 平方值。还想删除 plot.Following 代码和数据 link 的中心蓝点,我正在使用这个 input data link 想将这两个图合并到单个图像中,图像中的 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
中绘制绘图会更容易(请注意 ggscatter
只是 ggplot
的包装)。您可以从 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")