I try to plot a linear regression line to a scatterplot with an ordinal categorical variable on the x-axis. However, no line is plotted with my code (there is also no warning or error). Is it possible to plot a regression line for categorical x-axes? Probably I have to specify the order of the levels of group
? Setting the levels explicitly did not help.
# librarieslibrary(ggplot2)# dummy datadat <- data.frame(group = as.factor(c(rep("A", 10), rep("B", 10))), variable = c(rnorm(10, mean = 3), rnorm(10, mean = 12)))# specify order of levels explicitlydat$group <- factor(dat$group, levels = c("A", "B"))# plotggplot(dat, aes(x = group, y = variable)) + geom_point() + geom_smooth(method = "lm")
Output: