Create a Scatterplot with Regression Lines for Two Groups in R (Without ggplot)
Assuming you have a data frame called 'mydata' with a variable called 'Awareness' and a grouping variable called 'Group', you can use the base R 'plot()' function and 'abline()' function to create a scatterplot with two regression lines:
# Create scatterplot
plot(x = mydata$Group, y = mydata$Awareness, pch = 16,
xlab = "Group", ylab = "Awareness", main = "Scatterplot of Awareness by Group")
# Add regression lines
abline(lm(Awareness ~ Group, data = subset(mydata, Group == 1)), col = "red")
abline(lm(Awareness ~ Group, data = subset(mydata, Group == 2)), col = "blue")
This code uses the 'lm()' function to fit a linear regression model for each group separately, and then adds the resulting regression lines to the plot using 'abline()'. The 'subset()' function is used to subset the data by group before fitting the regression models. The 'pch' argument in the 'plot()' function sets the symbol used for the points in the scatterplot (in this case, filled circles).
原文地址: https://www.cveoy.top/t/topic/kAw8 著作权归作者所有。请勿转载和采集!