# Simple scatter plot with correlation coefficient and
# regression line
#::::::::::::::::::::::::::::::::::::::::::::::::::::
ggscatter(mtcars, x = "wt", y = "mpg", add = "reg.line") +
stat_cor(label.x = 3, label.y = 34) +
stat_regline_equation(label.x = 3, label.y = 32)
# Groupped scatter plot
#::::::::::::::::::::::::::::::::::::::::::::::::::::
ggscatter(
iris, x = "Sepal.Length", y = "Sepal.Width",
color = "Species", palette = "jco",
add = "reg.line"
) +
facet_wrap(~Species) +
stat_cor(label.y = 4.4) +
stat_regline_equation(label.y = 4.2)
# Polynomial equation
#::::::::::::::::::::::::::::::::::::::::::::::::::::
# Demo data
set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x, y, group = c("A", "B"),
y2 = y * c(0.5,2), block = c("a", "a", "b", "b"))
# Fit polynomial regression line and add labels
formula <- y ~ poly(x, 3, raw = TRUE)
p <- ggplot(my.data, aes(x, y2, color = group)) +
geom_point() +
stat_smooth(aes(fill = group, color = group), method = "lm", formula = formula) +
stat_regline_equation(
aes(label = paste(..eq.label.., ..adj.rr.label.., sep = "~~~~")),
formula = formula
) +
theme_bw()
ggpar(p, palette = "jco")
Run the code above in your browser using DataLab