Reputation: 57
Want to combine a regression line of the scatter plot with the ellipse and also how to show R square value in a single plot. Also want to remove center blue dot of the plot.Following code and data link, I am using this input data link want to combine this two plot in single image with R square value in the image too
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?
Upvotes: 0
Views: 342
Reputation: 173803
I think it would be easier to draw your plot directly in ggplot
(note that ggscatter
is just a wrapper around ggplot
). You can get the output from dataEllipse
as a matrix of x, y positions and simply add these to a scatterplot as a geom_path
to get the desired result:
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
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")
Upvotes: 2