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 plots it.

 

 

 

 

 

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

 

 

Now, let’s change the color of the 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 the legend and add axis names 2/ Make a horizontal barplot with ggplot2 3/ Custom bar width


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

[mediatagger]


Leave a Reply

avatar
  Subscribe  
Notify of