Select your country to view prices and course dates.

x

Recent Searches
Category

Microsoft Copilot Courses

Category

Microsoft Power BI

Category

AI for Business

Category

Microsoft Excel

Category

Professional Development

Category

Microsoft Office 365

Category

Excel Specialist

Category

Microsoft Project

Category

R Programming

Category

Python

Category

SQL

Category

Power Apps

Category

SharePoint

Category

Power Automate

Category

Microsoft Teams

Category

Microsoft Visio

Category

Microsoft PowerPoint

Category

Microsoft Word

Category

Microsoft Outlook

Category

Adobe InDesign Courses

Category

Adobe Photoshop Courses

Category

Adobe Illustrator Courses

Category

Adobe Premiere Pro Training

Category

Adobe After Effects Training

Category

Adobe Acrobat Courses

Category

Adobe Captivate Training

Category

Adobe Animate Training

Category

HTML Courses

Category

WordPress

Category

Canva Courses

Category

Microsoft Access

Category

Webinars

Course

Copilot for M365

Course

Power BI Beginner

Course

ChatGPT Beginner

Course

Excel Beginner

Course

Achieving Leadership & Success

Course

Microsoft 365 Beginner

Course

Financial Modelling

Course

Project Beginner

Course

R Programming Beginner

Course

Python Beginner

Course

SQL Beginner

Course

Power Apps Beginner

Course

SharePoint Beginner

Course

Teams Essentials

Course

Visio Essentials

Course

PowerPoint Level 1

Course

Word Beginner

Course

Microsoft Outlook Beginner

Course

InDesign Lite

Course

Photoshop Training Intro

Course

Illustrator Training Intro

Course

Premiere Training Intro

Course

After Effects Training Intro

Course

Acrobat Essentials

Course

Captivate Training

Course

Animate Training Intro

Course

HTML Training Intro

Course

WordPress Essentials

Course

Canva Beginners

Course

Microsoft Access Essentials

Course

Copilot for Word

Course

Power BI Intermediate

Course

Excel Intermediate

Course

Microsoft 365 Intermediate

Course

Analysis and Dashboards

Course

Project Intermediate

Course

R Programming Intermediate

Course

Python Intermediate

Course

SQL Intermediate

Course

Power Apps Intermediate

Course

SharePoint Intermediate

Course

PowerPoint Level 2

Course

Word Intermediate

Course

InDesign Training Intro

Course

Photoshop Lite

Course

Illustrator Training Advanced

Course

Premiere Basics Training

Course

Acrobat Forms

Course

Canva Intermediate

Course

Microsoft Access Advanced

Course

Copilot for Excel

Course

Power BI Advanced

Course

Excel Advanced

Course

Microsoft 365 Advanced

Course

Excel VBA

Course

Project Advanced

Course

R Programming Advanced

Course

Python Advanced

Course

SQL Advanced

Course

Power Apps Advanced

Course

SharePoint Advanced (Site Owner)

Course

Word Advanced

Course

InDesign Training Advanced

Course

Photoshop Training Advanced

Course

Advanced Premiere Training

Course

Advanced After Effects Training

Course

Canva Advanced

Course

Copilot for PowerPoint

Course

Power BI DAX

Course

Excel Expert

Course

Machine Learning in R

Course

SharePoint Advanced (Document Governance)

Course

InDesign Interactivity Training

Course

Copilot for Outlook and Teams

Course

InDesign Accessibility Training

Course

Power Automate Beginner

Course

Power Automate Intermediate

Course

Microsoft Outlook Advanced

Course

AI Prompting Fundamentals

Course

Power Automate Advanced

Course

Excel Tables and Pivot Tables

Course

Data Transformation with Power Query

Course

Excel Macro Mastery

Course

Power BI Desktop Advanced Reporting

Course

AI for Business Leaders and Managers

Course

Data Visualisation with Power BI Desktop

Course

Anger Management & Negotiation Skills

Course

Assertiveness & Confidence

Course

Building Resilience

Course

Coaching and Mentoring

Course

Communications

Course

Communications & Quality Client Service Training

Course

Critical Thinking and Problem Solving

Course

Cultural Diversity in the Workplace

Course

Embracing Change

Course

Growing Emotional Intelligence

Course

Minute Taking

Course

Persuasion and Negotiation Skills

Course

Presentation Skills and Public Speaking

Course

Practical Project Management

Course

Respect, Equity and Diversity (RED)

Course

Resumé Writing and Interview Skills

Course

Stress Management

Course

Team Leadership, Management and Development

Course

Time Management Intensive

Course

Train the Trainer

Course

Write Effective Business Documents

Course

Dealing with Difficult People

Course

Managing Difficult Conversations

Course

Managing the Virtual Workplace

How to make a Bar Graph in R

Brianna Higgins | Nov 24, 2022

Introduction              

R is a modern programming language, suitable for complex mathematical calculations, statistical analysis, creating graphs, and reporting. 

It was developed by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand. Both of its creators start with the letter "R", hence the name given to the programming language. 

This article is for programmers, analysts, and statisticians looking to get familiar with R and learn how to use it. We will discuss how easy it can be to code programs in R language. 

Before we do that, let's discuss some salient features of the R language:  

  • R is a simple yet effective programming language that includes conditional branching, loops, recursion, and I/O facilities. 
  • R supports multiple data types like arrays, vectors, lists, and matrices; for storing data in a way that is assistive in its usage. 
  • R has a rich set of operators, pre-defined functions, and numerous libraries for advanced calculations and analytics. 
  • R also supports libraries and graphical charting for data analysis and reporting. 

As such, it comes as no surprise that R is the world's most widely used statistical programming language. In this article, we will demonstrate R's capabilities by providing a few different program snippets. 

We will cover the steps to create a bar chart, to illustrate that R not only does calculations but also has the capability to create intuitive visuals. 

Creating Bar Charts in R

For creating bar charts, R has a function called barplot(). 

The syntax for the barplot()  function is:- 

barplot(H, xlab, ylab, main, names.arg, col, args.legend)

Definitions of these parameters are as follows:

  • H: This parameter is a vector or a matrix containing numeric values which are used in a bar chart.  

    The matrix data structure in R holds a collection of elements of the same data type (numeric, character, or logical) to be arranged into a fixed number of rows and columns, i.e., double dimensional array.  

     If you use a matrix here, R will plot a stacked bar chart. 

  • xlab: the x-axis label, this parameter is the label for the x-axis in the bar chart.
  • ylab: the y-axis label, this parameter is the label for y-axis in the bar chart.
  • main: This parameter is the title of the bar chart.
  • names.arg: This parameter is a vector of names or strings, appearing under each bar in the bar chart.
  • col: This parameter is used to give colors to the bars in the graph.  
  • args.legend: optional, determines where the legend will be placed and displayed. 

A simple Bar chart 

If I call the functions with the following parameters, 

barplot( c(12,28,3,41),  xlab="MyXLabel",  ylab="MyYLabel",  main="My First Bar Chart", col="blue"  ), the chart returned is shown below

basic bar chart in r

As you can see the chart is titled properly, the x and y axis have been labeled with values passed in xlab and ylab, the color is blue as specified in parameter col. 

More often we need to compare values as groups, like sales of different products in different regions OR marks obtained by students as a total of all subjects. 

For example, let's assume there are 3 students in a class, and they study 5 subjects. We want to compare the overall performance of these students but also want to see how their individual scores across the 5 subjects compare. 

Our data matrix can have values like 82, 76,89,49,54 for student1 ;  67, 98, 99, 56, 71 for student2; 93, 80, 50, 65,52 for student3. 

Since we want to plot 3 bars each representing a student, and we want their individual scores in all 5 subjects to be stacked up, we specify nrow=5 and ncol=3. 

Our matrix of values to be passed to the barplot function will look something like  

Values <- matrix( c(82,76,89,49,54,  67,98,99,56,71,  93,80,50,65,52) , nrow=5, ncol=3, byrow=true) 

Here is an example of code input: 

# Create the input vectors.

colors = c("green", "orange", "brown", "red", "yellow" )

stnames <-  c("Mary", "John", "Gill") 

subjects <-  c("Eng", "Math", "Science", "Social St", "Computer")       

# Create the matrix of the values.

Values <- matrix( c(72,76,59,49,54,  67,78,59,56,61,  73,80,50,65,52 ) , nrow=5, ncol=3, byrow=TRUE)  

# Create the bar chart

barplot(Values, main = "Performance comparison", names.arg = stnames, xlab = "Students", ylab = "Total Marks", col = colors ) 

#Ad an optional legend

legend("bottomright", subjects, cex = 0.7, fill = colors) 

The chart from this data should display like the graph shown below:

 

student grade comparison bar chart r

You can also pass the "args.legend" parameter to the barplot function itself to place the legend as per distance from x/y coordinates.  As you can see, R gives us a stacked bar chart depicting the contribution of each subject in a student's total marks obtained. All the stacked bars are uniformly colored based on subject names. 

Here is another variation of the above-stacked bar chart.

student performance side by side comparison

Here a "beside=TRUE" parameter has been added, so now the bars are not stacked on each other, but they are instead depicted side by side. Each student has their own group of bars. 

This type of depiction clearly shows:

  • Which subject(s) contributed heavily to a student's total 
  • The top and bottom performers in each subject 
  • If there is a lot of variation in the marks or if some student has scored consistently higher in all subjects 

Conclusion

To conclude, we have demonstrated how to make a bar chart in R. 

R is powerful enough to handle all your mathematical and statistical calculations required in the big data analytics space. It is likely worthwhile to take an R course to add this program to your skillset. 

Trusted Globally by Leading Organisations

At Nexacu, we are proud to be the trusted training partner for hundreds of leading organisations across Australia, New Zealand, and around the world. From government agencies to multinational corporations, we help teams build practical skills and achieve real outcomes through expert-led training.

  • 400+ companies rely on Nexacu for workforce development
  • Trusted by government agencies at all levels
  • Delivering training across 9 countries and growing

Why Nexacu? 

step by step courseware

Step by Step Courseware

Custom workbook included with a step by step exercises

Facility Image 2
Facility Image 3
Facility Image 1

Interactive real time training

Interactive, Real-Time Training

Learn with expert instructors, wherever you are

Trusted by Business

Trusted by Business

Procured by Government

Procured by Goverment

Reviews Not Found

Valued by Individuals