A pie chart is a circular graphical representation that is further subdivided into multiple slices according to the provided numerical values.
There are various libraries available in the R programming language
to create different types of graphs. We can make a pie chart in R by
using the function pie().
Syntax: pie(x, labels, radius, main, col, clockwise)
x: This parameter is a vector that contains the numeric values which are used in the pie chart.
labels: This parameter gives the description to the slices in pie chart.
radius: This parameter is used to indicate the radius of the circle of the pie chart.(value between -1 and +1).
main: This parameter is represents title of the pie chart.
clockwise: This parameter contains the logical value which indicates whether the slices are drawn clockwise or in anti clockwise direction. (not important)
col: This parameter give colors to the pie in the graph. (not important)
The information below shows the population (in millions) of the top ten most populous cities in the US in 2019.
New York - 8.60
Los Angeles - 4.06
Chicago - 2.68
Houston - 2.40
Phoenix - 1.71
Philadelphia - 1.58
San Antonio - 1.57
San Diego - 1.45
Dallas - 1.40
San Jose - 1.03
Using the given information, let us create a data frame and call it mydata.
Cities <- c("New York", "Los Angeles", "Chicago", "Houston", "Phoenix",
"Philadelphia", "San Antonio", "San Diego", "Dallas", "San Jose")
Population <- c(8.60, 4.06, 2.68, 2.40, 2.71, 1.58, 1.57, 1.45, 1.40, 1.03 )
# creating data frame or data matrix
mydata <- data.frame(Cities, Population)
# how does it look?
mydata
## Cities Population
## 1 New York 8.60
## 2 Los Angeles 4.06
## 3 Chicago 2.68
## 4 Houston 2.40
## 5 Phoenix 2.71
## 6 Philadelphia 1.58
## 7 San Antonio 1.57
## 8 San Diego 1.45
## 9 Dallas 1.40
## 10 San Jose 1.03
#naming the file where you want to save it
png(file = "piechart1_1.png")
# creating the pie chart
pie(mydata$Population)
#saving the file
dev.off()
## png
## 2
#naming file, where you want to save it
png(file = "piechart1_2.png")
# count in the first postion, labels in the second
pie(Population,Cities)
# saving the file
dev.off()
## png
## 2
A bar chart represents data in rectangular bars with length of the bar proportional to the value of the variable. R uses the function barplot() to create bar charts. R can draw both vertical and Horizontal bars in the bar chart. In bar chart each of the bars can be given different colors.
The basic syntax to create a bar-chart in R is −
H: This parameter is a vector or matrix containing numeric values which are used in bar chart.
xlab: This parameter is the label for x axis in bar chart.
ylab: This parameter is the label for y axis in bar chart.
main: This parameter is the title of the bar chart.
names.arg: This parameter is a vector of names appearing under each bar in bar chart.
col: This parameter is used to give colors to the bars in the graph.
Approach: In order to create a Bar Chart:
A vector (H <- c(Values…)) is taken which contain numeral values to be used.
This vector H is plot using barplot().
# Create the data for the chart
A <- c(17, 32, 8, 53, 1)
# Plot the bar chart
barplot(A, xlab = "X-axis", ylab = "Y-axis", main ="Bar-Chart")
Approach: To create a horizontal bar chart:
Take all parameters which are required to make simple bar chart.
Now to make it horizontal new parameter is added
barplot(A, horiz=TRUE )# Create the data for the chart
A <- c(17, 32, 8, 53, 1)
# Plot the bar chart
barplot(A, horiz = TRUE, xlab = "X-axis",ylab = "Y-axis", main ="Bar-Chart")
Label, title and colors are some properties in the bar chart which
can be added to the bar by adding and passing an argument.
Approach:
To add the title in bar chart.
barplot( A, main = title_name )X-axis and Y-axis can be labeled in bar chart. To add the label in bar chart.
barplot( A, xlab= x_label_name, ylab= y_label_name)To add the color in bar chart.
barplot( A, col=color_name)Example :
# Create the data for the chart
A <- c(17, 2, 8, 13, 1, 22)
B <- c("Jan", "feb", "Mar", "Apr", "May", "Jun")
# Plot the bar chart
barplot(A, names.arg = B, xlab ="Month",
ylab ="Articles", col ="blue",
main ="Bar Chart")