簡體   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