Skip to main content

View related sites

  • Thought leadership
  • Media
  • Newsletter
  • FAQ
  • Events
  • Contact us
  • Facebook
  • Twitter
  • LinkedIn

Cambridge Mathematics

  • About us
  • The Cambridge Mathematics Framework
  • Our services
  • For teachers and practitioners
  • Blogs
  • Research
  • Thought leadership
  • Media
  • Newsletter
  • FAQ
  • Contact us

Making more interesting charts in R

  • Cambridge Mathematics
  • Mathematical Salad
  • Making more interesting charts in R
  • Blogs
  • Teaching maths
  • Considering the research
  • Interviews and intersections
  • Mining for maths
  • Events and take-aways
  • Policy and big ideas

Making more interesting charts in R

by Darren Macey, 01 June 2017
Electronic barcharts and line graphs

The first blog on using R in the classroom finished with something of a challenge: could you interpret what effect the various parameters in the code below had on your chart? 

If you tried entering the code, a much more interesting chart is generated. 

DD R2 internal 1

R works by using set functions with parameters (called arguments) added to customise the appearance. Our bar chart is very simple, but the flexibility of the R language means sophisticated representations of multivariate data are possible. Each function has a range of parameters that can be added, with any that are not directly referenced automatically set to a default by the software. You only need to worry about the things you want to change. 

The parameters explained:

main = "Favourite Colours"    
sets the title of the chart
xlab = "colours"    
sets the label on the x axis
ylab = "Frequency"
sets the label on the y axis
col = c("blue","green","red")

sets the colours of the bars in the order listed. c() is an extra function that combines the values into a list, necessary to make this parameter work.

R has a set of pre-defined colours, but it is possible to put RGB hex codes between the quotation marks for finer control of the colour palette


To find out more about the various parameters that can be tweaked, R has an inbuilt help() function, which opens a web page for any function in the parenthesis; e.g. help(plot). At first glance these pages can be daunting, but as you become more familiar with using R you will begin to recognise standard structures and common arguments. 

Frequency charts for discrete data 

If you try to use discrete data with the functions we currently have available, the chart produced will look weird and will certainly not be a frequency chart. This is due to how plot() tries to process univariate, numerical data. A new function is needed. 

First, record some numerical data in a spreadsheet column, save it as a csv file called “numbers” and import into R as we did previously. 

The data is now recorded as a list, but it needs to be converted into a frequency table in order to work with plot(). To do this, use the command below. 

I hope that by now you are beginning to be able to interpret the commands as you read them. “frequencyTable” is a new object in which the processed data will be stored, the assignment operator is sending the output from the table() function to this new object (frequency table), which takes the list and converts it into a table with a count of each unique value. 

It’s now possible to generate a frequency chart by using the plot() function on the new “frequencyTable” object, but this will create a line graph. To get a bar chart, use the barplot() function instead. 

DD R2 second internal image

As with plot() you can add parameters to customise the appearance of the bar plot. Have a go with your own data and tweet your creations to @Cambridgemaths

Useful links

  • Home
  • About us
  • The Cambridge Mathematics Framework
  • Services
  • For teachers & practitioners
  • Blogs
  • Research
  • Thought leadership
  • Media
  • Newsletter
  • FAQ
  • Contact us

About Cambridge Mathematics

Cambridge Mathematics is committed to championing and securing a world class mathematics education for all students from 3 – 19 years old, applicable to both national and international contexts and based on evidence from research and practice.

  • Cambridge Mathematics

View Related Sites

  • University of Cambridge
  • Cambridge University Press & Assessment
  • Faculty of Mathematics
  • Faculty of Education

© Cambridge University Press & Assessment 2025

  • Sitemap
  • Accessibility and Standards
  • Data Protection
  • Use of Cookies
  • Statement on Modern Slavery
  • Terms and Conditions
Back to top
We use cookies. By clicking any link on this page you are giving your consent for us to set cookies