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”.
- 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")
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()
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")
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")
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")
You can also watch video on R tutorial from our YouTube channel.
0 Comments