简体   繁体   中英

Adding a separate line of regression to ggplot in R

Assuming I have a data frame with the following column headings: Height, Weight, Gender.

I am using ggplot to create a scatter graph.

ggplot(df, aes(x = height, y = weight , col = gender)) +
 geom_point() +
 theme_classic() +
 geom_smooth(method = "lm", se = FALSE)

This plots two lines of regression one for male and female. I would like to add another regression for overall comparing height and weight. How do I do this?

You can add another geom_smooth with col set to a static label. This overrides the first aes(col = gender) argument, puts all observations back in one group and gives it the label you want to use:

library(ggplot2)

ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, col = Species)) +
  geom_point() +
  theme_classic() +
  geom_smooth(method = "lm", se = FALSE) +
  geom_smooth(aes(col = "Overall"), method = "lm", se = FALSE)
#> `geom_smooth()` using formula 'y ~ x'
#> `geom_smooth()` using formula 'y ~ x'

Created on 2020-12-11 by the reprex package (v0.3.0)

Edit: Following up questions below, this also works with scale_colour_lancet :

library(ggplot2)
library(dplyr)

iris %>% 
  modelr::add_predictions(model = lm(Sepal.Length ~ Sepal.Width + Species, 
                          data = iris)) %>% 
  ggplot(aes(x = Sepal.Width, y = Sepal.Length, col = Species)) +
  geom_point() +
  theme_classic() +
  geom_line(aes(y = pred)) +
  geom_smooth(aes(col = "Overall"), method = "lm", se = FALSE) +
  ggsci::scale_color_lancet()
#> `geom_smooth()` using formula 'y ~ x'

Created on 2020-12-12 by the reprex package (v0.3.0)

The geom_smooth used above does lm(y ~ x) for each grouping automatically, in this case giving the equivalent lines for lm(Sepal.Length ~ Sepal.Width*Species) . To get lm(Sepal.Length ~ Sepal.Width + Species) a simple way would be to use modelr::add_predictions() to create a pred variable for y values before passing into ggplot .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM