ts3n_mt
ts3n_mt

Reputation: 67

ggplot2 get rid of confidence interval

I am using the "ggplot2" package in R.

ggplot2::mpg


A tibble: 234 x 11
   manufacturer model      displ  year   cyl trans      drv     cty   hwy fl    class  
   <chr>        <chr>      <dbl> <int> <int> <chr>      <chr> <int> <int> <chr> <chr>  
 1 audi         a4           1.8  1999     4 auto(l5)   f        18    29 p     compact
 2 audi         a4           1.8  1999     4 manual(m5) f        21    29 p     compact
 3 audi         a4           2    2008     4 manual(m6) f        20    31 p     compact
 4 audi         a4           2    2008     4 auto(av)   f        21    30 p     compact
 5 audi         a4           2.8  1999     6 auto(l5)   f        16    26 p     compact
 6 audi         a4           2.8  1999     6 manual(m5) f        18    26 p     compact
 7 audi         a4           3.1  2008     6 auto(av)   f        18    27 p     compact
 8 audi         a4 quattro   1.8  1999     4 manual(m5) 4        18    26 p     compact
 9 audi         a4 quattro   1.8  1999     4 auto(l5)   4        16    25 p     compact
10 audi         a4 quattro   2    2008     4 manual(m6) 4        20    28 p     compact
# … with 224 more rows

I am using the mpg data in R. I am having trouble making a scatterplot and removing the confidence interval from the regression line I am adding to my scatterplot.

When I use the see =FALSE, I get Warning message:
Ignoring unknown aesthetics: se

Here is my code, and the attached picture is an example of how I am trying to visualize the data.I have the points but cant get the regression line figured out with the "se" argument. I have used se =FALSE, method=lm etc. Thanks

ggplot(data=mpg)+
  geom_point(mapping = aes(x=displ, y=hwy))+
  geom_smooth(mapping=aes(x=displ, y=hwy, color=drv, se=FALSE))

https://i.sstatic.net/pMqqJ.jpg

Upvotes: 1

Views: 1037

Answers (2)

KoenV
KoenV

Reputation: 4283

If you want to suppress the confidence interval visualization, the code to add is se = FALSE indeed. This is however not an "aesthetic" and thus should not be within the aesthetics' definition. An alternative solution is the one provided by @Aite97.

The following code will do the trick.

ggplot(mpg) +
  geom_point(aes(x = displ, y = hwy))+
  geom_smooth(aes(x = displ, y = hwy, color = drv), se = FALSE)

This yields the following graph:

enter image description here

Upvotes: 2

Aite97
Aite97

Reputation: 165

a solution would be to replace se = FALSE with level = 0. Level is the confidence level, and setting it to 0 fixes the problem:

ggplot(data=mpg)+
  geom_point(mapping = aes(x=displ, y=hwy))+
  geom_smooth(mapping=aes(x=displ, y=hwy, color=drv), level = 0)

Upvotes: 2

Related Questions