Share the Gallery !Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someone



This page describes how to draw barplots with R and the ggplot2 library.

Let’s start with the most basic barchart ever. We apply the method on the mtcars dataset which is native with R, using geom_bar().

Note that we apply this geom on a single discrete variable. By default ggplot2 counts the occurence of each level and plot it.






If you want to plot the relationship between a continuous variable with a discrete variable, you need to specify stat=”identity” when calling the barplot:



Now, let’s change the color of bars. There are 5 main ways to do so, all presented above and explained in the code below.

The 3 last examples show you how to: 1/ Remove legend and add axis names 2/ Make an horizontal barplot with ggplot2 3/ Custom the width of bars

Not what you are looking for ? Make a new search !

Leave a Reply

Be the First to Comment!

Notify of