Quantcast
Channel: Active questions tagged lm - Stack Overflow
Viewing all articles
Browse latest Browse all 124

Linear regression with geom_smooth() from ggplot2 with categorical variable on the x-axis

$
0
0

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:

enter image description here


Viewing all articles
Browse latest Browse all 124

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>