How to Store and Calculate Values in R
How to Create Subsets of Your Data in R
How to Calculate Data Proportions and Find the Center in R

How to Create a Bar Chart Using ggplot2 in R

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.

image0.jpg
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Track Data Correlations in R
How to Change Lattice Plot Options in R
How to Combine Logical Statements in R
How to Use Logical Vectors as Indices in R
How to Look at Data Margins and Proportions in R
Advertisement

Inside Dummies.com