You can do this fairly easily by stepping back from the 'qplot' wrapper function and using the 'ggplot' and geometry functions directly.
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(aes(colour=factor(cyl))) +
geom_smooth(method="lm")
Step 1: Set your initial 'ggplot' settings. These are the settings that you want to be defaults for the geometry functions.
ggplot(mtcars, aes(x=wt, y=mpg))
In this case, we are using the 'mtcars' data for all geometries with 'wt' assigned to the x-axis and 'mpg' assigned to the y-axis. By specifying these at the beginning, we lessen the risk of messing something up when copy-pasting into the geometry functions.
Step 2: Draw the point geometry, using the factors of 'cyl' to color the points. This is what the original 'qplot' function was doing, but we're specifying it a little more explicitly.
geom_point(aes(colour=factor(cyl)))
Step 3: Draw the smoothed linear model. This is exactly what the OP wrote before, but now that the aesthetic of coloring is no longer part of the defaults, the model draws as intended.
geom_smooth(method="lm")
Chain it all together with the +
et voila!
For reference: You could just as easily do this by being explicit in each layer, like so:
ggplot() +
geom_point(data=mtcars, aes(x=wt, y=mpg, colour=factor(cyl))) +
geom_smooth(data=mtcars, method="lm", aes(x=wt, y=mpg))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…