How to Draw a Horizontal Barplot in R
A barplot is a representation of data in form of the bar the height of bar represents the number of values. In this article, we will discuss How to Draw a Horizontal Barplot in R programming language.
R language supports two ways to plot a bar plot, and we will discuss how this can be modified to be put to our use.
Method 1: Using barplot()
R Language uses the function barplot() to create bar charts. Here, both vertical and Horizontal bars can be drawn.
Syntax: barplot(H, xlab, ylab, main, names.arg, col)
- H: This parameter is a vector or matrix containing numeric values which are used in bar chart.
- xlab: This parameter is the label for x axis in bar chart.
- ylab: This parameter is the label for y axis in bar chart.
- main: This parameter is the title of the bar chart.
- names.arg: This parameter is a vector of names appearing under each bar in bar chart.
- col: This parameter is used to give colors to the bars in the graph.
The bar function has a field named “horiz” keep that field value “True” to create the bar plot horizontally.
Method 2: Using GGPLOT2
In this method, we draw the bar plot using the ggplot2 function. ggplot2 have a function named geom_bar() which is used to plot the horizontal bar, and we put our data into the geom_bar function with ggplot() to plot the bar. At last, we will flip the whole plot using the coord_flip() function. A flipped vertical plot will generate a horizontal plot.