• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Analysis Factor

The Analysis Factor

Statistical Consulting, Resources, and Statistics Workshops for Researchers

  • Home
  • About
    • Our Programs
    • Our Team
    • Our Core Values
    • Our Privacy Policy
    • Employment
    • Guest Instructors
  • Membership
    • Statistically Speaking Membership Program
    • Login
  • Workshops
    • Online Workshops
    • Login
  • Consulting
    • Statistical Consulting Services
    • Login
  • Free Webinars
  • Contact
  • Login

R Is Not So Hard! A Tutorial, Part 11: Creating Bar Charts

by guest 8 Comments

by David Lillis, Ph.D.

Let’s create a simple bar chart in R using the barplot() command, which is easy to use.

First, we set up a vector of numbers. Then we count them using the table() command, and then we plot them.

The table() command creates a simple table of counts of the elements in a data set.

H <- c(2,3,3,3,4,5,5,5,5,6)

Now we count the elements using the table() command, as follows:

counts <- table(H)
counts
H
2 3 4 5 6
1 3 1 4 1

Now we plot the counts.

barplot(counts)

image001

The horizontal axis records the values in your data set, while the vertical axis gives the counts of each value. You will see that the barplot() command does not perform the count directly, so we use the table() command first.

You can plot your data directly if we omit the table() command. Now, the height of the bars matches the values in the data set. This is a useful technique if your data are already in the form of counts or if you wish to plot the magnitudes of each element.

B <- c(3, 2, 25, 37, 22, 34, 19)
barplot(B, col="darkgreen")

image002

Here we have one bar for each element, and the height gives the value of the element.

Now, let’s create a more complex barplot using various arguments, some of which you have already met in previous blogs.

barplot(B, main="MY NEW BARPLOT", xlab="LETTERS", ylab="MY Y VALUES", names.arg=c("A","B","C","D","E","F","G"),
border="red", density=c(90, 70, 50, 40, 30, 20, 10))

image003

You have seen the following arguments before: main, xlab and ylab, but not the others. OK. What did the density parameter achieve? Try other values of the density parameter and see what you get. What does the command names.arg do?

OK. Let’s create a third, more complex, bar chart, based on several (four) variables. Let’s assume that you wish to plot the magnitudes of each value within each variable.

data <- structure(list(W= c(1L, 3L, 6L, 4L, 9L), X = c(2L, 5L,
4L, 5L, 12L), Y = c(4L, 4L, 6L, 6L, 16L), Z = c(3L, 5L,
6L, 7L, 6L)), .Names = c("W", "X", "Y", "Z"), class = "data.frame", row.names = c(NA, -5L))
attach(data)
print(data)

W X Y Z
1 1 2 4 3
2 3 5 4 5
3 6 4 6 6
4 4 5 6 7
5 9 12 16 6

Now we wish to create side-by-side bar charts for the four variables, W, X, Y and Z. First, we set up some colours, one for each of the five values within each variable.

colours <- c("red", "orange", "blue", "yellow", "green")

Now, plot a bar chart of the four variables, with adjacent bars, using the as.matrix() command and the argument beside = T.

barplot(as.matrix(data), main="My Barchart", ylab = "Numbers", cex.lab = 1.5, cex.main = 1.4, beside=TRUE, col=colours)

image004

What did the argument beside = TRUE achieve?

Now we create a legend at the top-left corner. To create a legend without a frame, include: bty=”n”. The bty argument controls legend borders.

legend("topleft", c("First","Second","Third","Fourth","Fifth"), cex=1.3, bty="n", fill=colours)

image005

We used the topleft argument to position the legend towards the top left of the chart. Other options include:

"bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right", "center".

Try these for yourselves.

That wasn’t so hard! In then next post in this series, we will look at further plotting techniques in R.

About the Author: David Lillis has taught R to many researchers and statisticians. His company, Sigma Statistics and Research Limited, provides both on-line instruction and face-to-face workshops on R, and coding services in R. David holds a doctorate in applied statistics.

Bookmark and Share

Tagged With: programming, R

Related Posts

  • R Is Not So Hard! A Tutorial, Part 9: Sub-setting
  • The Advantages of RStudio
  • What Really Makes R So Hard to Learn?
  • R is Not So Hard! A Tutorial, Part 22: Creating and Customizing Scatter Plots

Reader Interactions

Comments

  1. Morine Akoth says

    February 21, 2019 at 12:13 pm

    Thank you for your clear explanation. I really appreciate.

    Reply
  2. MADHUBALA says

    October 1, 2018 at 5:54 am

    Thanks for explaining in simple way.

    Reply
  3. andres says

    June 5, 2017 at 9:08 pm

    Hi, my name is andres I am working in my undergraduate thesis and I need to split categories within month for each year that I have.I have 17 years. Can you help me with this, also I need to transform categories in frequency and get SD and confidence interval

    Reply
  4. RAJI, B.T says

    April 16, 2017 at 3:08 am

    Thanks for this post.
    I will appreciate if you can drop another on percentage component bar chart.
    kudos to you for this work.

    Reply
  5. Luís says

    November 9, 2016 at 12:08 pm

    Thank you for this informative posts. Do you happen to have a similar tutorial with ggplot2?

    Cheers.

    Reply
  6. Neo says

    September 15, 2016 at 10:28 am

    Thanks for a very useful post.
    I was wondering if it is possible to annotate each bar with a name or an asteriks (for indicating statistically significant values) in a similar way – without using ggplot2..

    Reply
  7. Nodir says

    December 7, 2015 at 2:24 am

    Thanks for the post; very useful. It would be great if you could include an example to plot numbers on top of bars.

    Reply
  8. rosario pacheco marin says

    June 11, 2015 at 5:40 pm

    Hola Buen dia
    Antes que nada felicitaciones y agradecimientos por su pagina. En mi caso fue de gran ayuda gracias por compartir sus conocmientos.
    Quisiera ver se me pudieran ayudar, como poder graficar dos variables por dato pero asu vez cada variable contiene dos datos
    Haber si me logro explicar
    Dato1 dato 2 etc
    Mujer Hombre Mujer Hombre
    barra1 barra2 barra1 barra2 juntas
    divida divida dividida dividida
    en dos en dos en dos en dos
    blanco gris claro blanco gris claro
    negro gris fuerte negro gris fuerte

    es decir para cada dato tener dos barras (hombre, mujer juntas las barras)
    pero a su vez cada barra es decir la de Mujer divida en 2, y la de hombre divida en 2. la barra de mujeres que tenga los mismos colores en cada dato por ejemplo blanco y negro y la de hombres gris claro y gris fuerte.
    Perdon es que no se como enviarles una imagen.
    De antemano gracias por sus comentarios.
    Sin mas por el momento me despido enviandoles un cordial saludo.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Please note that, due to the large number of comments submitted, any questions on problems related to a personal study/project will not be answered. We suggest joining Statistically Speaking, where you have access to a private forum and more resources 24/7.

Primary Sidebar

This Month’s Statistically Speaking Live Training

  • January Member Training: A Gentle Introduction To Random Slopes In Multilevel Models

Upcoming Workshops

  • Logistic Regression for Binary, Ordinal, and Multinomial Outcomes (May 2021)
  • Introduction to Generalized Linear Mixed Models (May 2021)

Read Our Book



Data Analysis with SPSS
(4th Edition)

by Stephen Sweet and
Karen Grace-Martin

Statistical Resources by Topic

  • Fundamental Statistics
  • Effect Size Statistics, Power, and Sample Size Calculations
  • Analysis of Variance and Covariance
  • Linear Regression
  • Complex Surveys & Sampling
  • Count Regression Models
  • Logistic Regression
  • Missing Data
  • Mixed and Multilevel Models
  • Principal Component Analysis and Factor Analysis
  • Structural Equation Modeling
  • Survival Analysis and Event History Analysis
  • Data Analysis Practice and Skills
  • R
  • SPSS
  • Stata

Copyright © 2008–2021 The Analysis Factor, LLC. All rights reserved.
877-272-8096   Contact Us

The Analysis Factor uses cookies to ensure that we give you the best experience of our website. If you continue we assume that you consent to receive cookies on all websites from The Analysis Factor.
Continue Privacy Policy
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Non-necessary

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.