Skip to content

Instantly share code, notes, and snippets.

@juanchiem
Last active July 2, 2020 15:47
Show Gist options
  • Save juanchiem/39b570ff27febafa5874fdd720099534 to your computer and use it in GitHub Desktop.
Save juanchiem/39b570ff27febafa5874fdd720099534 to your computer and use it in GitHub Desktop.
library(ggpubr)
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")
# Opcion 2
ggplot(my.data, aes(x, y2)) +
geom_point() +
facet_wrap("group")+
stat_smooth(method = "lm", formula = formula) +
stat_regline_equation(formula = formula,
aes(label = paste(..eq.label.., ..adj.rr.label.., sep = "\n")),
label.y = Inf, vjust = 2,
) +
theme_bw(base_size = 12)
ggsave(file="myplot.tiff", device = "tiff", width = 6, height = 4, scale=1.2)
@juanchiem
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment