Some useful bar plots using R

[This article was first published on - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In this article I am trying to show how to produce bar plots using R. Many of my friends think SPSS is the most useful software for producing plots and they keep using it (some of them even use Excel!).
My goal is to show that R can do every type of graphs that other commercial softwares can do. In fact it does much better than the simple point and click packages, as R gives us much better control over our analysis.

The data of my concern is –

   sex income   district
female     21      dhaka
  male     11      dhaka
  male     43 chittagong
female     22      dhaka
  male     56    barisal
female     23    barisal
female     66      dhaka
  male     76      dhaka
female     11 chittagong
female     89      dhaka

This data is not a real data, completely created by me just to do experiments using R codes.

Now, I want to produce a bar plot where ‘sex’ would be the category axis and the clusters will represent mean and median ‘income’, i.e. I want to produce a plot that we produce in SPSS by the command-

/bar=mean(income) median(income) by sex.

So, at first I calculate mean and median ‘income’ for both male and female.


Then if I want to put the numbers represented by the bars above them,
the code will be-


And the plot is-plot1.png

Now, if I want to produce a more complex plot that is a bar plot that will show mean income for all the three districts separately for male and female, i.e. the plot we produce in SPSS by the command-

/bar=mean(income) by sex by district.

For the required summary statistics I used a package ‘Epi’ and with the following command produced a very useful summary table-


And the produced table is-

 district      female    male  
 barisal        23.00   56.00  
 chittagong     11.00   43.00  
 dhaka          49.50   43.50  

As we all know this statistics can also be found by a few lines of codes instead of ‘stat.table’, but I used it just to cut down some codes.
Then, I did the following commands-


And the plot is-

Hope this codes will be useful to those who really want to do every type of statistical work(including producing graphs) in R.


To leave a comment for the author, please follow the link and comment on their blog: - R. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)