R Bar Plot - r - learn r - r programming



  • In this article, you will learn to draw bar graph in R programming (using both vector, and matrix).
  • Bar plots can be created in R using the barplot() function.
  • We can supply a vector or matrix to this function. If we supply a vector, the plot will have bars with their heights equal to the elements in the vector.
  • A bar chart or bar plot displays rectangular bars with lengths proportional to the values that they represent.
  • The bars can be plotted vertically or horizontally.
  • One axis of the chart shows the specific categories being compared, and the other axis represents a discrete value.
  • In R, they are made using the barplot function. Bars can be grouped by categories, Making grouped barplot.
  • Barplot must not be confused with histogram nor with boxplot.
  • Suppose, we have a vector of maximum temperatures (in degree Celsius) for seven days as follows.
chart

Sample Code:

max.temp<- c(22, 27, 26, 24, 23, 26, 28)
  • Now we can make a bar plot out of this data.
barplot(max.temp)

Output:

Barplot R-Programming
  • This function can take a lot of argument to control the way our data is plotted.
  • Some of the frequently used ones are, main to give the title, xlab and ylab to provide labels for the axes, names.arg for naming each bar, col to define color etc.
  • We can also plot bars horizontally by providing the argument horiz = TRUE.
# barchart with added parameters

barplot(max.temp,
  main = "Maximum Temperatures in a Week",
xlab = "Degree Celsius",
ylab = "Day",
names.arg = c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"),
  col = "darkred",
horiz = TRUE)
 This function can take a lot of argument to control the way our data is plotted. You can read about them in the help section ?barplot.

Plotting Categorical Data

  • Sometimes we have to plot the count of each item as bar plots from categorical data. For example, here is a vector of age of 10 college freshmen.
age <- c(17,18,18,17,18,19,18,16,18,18)
  • Simply doing barplot(age) will not give us the required plot. It will plot 10 bars with height equal to the student's age.
  • But we want to know the number of student in each age category.
  • This count can be quickly found using the table() function, as shown below.
> table(age)
age
16171819
1261
  • Now plotting this data will give our required bar plot. Note below, that we define the argument density to shade the bars.
barplot(table(age),
  main="Age Count of 10 Students",
xlab="Age",
ylab="Count",
  border="red",
  col="blue",
  density=10
)
 This function can take a lot of argument to control the way our data is plotted. You can read about them in the help section ?barplot.

How to plot higher dimensional tables?

  • Sometimes the data is in the form of a contingency table.
  • For example, let us take the built-in Titanic dataset.
  • This data set provides information on the fate of passengers on the fatal maiden voyage of the ocean liner 'Titanic', summarized according to economic status (class), sex, age and survival.
>Titanic
, ,Age = Child, Survived = No

Sex
ClassMaleFemale
1st00
2nd00
3rd3517
Crew00

, ,Age = Adult, Survived = No

Sex
ClassMaleFemale
1st1184
2nd15413
3rd38789
Crew6703

, ,Age = Child, Survived = Yes

Sex
ClassMaleFemale
1st51
2nd1113
3rd1314
Crew00

, ,Age = Adult, Survived = Yes

Sex
ClassMaleFemale
1st57140
2nd1480
3rd7576
Crew19220
  • We can see that this data has 4 dimensions, class, sex, age and survival. Suppose we wanted to bar plot the count of males and females.
  • In this case we can use the margin.table() function.
  • This function sums up the table entries according to the given index.
>margin.table(Titanic,1)  # count according to class
Class
1st2nd3rdCrew
325285706885

>margin.table(Titanic,4)  # count according to survival
Survived
NoYes
1490711

>margin.table(Titanic)  # gives total count if index is not provided
[1] 2201
  • Now that we have our data in the required format, we can plot, survival for example, as barplot(margin.table(Titanic,4)) or plot male vs female count as barplot(margin.table(Titanic,2)).

How to plot barplot with matrix?

  • As mentioned before, barplot() function can take in vector as well as matrix .
  • If the input is matrix, a stacked bar is plotted.
  • Each column of the matrix will be represented by a stacked bar.
  • Let us consider the following matrix which is derived from our Titanic dataset.
>titanic.data
Class
Survival1st2nd3rdCrew
No122167528673
Yes203118178212
  • This data is plotted as follows.
barplot(titanic.data,
  main = "Survival of Each Class",
xlab = "Class",
  col = c("red","green")
)

legend("topleft",
c("Not survived","Survived"),
  fill = c("red","green")
)
 We have used the legend() function to appropriately display the legend.
  • We have used the legend() function to appropriately display the legend.
  • Instead of a stacked bar we can have different bars for each element in a column juxtaposed to each other by specifying the parameter beside = TRUE as shown below
 We have used the legend() function to appropriately display the legend.

Related Searches to R Barplot