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.
Sample Code:
- Now we can make a bar plot out of this data.
Output:
- 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.
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.
- 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.
- Now plotting this data will give our required bar plot. Note below, that we define the argument density to shade the bars.
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.
- 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.
- 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.
- This data is plotted as follows.
- 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