r/R_Programming • • Jan 26 '18

Ggplot2: group in aes()

Hello there,

currently trying to figure out how to work with ggplot2.

I'm stuck at Data Camp's second tutorial (2-2) on ggplot (just so anyone knows what I am dealing with).

Have this code snippet:

ggplot(mtcars, aes(x = wt, y = mpg, col = factor(cyl))) +
  geom_point() +
  stat_smooth(method = "lm", se = F, aes(group= 1))

What does group in aesthetic actually do? I know that method="lm" gives me a straight line and se=F removes the shading of the line (does the shading represent standard derivation?)

DC's ggplot 2-1 tutorial was easy but 2-2 is unclear as hell.

3 Upvotes

3 comments sorted by

View all comments

Show parent comment

2

u/[deleted] Jan 26 '18

So what we do is pretty much we "factor something" by its categorial variable (say, for instance, "horsepower as a group" - each with the same horsepower is displayed by one line.

If that is true, how is that line made? I mean, even though you may group statistical units that have a common realisation of a variable , they may differ in other variables. How is that line made? Is the mean calculated for (here wt and mpg) ?

Also, how does group = 1 work? Is the first categorial variable in the data frame referred to by 1?

1

u/Darwinmate Feb 02 '18

I think, but I can't find any documentation anywhere, that group=1 is actually overriding the grouping variable, so smooth line is proportion to all data. You will get the same effect if you use group="PENIS" or group=2

Here is where I found the answer: https://stackoverflow.com/questions/39878813/r-ggplot-geom-bar-meaning-of-aesgroup-1

BTW, group="PENIS" does give the same result as group=1. If you remove aes(group=1) you will see the result of the standard behaviour.