Introduction
I have written this article to demonstrate creating charts in R. In this article, I aim to explain how we can easily create our charts to display data graphically. A chart helps to visualize data that is otherwise difficult or tedious to understand. Charts can also be used to identify trends in the behavior of data, which is extremely difficult to understand by simply looking at the figures. I will show the readers how to create Pie Charts, Bar Charts, Horizontal Bar Charts, and Stacked Bar Charts using R programming.
Pie Chart
Pie charts are circular charts used to indicate the proportion of subsets of data as a percentage of the overall data. In R, pie charts can be created using the pie() function.
Following is the syntax of the pie() function,
pie(x,labels,col,main)
The x parameter specifies the values to be plotted.
The labels parameter specifies the labels to be used for the slices of the pie chart.
The col parameter specifies the colors to be used to fill the slices.
The main parameter specifes the main title for the chart
The following code illustrates the creation of a pie chart using data stored in lists,
# Plotting a Pie chart of student marks
marks <- c(75,82,65,40,87)
names <- c("AAA","BBB","CCC","DDD","EEE")
percentages <- round(100 * marks / sum(marks),1)
png(file="pie.png")
pie(marks,labels=paste(names," - ",marks," - ",percentages,"%"),
col=rainbow(length(names)),main="Student Marks Data")
legend("bottomleft",names,fill=rainbow(length(names)))
print("Chart saved as pie.png")
The above code computes the percentages using marks and plots the marks with labels as names, marks, and percentages.
The paste() function is used to concatenate the names, marks, and percentages to be used as labels.
The rainbow color palette is specified using the col parameter.
The legend() function displays the legend at the bottom left corner.
The resulting pie.png file contains the pie chart as follows,

Bar Chart
Bar charts show data as rectangular bars with the length of the bars indicating the value of the data being plotted. In R, bar charts can be created using the barplot() function.
Following is the syntax of the barplot() function:
x <- barplot(H,xlab,ylab,main,col,xlim,ylim)
The H parameter specifies the values to be plotted.
The xlab parameter specifies the labels to be displayed on the X-Axis.
The ylab parameter specifies the labels to be displayed on the Y-Axis.
The col parameter specifies the colors to be used to fill the slices.
The main parameter specifies the main title for the chart
The xlim parameter specifies the limit for the X-Axis.
The ylim parameter specifies the limit for the Y-Axis.
The following code illustrates the creation of a bar chart using data stored in lists,
# Plotting a Bar chart of student marks
marks <- c(75,82,65,40,87)
names <- c("AAA","BBB","CCC","DDD","EEE")
png(file="bar.png")
x <- barplot(marks~names,xlab="Names",ylab="Marks",main="Student Mark Data",col=rainbow(length(names)),ylim=c(0,100))
y <- as.matrix(marks)
text(x,y+2,labels=as.character(y))
print("Chart saved as bar.png")
print(x)
print(y)
The above code plots names on the X and marks on the Y-Axis. The barplot() function returns a vector containing the X Co-ordinates of the center of each bar, which is used as the first parameter in the text() function indicating the X Co-ordinate of the data label to be displayed. The second parameter of the text() function is the Y Co-ordinate which is obtained using the matrix() function with the data plotted on the Y-Axis. The third parameter of the text() function is the value to be displayed converted to character type using the as.character() function. The ylim parameter is used to specify the Y-Axis limit.




Join the conversation! Your thoughts help the community grow.