geom_smooth() questions

Hi there! I have some questions when using geom_smooth(). Here is a reproducible example:

ggplot(mtcars,aes(y=mpg,x=drat,color=as.factor(vs)))+stat_summary(fun='mean')+geom_smooth(method='lm',se=F)+labs(color='vs')

My questions are:

  1. how to make the points go away? I only want to keep the lines.
  2. how to make one of the lines a dot line?
  3. how to make the value of vs shown as 'high group' and 'low group'?
  4. how to make the grey background go away?
  5. is it possible to put the geom_smooth() into the stat_summary() with the method='lm'?

Thank you very much.

Hi, here are some code snippets for each of your questions.

library(tidyverse)

## 1. version without points - sim,ply remove layer with points
ggplot(mtcars, 
       aes(x = drat, y = mpg, 
       color = as.factor(vs)
  )) + 
  #stat_summary(fun = 'mean') + 
  geom_smooth(method = 'lm', se = F) + 
  labs(color = 'vs')
#> `geom_smooth()` using formula 'y ~ x'


## 2. one line as dotted
ggplot(mtcars, 
       aes(x = drat, y = mpg, 
           color = as.factor(vs),
           linetype = as.factor(vs)
       )) + 
  geom_smooth(method = 'lm', se = F) +
  labs(color = 'vs', linetype = 'vs') + 
  scale_linetype_manual(values = c(1, 3))
#> `geom_smooth()` using formula 'y ~ x'


## 3. rename legend key
## simplest way is to create those before handing it to ggplot
mtcars %>% 
  mutate(group = if_else(vs == 0, "low", "high")) %>% 
  ## to keep same order...
  mutate(group = factor(group, levels = c("low", "high"))) %>% 
  ggplot(aes(x = drat, y = mpg, 
         color = group,
         linetype = group
    )) + 
    geom_smooth(method = 'lm', se = F) +
    labs(color = 'vs', linetype = 'vs') + 
    scale_linetype_manual(values = c(1, 3))
#> `geom_smooth()` using formula 'y ~ x'


## 4. remove grey background
mtcars %>% 
  mutate(
    group = if_else(vs == 0, "low", "high"),
    group = factor(group, levels = c("low", "high"))
  ) %>% 
  ggplot(aes(x = drat, y = mpg, 
             color = group,
             linetype = group
  )) + 
  geom_smooth(method = 'lm', se = F) +
  labs(color = 'vs', linetype = 'vs') + 
  scale_linetype_manual(values = c(1, 3)) +
  theme_minimal() ## you can try other themes
#> `geom_smooth()` using formula 'y ~ x'

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

With regard to the last question: Why do you want to do that? What would be the benefit? There is a stat_smooth() function but note that the stat_* and geom_* functions are generally exchangeable.

2 Likes

hi @Z3tt Thank you very much for the quick response. I just started to learn ggplot2, I am trying to get my syntax work in the same way so that I can understand the logic of ggplot2 better. So far I used ggplot + stat_summary way to write my syntax, and use the geom='' inside of stat_summary for the geom. I intend to keep this way of thinking. And I am not using tidyverse, so do you know of a base R way to do these things? I know tidyverse is quite popular and nice, I am just not there yet, still trying to figure out the basic ways in R and ggplot2, preferably without changing the data.frame:) Thank you very much.

ggplot is a part of the tidyverse.
For a structured education you can use

from https://r4ds.had.co.nz

The thing is you can basically convert most geoms to stats and vice versa but not all. Beside that, stat und geom have specific meaning and I suggest to use them accordingly. See also Function reference • ggplot2. In your example, you can also use stat_smooth() instead of geom_smooth() (there is in general some discussion which makes more sense here - but the result is the same as expected). Hoiwever, there is no way as far as I know to put it in your stat_summary() call.

library(tidyverse)

## 5. use stat_* instead geom_*
mtcars %>% 
  mutate(
    group = if_else(vs == 0, "low", "high"),
    group = factor(group, levels = c("low", "high"))
  ) %>% 
  ggplot(aes(x = drat, y = mpg, 
             color = group,
             linetype = group
    )) + 
    stat_smooth(method = 'lm', se = F) +
    labs(color = 'vs', linetype = 'vs') + 
    scale_linetype_manual(values = c(1, 3)) +
    theme_minimal() ## you can try other themes
#> `geom_smooth()` using formula 'y ~ x'

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

It is a good idea to learn base R first. But, as for the plotting part, it makes no sence in my opinion to do it the base R way first and then learn ggplot2 since both are quite different.

Actually, it is possible to overwrite the legend labels in different ways, also without transforming your data frame beforehand. Actually I missed the simplest way without any transformation:

library(tidyverse)

ggplot(mtcars, 
       aes(x = drat, y = mpg, 
           color = as.factor(vs),
           linetype = as.factor(vs))) + 
  geom_smooth(method = 'lm', se = F) + 
  scale_color_manual(name = "vs", values = c("black", "red"), labels = c("low", "high")) +
  scale_linetype_manual(name = "vs", values = c(1, 3), labels = c("low", "high"))
#> `geom_smooth()` using formula 'y ~ x'

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

If my answer solved the issue for you, please mark the topic as solved too.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.