Press "Enter" to skip to content

How to create a Bar plot?

Zigya Acadmey 0

A Bar Graph (or a Bar Chart) is a graphical display of data using bars of different heights.
They are good if you want to visualize the data of different categories that are being compared with each other.

In R, you can create a bar graph using the barplot() function. It has many options and arguments to control many things, such as labels, titles and colors.

Usage

> barplot(x, y, type, main, xlab, ylab, pch,
          col, las, bty, bg, cex, …)

Arguments

ValueDescription
heightA vector or matrix of values describing the bars which make up the plot
widthA vector specifying bar widths
spaceThe amount of space left before each bar
names.argThe names to be plotted below each bar
legend.textThe text used to construct a legend for the plot
besideIf TRUE the columns are portrayed as juxtaposed bars
horizIf TRUE, the bars are drawn horizontally
densityThe density of shading lines
angleThe slope of the shading line
mainAn overall title for the plot
xlabThe label for the x-axis
ylabThe label for the y axis

Create a Bar Graph

To get started, We need to a dataset to create a bar pot. For this example we are going to make a dataset Fruits

> fruits <- c(Banana=10, orange=80, Kiwi=30, Grapes=20, Apple=50)
> fruits
Banana orange   Kiwi Grapes  Apple
    10     80     30     20     50

To create a bar graph just specify the vector in barplot() function.

> barplot(fruits)
default plot`

Coloring a Bar Graph

Use col argument to change the colors used for the bars.

> barplot(fruits,
          col= c("red", "green", "blue", "yellow", "orage", "purple"))
`Colored plot

Create a Hatched Bar Graph

Creating hatched graphs in R is rather easy, just specify the density argument in the barplot() function.

By default, the bars are hatched with 45° slanting lines; however, you can change it with the angle argument.

> barplot(fruits,
          col= "red",
          density = 20,
          angle = 45)

Bar Width and Spacing

To make the bars narrower or wider, set the width of each bar with the width argument. Larger values make the bars wider, and smaller values make the bars narrower.

> barplot(fruits,
          col= "red",
          density = 20,
          angle = 45,
          space = 1,
          width = c(10, 35, 25, 20, 30))

Adding Titles and Axis Labels

You can add your own title and axis labels easily by specifying following functions.

> barplot(fruits,
          col= "red",
          density = 20,
          angle = 45,
          space = 1,
          width = c(10, 35, 25, 20, 30),
          main = "Fruit Stock",
          ylab = "Number of Fruits")

Horizontal Bar Graph

You can also plot bars horizontally by setting the horiz argument to TRUE.

> barplot(fruits,
          col= "red",
          density = 20,
          angle = 45,
          space = 1,
          width = c(10, 35, 25, 20, 30),
          main = "Fruit Stock",
          xlab = "Number of Fruits",
          horiz = TRUE)
Horiz Plot

Stacked Bar Graph

If your data contains several groups of categories, you can display the data in a bar graph in one of two ways. You can decide to show the bars in groups (grouped bars) or you can choose to have them stacked (stacked bars).

Let’s put the data in a matrix

> new <- c(Banana=10, orange=80, Kiwi=30, Grapes=20, Apple=50)
> old <- c(Banana=4, orange=35, Kiwi=15, Grapes=40, Apple=50)
> fruits <- rbind(old, new)

Now you can pass this matrix to the barplot() function to create a stacked bar graph.

> barplot(fruits,
          col= c("green", "yellow"),
          main = "Fruit Stock",
          legend.text = rownames(fruits),
          args.legend=list(cex=0.75,x = "topright"),
          ylab = "Number of Fruits")

Conclusion

Hence we studied, How to make Bar plot in R, with different colors and different widths along with different density and also learned how to make a stacked bar plot.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Leave a Reply

Your email address will not be published.