I am trying to add shape to a regression model. Here is the example:
library(ggpubr)
data(iris)
iris$ran <- as.factor(rep(c(1:2), each = 75))
fit <- lm(Sepal.Length ~ Petal.Width+Species+ran, data = iris)
ggplot(fit$model, aes_string(x = names(fit$model)[2], y = names(fit$model)[1],
color=names(fit$model)[3], shape=names(fit$model)[4])) +
geom_point() +
geom_smooth(aes_string(fill = names(fit$model)[3], color = names(fit$model)[3]),
method = "lm", col= "red", fullrange = TRUE) +
labs(x=expression(paste("Petal Width")),
y=expression(paste("Sepal Length")),
caption = paste("R2 =",signif(summary(fit)$r.squared, 2),
"\tAdj R2 =",signif(summary(fit)$adj.r.squared, 2),
"\tIntercept =",signif(fit$coef[[1]],2 ),
"\tSlope =",signif(fit$coef[[2]], 2),
"\tP =",signif(summary(fit)$coef[2,4], 2)))+
theme_classic2(base_size = 14)
I am getting a plot with four linear lines for each of the factor. I rather want linear regression lines only for "Species" but different shapes for "ran"(without adding regression lines for "ran" to the plot).
Also, I am also intending to change "R2" to R^2 which I am unable to do using current script and change the legend for ran as "Random" - "Factor1" and "Factor2".
Thank you in advance for your help.