-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScatter plot with Regression
25 lines (21 loc) · 1.13 KB
/
Scatter plot with Regression
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
## ----- Import library and data [mtcars]
library(ggpubr)
mtcars <- mtcars
head(mtcars)
##------ Scatter Plot with regression line ------
scatter_plot <- ggscatter(mtcars, x = 'wt', y = 'mpg',
xlab = 'Weight', ylab = 'mpg',
color = 'black', fill ='red',
shape = 21, size = 4,
title = 'Different mpg on car weight on mtcars',
subtitle = 'US 32 automobiles on 1973-1974',
caption = 'R = Correlation, p = p-value',
add = 'reg.line',
conf.int = TRUE,
add.params = list(color = 'blue',
fill = 'blue'))
scatter_plot + stat_cor(method = 'pearson', label.x = 4, label.y = 32, color = 'red', size = 4) +
theme_gray() +
geom_text(x = 4.5, y = 27.5, label = 'R-square = 0.75\nIntercept = 6.05\nCoefficent = -0.14', col = 'red') +
theme(plot.title = element_text(size = 14, colour = 'red', face = 'bold'),
plot.caption = element_text(size = 12, colour = 'red', hjust = 0.5))