How to Create a Bar Chart Using ggplot2 in R - dummies

How to Create a Bar Chart Using ggplot2 in R

By Andrie de Vries, Joris Meys

To make a bar chart with ggplot2 in R, you use the geom_bar() function. However, note that the default stat is stat_bin(), which is used to cut your data into bins. Thus, the default behavior of geom_bar() is to create a histogram.

For example, to create a histogram of the depth of earthquakes in the quakes dataset, you do the following:

> ggplot(quakes, aes(x=depth)) + geom_bar()
> ggplot(quakes, aes(x=depth)) + geom_bar(binwidth=50)

Notice that your mapping defines only the x-axis variable (in this case, quakes$depth). A useful argument to geom_bar() is binwidth, which controls the size of the bins that your data is cut into.

So, if geom_bar() makes a histogram by default, how do you make a bar chart? The answer is that you first have to aggregate your data, and then specify the argument stat=”identity” in your call to geom_bar().

In the next example, you use aggregate() to calculate the number of quakes at different depth strata:

> quakes.agg <- aggregate(mag ~ round(depth, -1), data=quakes,
+         FUN=length)
> names(quakes.agg) <- c("depth", "mag")

Now you can plot the object quakes.agg with geom_bar(stat=”identity”):

> ggplot(quakes.agg, aes(x=depth, y=mag)) +
+   geom_bar(stat="identity")

In summary, you can use geom_bar() to create a histogram and let ggplot2 summarize your data, or you can pre-summarize your data and then use stat=”identity” to plot a bar chart.