R Bar Charts

R Bar Charts

In this guide, we will discuss R Bar Charts.

A bar chart is a pictorial representation in which numerical values of variables are represented by the length or height of lines or rectangles of equal width. A bar chart is used for summarizing a set of categorical data. In a bar chart, the data is shown through rectangular bars having the length of the bar proportional to the value of the variable.

In R, we can create a bar chart to visualize the data in an efficient manner. For this purpose, R provides the barplot() function, which has the following syntax:

  1. barplot(h,x,y,main, names.arg,col)  
barplot(h,x,y,main, names.arg,col)  
S.NoParameterDescription
1.HA vector or matrix which contains numeric values is used in the bar chart.
2.labA label for the x-axis.
3.labA label for the y-axis.
4.mainA title of the bar chart.
5.names.argA vector of names that appear under each bar.
6.colIt is used to give colors to the bars in the graph.

Example

# Creating the data for Bar chart  
H<- c(12,35,54,3,41)  
# Giving the chart file a name  
png(file = "bar_chart.png")  
# Plotting the bar chart   
barplot(H)  
# Saving the file  
dev.off()  

Output

R Bar Charts

Labels, Title & Colors

Like pie charts, we can also add more functionalities in the bar chart bypassing more arguments in the barplot() functions. We can add a title in our bar chart or can add colors to the bar by adding the main and col parameters, respectively. We can add another parameter i.e., args. name, which is a vector that has the same number of values, which are fed as the input vector to describe the meaning of each bar.

Let’s see an example to understand how labels, titles, and colors are added to our bar chart.

Example

# Creating the data for Bar chart  
H <- c(12,35,54,3,41)  
M<- c("Feb","Mar","Apr","May","Jun")  
  
# Giving the chart file a name  
png(file = "bar_properties.png")  
  
# Plotting the bar chart   
barplot(H,names.arg=M,xlab="Month",ylab="Revenue",col="Green",  
        main="Revenue Bar chart",border="red")  
# Saving the file  
dev.off()  

Output

R Bar Charts

Group Bar Chart & Stacked Bar Chart

We can create bar charts with groups of bars and stacks using matrices as input values in each bar. One or more variables are represented as a matrix that is used to construct group bar charts and stacked bar charts.

Let’s see an example to understand how these charts are created.

Example

library(RColorBrewer)  
months <- c("Jan","Feb","Mar","Apr","May")  
regions <- c("West","North","South")  
# Creating the matrix of the values.  
Values <- matrix(c(21,32,33,14,95,46,67,78,39,11,22,23,94,15,16), nrow = 3, ncol = 5, byrow = TRUE)  
# Giving the chart file a name  
png(file = "stacked_chart.png")  
# Creating the bar chart  
barplot(Values, main = "Total Revenue", names.arg = months, xlab = "Month", ylab = "Revenue", ccol =c("cadetblue3","deeppink2","goldenrod1"))  
# Adding the legend to the chart  
legend("topleft", regions, cex = 1.3, fill = c("cadetblue3","deeppink2","goldenrod1"))  
  
# Saving the file  
dev.off()  

Output

R Bar Charts

Next Topic; Click Here

This Post Has 2 Comments

  1. ‏lewdle

    A round of applause for your blog article.Thanks Again.

Leave a Reply