R Programming

Plot Barchart with ggplot2 in R

We provide effective and economically affordable training courses for R and Python, Click here for more details and course registration !

Barcharts can be used to plot the frequency of a single categorical variable, or illustrate the cross relationship between two or more categorical variables. In R programming, function geom_bar() from ggplot2 package can easily create simple barcharts, stacked or grouped barcharts. The following codes show creation of different barcharts, using a virtual dataset “grade”.

  1. Create data frame ‘grade’ by reading csv file from working directory.
#set the number of digits printed
options(digits = 2)  
 # to set working directory
setwd("d:\\RStatistics-Tutorial")  
#create a grade data frame
vartype<-c("character", "character", "character", "character", "character", "numeric","numeric", "numeric","numeric","character")
grade <- read.table("University-Fullname-full.csv", colClasses=vartype, header=TRUE, sep=",")                                      

#to show first several observations of the data frame
head(grade)
  StudentID        Fullname Race Gender Country Age Math Physics
1         1     James Zhang    A   Male      US  23   73      70
2         2       Wilson Li    E Female      UK  26   95      76
3         3 Richard Nuan Ye    A   Male      UK  35   77      83
4         4       Mary Deng    E Female      US  21   60      99
5         5    Jason Wilson    A   Male      UK  19   77      89
6         6 Jennifer Hopkin    A Female      UK  43   79      64
  Chemistry       Date
1        87 10/31/2008
2        83  3/16/2008
3        92  5/22/2008
4        84  1/24/2009
5        93  7/30/2009
6        83   4/5/2009
2. Create a simple vertical barchart
#Simple vertical bar chart of variable 'Gender'
ggplot(grade, aes(x=Gender)) + geom_bar() +
  labs(title="Simple Bar chart",                 
       x="Gender",                          
       y="Frequency") 

        
Simple barchart

3. We can also plot a simple horizontal barchart using the same variable

#Simple horizontal bar chart
ggplot(grade, aes(x=Gender)) + geom_bar() +
  labs(title="Horizontal Bar chart",             
       x="Gender",                          
       y="Frequency") +                          
  coord_flip()  

Horizontal barchart

4. If we want to show how is the variation of one categorical variable inside another categorical variable, then stacked or group barcharts are appropriate. The following code is used for creating a stacked barchart, where different countries are stacked together inside each Gender value.

ggplot(grade, aes(x=Gender, fill=Country)) +
  geom_bar(position = "stack") +  
  labs(title="Stacked Bar chart",                   
       x="Gender",                               
       y="Country") 
Stacked barchart

5. Comparatively, categorical variables can be grouped alongside when we plot barcharts to reflect cross relationship between two variables. Here, a grouped barchart is plotted in which countries values stay side by side with each Gender value.

ggplot(grade, aes(x=Gender, fill=Country)) +
  geom_bar(position = "dodge") + 
  labs(title="Grouped Bar chart",                   
       x="Gender",                               
       y="Country")   

Grouped barchart

6. Another type barchart is filled barchart. It is quite similar as stacked barchart, while the hight of each bar is scaled to same length, such that the proportion of the categorical variable inside could be identified by first glance.

ggplot(grade, aes(x=Gender, fill=Country)) +
  geom_bar(position = "fill") +  
  labs(title="Filled Bar chart",                   
       x="Gender",                               
       y="Country") 

Filled barchart

You can also watch video on R tutorial from our YouTube channel.

wilsonzhang746

Recent Posts

Download R Course source files

Click here to download R Course source files !

2 months ago

Download Python Course source files

Click here to download Python Course Source Files !

2 months ago

How to create a data frame from nested dictionary with Pandas in Python

For online Python training registration, click here ! Pandas provides flexible ways of generating data…

5 months ago

How to delete columns of a data frame in Python

For online Python training registration, click here ! Data frame is the tabular data object…

5 months ago

Using isin() to check membership of a data frame in Python

Click her for course registration ! When a data frame in Python is created via…

5 months ago

How to assign values to Pandas data frame in Python

We provide affordable online training course(via ZOOM meeting) for Python and R programming at fundamental…

5 months ago