ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
To make a bar chart with {ggplot2}, add geom_bar() to the ggplot2 template. For example, the code below plots a bar chart of the cut variable in the diamonds dataset, which comes with {ggplot2}.
You should not supply a \(y\) aesthetic when you use geom_bar(); {ggplot2} will count how many times each \(x\) value appears in the data, and then display the counts on the \(y\) axis. So, for example, the plot above shows that over 20,000 diamonds in the data set had a value of Ideal.
You can compute this information manually with the count() function from the {dplyr} package.
geom_col()Sometimes, you may want to map the heights of the bars not to counts, but to a variable in the data set. To do this, use geom_col(), which is short for column.
geom_col() dataWhen you use geom_col(), your \(x\) and \(y\) values should have a one to one relationship, as they do in the pressure data set (i.e. each value of temperature is paired with a single value of pressure).
Use the code chunk below to plot the distribution of the color variable in the diamonds data set, which comes in the {ggplot2} package.

What is the most common type of cut in the diamonds dataset?
How many diamonds in the dataset had a Good cut?
Diagnose the error below and then fix the code chunk to make a plot.
count() and geom_col()Recreate the bar graph of color from exercise one, but this time first use count() to manually compute the heights of the bars. Then use geom_col() to plot the results as a bar graph. Does your graph look the same as in exercise one?