This slide contains basics of charts and graphs in R programming language. I also focused on practical knowledge so I tried to give maximum example to understand the concepts.
2. Introduction
Since we know that a huge amount of data is generated when
it comes to interpreting any sector
To acquire significant insights, it is usually preferable to depict
data through charts and graphs rather than scanning large
Excel sheets.
The R programming language is mostly used to depict data
graphically in software for statistics and data analytics.
3. The R programming language includes some simple and easy
techniques for converting data into visually appealing features
such as graphs and charts.
In R, charts and graphs are used to graphically depict the
data.
R Programming language has numerous libraries to create
charts and graphs.
There are numerous types of charts and graphs are present in
R language such as pie chart, scatter graph, bar plot, box plot,
mosaic plot, dot chart, coplot, histogram, etc.
4. Pie Chart
A pie chart is a visual depiction of values as colored slices of a
circle.
The slices are identified, and the graphic also shows the
numbers that correlate to each slice.
The pie chart is made in R using the pie() function, which
requires a vector input of positive values.
The extra options are used to customize labels, color, and
title, among other things.
5. Syntax:
pie(x, labels, radius, main, col, clockwise)
vector
Description
radius of
the circle
Direction
color
palette
Title
6. Here,
x indicates a vector that contain numerical values.
labels is used to describe the slices.
radius indicates the radius of the circle of the pie chart.
(value between −1 and +1).
The chart's title is indicated by the main.
The color palette is indicated by col.
The logical value clockwise indicates whether the slices are
drawn clockwise or anticlockwise.
7. Example-1
We can create a very simple pie-chart with the help of only two
parameters such as the input vector and labels.
# Firstly we Create data i.e. vector for the chart.
x <- c(35, 60, 20, 40)
# After that naming labels of slices
labels <- c(“INDIA", "NEW YORK", “NEW DELHI", "MUMBAI")
# Call pie() function to Plot the chart
pie(x,labels)
9. Example- 2
#The below script will create a pie chart and save the pie chart in
the current R working directory.
x <- c(35, 60, 20, 40)
labels <- c(“KANPUR", “BAREILLY", “NEW DELHI", "MUMBAI")
# Call png() function to give the chart file name.
png(file = "city.png")
pie(x,labels)
# To Save the file.
dev.off()
10. Example- 3
#We can expand the features of the pie chart by adding more parameters
to the function. The below script shows it.
pie(x, labels, main="City Pie Chart")
12. Example- 5
pie(x, labels, radius= 1, main="City Pie Chart", col="RED")
Note: For rainbow color pallet, Use rainbow
function.
13. 3D Pie Chart
We can also draw pie chart with 3 dimensions i.e. 3D using
additional packages.
plotrix package has a function called pie3D() that is used for
creating 3D pie chart.
14. Example- 6
# Install Package
Install.packages(“plotrix”)
# Get the library.
library(plotrix)
# Create data for the graph.
x <- c(35, 60, 20, 40)
labels <- c(“KANPUR", “BAREILLY", “NEW DELHI",
"MUMBAI")
pie3D(x,labels = lbl,explode = 0.1, main = "Pie Chart of
Countries ")
15. Histograms
A histogram represents the frequencies of values of a variable
bucketed into ranges.
Histogram is similar to bar chat but the difference is it groups
the values into continuous ranges i.e. In a histogram, there
are no gaps between the bars, unlike a bar graph.
Each bar in histogram represents the height of the number of
values present in that range.
16. R creates histogram using hist() function.
This function takes a vector as an input and uses some more
parameters to plot histograms.
We Use histograms when we have continuous
measurements and want to understand the distribution of
values and look for outliers.
18. Here
v indicates vector that contain numeric values.
Title of the chart is indicated by main
col parameter is used to set color of the bars.
To set border color of each bar, border parameter is used.
xlab is used to give description of x-axis.
xlim and ylim are used to specify the range of values on the x-
axis and y-axis respectively.
breaks is used to mention the width of each bar.
19. Example- 1
# Create histogram
# Create data for the graph.
v <- c(7,11,19,7,40,12,22,31,44,55,43)
# Create the histogram.
hist( v, xlab = "Weight", col = "yellow",
border = "blue")
20. Example- 2
The xlim and ylim parameters can be used to set the range of
values allowed in the X and Y axes, respectively.
Breaks can be used to determine the width of each bar.
v <- c(7,11,19,7,40,12,22,31,44,55,43)
hist(v,xlab = "Weight",col = "green",border =
"red",xlim = c(0,40), ylim = c(0,5), breaks = 5)
21. Bar Charts
A bar chart depicts data as rectangular bars whose length is
proportionate to the variable's value.
The function barplot() in R is used to make bar charts.
In a bar chart, R can create both vertical and horizontal bars.
Each of the bars in a bar chart can be colored differently.
23. Here,
H indicates vector or matrix that contain numeric values.
xlab is the label for x axis.
ylab is the label for y axis.
main is the title of the bar chart.
names.arg is a vector of names appearing under each bar.
col is used to give colors to the bars in the graph.
24. Example- 1
Create a simple bar chart
# Create the data for the bar chart
H <- c(9,15,23,13,22)
# Plot the bar chart
barplot(H)
25. Example- 2
Create a simple bar chart using vector and names of each bar.
# Create the data for the bar chart
H <- c(9,15,23,13,22)
names <- c("March","April","May","June","July")
# Plot the bar chart
barplot(H,names.arg = names)
26. Example- 3
Create a bar chart using other parameters.
H <- c(9,15,23,13,22)
names <- c("March","April","May","June","July")
barplot(H,names.arg=names,xlab="Months",ylab=“
Expenditure",col="yellow", main="Expenditure
chart",border="black")
Or
We also use parameters in this sequence.
barplot(H,xlab="Months",ylab="Expenditure",main="Expenditur
e chart", names.arg = names, col= "blue")
27. Line Graph
A line chart is a graph that connects a set of points by
connecting them with line segments.
These points are sorted according to the value of one of their
coordinates (typically the x-coordinate).
Line charts are commonly used to identify data trends.
The line graph was created using R's plot() function.
28. Syntax:
plot(v, type, main, col, xlab, ylab)
vector
Title
Draw points
or lines
label for
x axis
label for y
axis
colors to both the points
and lines
29. Here
The numeric values are stored in vector V
Type takes value "p" is used to draw only points, "l" is used to
draw just lines, and "o" is used to draw both points and lines.
The x axis is labeled as xlab.
The y axis label is ylab.
The main is the chart's title.
The col is used to color both the points and the lines.
30. Example- 1
We can create a simple line chart using two parameters the
input vector and the type parameter as “o".
v <- c(10,15,22,32)
plot (v,type = “o”)
31. Example - 2
Create using other parameters
v <- c(10,15,11,17,28)
plot (v, main = “Line Graph”, xlab = “Months”, ylab =
“Expenditure”, type = “o”, col = “RED”)
32. Example - 3
Using lines()function, More than one line can be
drawn on the same chart
v <- c(7,12,28,3,41)
t <- c(14,7,6,19,3)
plot(v,type = "o",col = “blue", xlab
= "Months", ylab = “Expenditure",
main = “Expenditure chart")
lines(t, type = "o", col = “yellow")