Tutorial for Bar Plot in ggplot2 with Examples

Syntax of Bar Plot in ggplot2

In ggplot2, the following is the minimal syntax for generating the bar plot  –

ggplot(<data>, mapping = aes(<mapping>) + geom_bar()

You can customize the bar plot by adding more layers such as themes, labs, etc.

Loading ggplot2

The ggplot2 library can be loaded with the below line.
In [1]:
library(ggplot2)

In the below example, we are plotting the very basic bar plot in ggplot2 by passing the dataset along with the mapping of attributes for x and y.

Example 2: Horizontal Bar Plot

horizontal bar plot in R ggplot2 Example
Out[4]:
Out[5]:
Out[6]:

 

 

  • Veer Kumar

    I am passionate about Analytics and I am looking for opportunities to hone my current skills to gain prominence in the field of Data Science.

    View all posts

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *