Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot geom_bar: meaning of aes(group = 1)

I am learning geom_bar on section 3.7 of r4ds.had.co.nz. I run a code like this:

library(ggplot2) ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1)) 

Then I have this plot: click here

The point is, if I exclude the "group = 1" part:

library(ggplot2) ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop..)) 

The plot will be wrong, like this

But if I replace group = 1 by group = 2 or group = "x", the plot still looks correct. So I don't quite understand the meaning of group = 1 here and how to use it.

like image 310
Novembrain Avatar asked Oct 05 '16 16:10

Novembrain


People also ask

What is AES group in Ggplot?

In the right figure, aesthetic mapping is included in ggplot(..., aes(..., color = factor(year)) . It displays data points of different years with different colors as expected. It also further split each drv group into factor(year) subgroups.

What does stat mean in Geom_bar?

By default, geom_bar uses stat="count" which makes the height of the bar proportion to the number of cases in each group (or if the weight aethetic is supplied, the sum of the weights).

What does stat identity mean in Ggplot?

In the above example, we've overridden the default count value by specifying stat = "identity" . This indicates that R should use the y-value given in the ggplot() function. Notice that bar graphs use the fill argument instead of the color argument to color-code each cut category.

What does Geom_col do in R?

geom_col makes the height of the bar from the values in dataset.

How do I plot means and error bars in ggplot2?

You want to plot means and error bars for a dataset. To make graphs with ggplot2, the data must be in a data frame, and in “long” (as opposed to wide) format. If your data needs to be restructured, see this page for more information. The examples below will the ToothGrowth dataset.

How does ggplot2 create groups?

There are two ways in which ggplot2 creates groups implicitly: If x or y are categorical variables, the rows with the same level form a group. If aesthetic mapping, such as color, shape, and fill, map to categorical variables, they subset the data into groups.

How do I use the group aesthetic in a plot?

The group aesthetic is by default set to the interaction of all discrete variables in the plot. This choice often partitions the data correctly, but when it does not, or when no discrete variable is used in the plot, you will need to explicitly define the grouping structure by mapping group to a variable that has a different value for each group.

What is aesthetic mapping in ggplot2?

Each group has its own boxplot. In the right figure, aesthetic mapping is included in ggplot (..., aes (..., color = factor (year)). It displays data points of different years with different colors as expected. It also further split each drv group into factor (year) subgroups.


2 Answers

Group will help the plot to look at the specific rows that contain the specific cut and the proportion is found with respect to the whole database as in proportion of an ideal cut in the whole dataset.

If group is not used, the proportion is calculated with respect to the data that contains that field and is ultimately going to be 100% in any case. For instance, The proportion of an ideal cut in the ideal cut specific data will be 1.

like image 25
tejas lad Avatar answered Oct 23 '22 17:10

tejas lad


group="whatever" is a "dummy" grouping to override the default behavior, which (here) is to group by cut and in general is to group by the x variable. The default for geom_bar is to group by the x variable in order to separately count the number of rows in each level of the x variable. For example, here, the default would be for geom_bar to return the number of rows with cut equal to "Fair", "Good", etc.

However, if we want proportions, then we need to consider all levels of cut together. In the second plot, the data are first grouped by cut, so each level of cut is considered separately. The proportion of Fair in Fair is 100%, as is the proportion of Good in Good, etc. group=1 (or group="x", etc.) prevents this, so that the proportions of each level of cut will be relative to all levels of cut.

like image 160
eipi10 Avatar answered Oct 23 '22 17:10

eipi10