I am plotting multiple regression lines separated by a factor count.
- I need all of the lines to be the same colour and transparency (eg. color = red, alpha = 0.5)
- Draw a mean for all the lines in the graph e.g. black, dashed line)
I tried adding the color alpha parameters to the geom_line() but it produced a single line. Any suggestions?
#CODE to produce above graph
count <- c(1,2,3,1,2,3,1,2,3 1,2,3,1,2,3)
xcount <- rnorm(15,3,1)
ycount <- rnorm(15,5,1))
df <- data.frame(count, xcount, ycount)
ggplot(aes(x = xcount, y = ycount, color = factor(count)), data = df)
geom_smooth(method = "lm", fill = NA)
theme(panel.background = element_blank())
ylab("Y")
xlab ("X") theme(legend.position="none")
CodePudding user response:
Move your group aesthetics out of the first ggplot declaration, then use in the first geom_smooth your grouping to create the 3 red lines. Add another geom_smooth without the grouping to create the line on all values (average)
ggplot(aes(x = xcount, y = ycount), data = df)
geom_smooth(aes(group = factor(count)), method = "lm", fill = NA, color = "red")
geom_smooth(method = "lm", fill = NA, color = "black", linetype = "longdash")
Note that in the above alpha = 0.5 is removed as it is not supported in geom_smooth, to achieve that as well you can use stat_smooth instead like this so your red lines will have the desired transparency.
ggplot(aes(x = xcount, y = ycount), data = df)
stat_smooth(aes(group = factor(count)), geom = "line", method = "lm", se = FALSE, color = "red", alpha=0.5)
stat_smooth(geom = "line", method = "lm", se = FALSE, color = "black", linetype = "longdash")
CodePudding user response:
For the single color and alpha, just pull the dimension you're specifying outside of the aes() call. Anything outside of aes will be applied to every line. The trick is also having the groups specified to avoid getting a single line.
ggplot(aes(x = xcount, y = ycount, group = factor(count), data = df)
geom_smooth(method = "lm", fill = NA, color = 'red', alpha = 0.5)
As for an average line, the easiest way to do that is pre-compute the mean right before your plot. Assuming you place the mean in a variable called mu, you would just need to add the following:
geom_hline(yintercept = mu, linetype = 'longdash')

