0% found this document useful (0 votes)
25 views38 pages

Graphical Analysis Using R

The document provides an overview of graphical analysis using R, detailing various types of plots such as histograms, pie charts, scatter plots, and box plots. It explains the syntax and parameters for creating these plots, along with examples of how to implement them in R. Additionally, it highlights the importance of graphical data analysis in understanding data properties and assumptions.

Uploaded by

anvisuri05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views38 pages

Graphical Analysis Using R

The document provides an overview of graphical analysis using R, detailing various types of plots such as histograms, pie charts, scatter plots, and box plots. It explains the syntax and parameters for creating these plots, along with examples of how to implement them in R. Additionally, it highlights the importance of graphical data analysis in understanding data properties and assumptions.

Uploaded by

anvisuri05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Dr.

Rakhee Chhibber

Graphical Analysis
using R
Plots in R
Basic Plotting,

Manipulating the plotting window

Box Whisker Plots

Scatter Plots

Pair Plots

Pie Charts

Bar Charts.
Introduction
•Much of the statistical analysis is based on numerical techniques,
such as confidence intervals, hypothesis testing, regression
analysis, and so on. In many cases, these techniques are based on
assumptions about the data being used. One way to determine if
data confirm to these assumptions is the graphical data analysis
with R, as a graph can provide many insights into the properties of
the plotted dataset.
•Graphs are useful for non-numerical data, such as colours,
flavours, brand names, and more. When numerical measures are
difficult or impossible to compute, graphs play an important role.
Types of plots drawn
in R programming are
Plots with Single Variable – You
can plot a graph for a single
variable.
Plots with Two Variables – You
can plot a graph with two
variables.
Plots with Multiple Variables
– You can plot a graph with
multiple variables.
Special Plots – R has low and
high-level graphics facilities.
Plots with Single Variable
•You may need to plot for a single variable
in graphical data analysis with R
programming. For example – A plot
showing daily sales values of a particular
product over a period of time. You can
also plot the time series for month by
month sales.
•The choice of plots is more restricted
when you have just one variable to the
plot. There are various plotting functions
for single variables in R:
 hist(y) – Histograms to show a
frequency distribution.
 plot(y) – We can obtain the values of y
in a sequence with the help of the plot.
 [Link](y) – Time-series plots.
 pie(x) – Compositional plots like pie
diagrams.
Types of plots available in R

Histograms – Used to display the


mode, spread, and symmetry of a
set of data.
Index Plots – Here, the plot takes
a single argument. This kind of plot
is especially useful for error
checking.
Time Series Plots – When a
period of time is complete, the time
series plot can be used to join the
dots in an ordered set of y values.
Pie Charts – Useful to illustrate the
proportional makeup of a sample in
presentations.
Difference between
histogram and bar-chart
A common mistake among
beginners is getting confused
between histograms and bar
charts. Histograms have the
response variable on the x-axis,
and the y-axis shows the
frequency of different values of
the response. In contrast, a bar
chart has the response variable
on the y-axis and a categorical
explanatory variable on the x-
axis.
Bar Plot or Bar Chart
• Bar plot or Bar Chart in R is used to represent the values in data
vector as height of the bars. The data vector passed to the function
is represented over y-axis of the graph. Bar chart can behave like
histogram by using table() function instead of data vector.
Syntax: barplot(data, xlab, ylab)
where:
data is the data vector to be represented on y-axis
xlab is the label given to x-axis
ylab is the label given to y-axis
Note: To know about more optional parameters
in barplot() function, use the below command in R console:
help("barplot")
# defining vector
x <- c(7, 15, 23, 12, 44, 56, 32)

# output to be present as PNG file


png(file = "[Link]")

# plotting vector
barplot(x, xlab = "Geeks Audience", ylab = "Count", col = "white",
[Link] = "darkgreen", [Link] = "darkgreen")

# saving the file


[Link]()
Pie Diagram or Pie Chart
Pie chart is a circular chart divided into different segments according to the
ratio of data provided. The total value of the pie is 100 and the segments tell
the fraction of the whole pie. It is another method to represent statistical data
in graphical form and pie() function is used to perform the same.
Syntax: pie(x, labels, col, main, radius)
where,
• x is data vector
• labels shows names given to slices
• col fills the color in the slices as given parameter
• main shows title name of the pie chart
• radius indicates radius of the pie chart. It can be between -1 to +1
Note: To know about more optional parameters in pie() function, use the
below command in the R console:
help("pie")
# defining vector x with number of articles
x <- c(210, 450, 250, 100, 50, 90)

# defining labels for each value in x


names(x) <- c("Algo", "DS", "Java", "C", "C++", "Python")

# output to be present as PNG file


png(file = "[Link]")

# creating pie chart


pie(x, labels = names(x), col = "white", main = "Articles on
GeeksforGeeks", radius = -1,
[Link] = "darkgreen")

# saving the file


[Link]()
Pie chart – 3D
Pie chart in 3D can also be created in R by using
following syntax but requires plotrix library.
Syntax: pie3D(x, labels, radius, main)
Note: To know about more optional parameters in
pie3D() function, use below command in R console:
help("pie3D")
# importing library plotrix for pie3D()
library(plotrix)
# defining vector x with number of articles
x <- c(210, 450, 250, 100, 50, 90)
# defining labels for each value in x
names(x) <- c("Algo", "DS", "Java", "C", "C++", "Python")
# output to be present as PNG file
png(file = "[Link]")
# creating pie chart
pie3D(x, labels = names(x), col = "white",
main = "Articles on GeeksforGeeks",
labelcol = "darkgreen", [Link] = "darkgreen")
# saving the file
[Link]()
Histogram
Histogram is a graphical representation used to create a graph with bars representing the
frequency of grouped data in vector. Histogram is same as bar chart but only difference
between them is histogram represents frequency of grouped data rather than data itself.
Syntax: hist(x, col, border, main, xlab, ylab)
where:
x is data vector
col specifies the color of the bars to be filled
border specifies the color of border of bars
main specifies the title name of histogram
xlab specifies the x-axis label
ylab specifies the y-axis label

Note: To know about more optional parameters in hist() function, use below command
in R console:
help("hist")
# defining vector
x <- c(21, 23, 56, 90, 20, 7, 94, 12,
57, 76, 69, 45, 34, 32, 49, 55, 57)

# output to be present as PNG file


png(file = "[Link]")

# hist(x, main = "Histogram of Vector x",


xlab = "Values",
[Link] = "darkgreen",
[Link] = "darkgreen")

# saving the file


[Link]()
Scatter Plot
A Scatter plot is another type of graphical representation used to plot the points
to show relationship between two data vectors. One of the data vectors is
represented on x-axis and another on y-axis.
Syntax: plot(x, y, type, xlab, ylab, main)
Where,
x is the data vector represented on x-axis
y is the data vector represented on y-axis
type specifies the type of plot to be drawn. For example, “l” for lines, “p” for
points, “s” for stair steps, etc.
xlab specifies the label for x-axis
ylab specifies the label for y-axis
main specifies the title name of the graph

Note: To know about more optional parameters in plot() function, use the below
command in R console:
help("plot")
• # taking input from dataset Orange already
• # present in R
• orange <- Orange[, c('age', 'circumference')]

• # output to be present as PNG file


• png(file = "[Link]")

• # plotting
• plot(x = orange$age, y = orange$circumference, xlab = "Age",
• ylab = "Circumference", main = "Age VS Circumference",
• [Link] = "darkgreen", [Link] = "darkgreen",
• [Link] = "darkgreen")

• # saving the file


• [Link]()
Pairs
• If a scatter plot has to be drawn to show the relation between 2
or more vectors or to plot the scatter plot matrix between the
vectors, then pairs() function is used to satisfy the criteria.

Syntax: pairs(~formula, data)
• where,
• ~formula is the mathematical formula such as ~a+b+c
• data is the dataset form where data is taken in formula
Note: To know about more optional parameters in pairs()
function, use the below command in R console:
help("pairs")
# output to be present as PNG file
png(file = "[Link]")

# plotting scatterplot matrix


# using dataset Orange
pairs(~age + circumference, data = Orange,
[Link] = "darkgreen")

# saving the file


[Link]()
Box Plot
Box plot shows how the data is distributed in the data vector. It
represents five values in the graph i.e., minimum, first quartile, second
quartile(median), third quartile, the maximum value of the data vector.
Syntax: boxplot(x, xlab, ylab, notch)
where,
x specifies the data vector
xlab specifies the label for x-axis
ylab specifies the label for y-axis
notch, if TRUE then creates notch on both the sides of the box

Note: To know about more optional parameters in boxplot() function,


use the below command in R console:
help("boxplot")
# defining vector with ages of employees
x <- c(42, 21, 22, 24, 25, 30, 29, 22,
23, 23, 24, 28, 32, 45, 39, 40)

# output to be present as PNG file


png(file = "[Link]")

# plotting
boxplot(x, xlab = "Box Plot", ylab = "Age",
[Link] = "darkgreen", [Link] = "darkgreen")

# saving the file


[Link]()
Manipulating the plots
in R
Dr. Rakhee Chhibber
R – Pie Charts
• R Programming Language uses the function pie() to create pie charts. It
takes positive numbers as a vector input.
• Syntax: pie(x, labels, radius, main, col, clockwise)
• Parameters:
• 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.
• col: This parameter give colors to the pie in the graph.
Creating a simple pie chart
• To create a simple R pie chart:
• By using the above parameters, we
can draw a pie chart.
• It can be described by giving simple
labels.

# Create data for the graph.


geeks<- c(23, 56, 20, 63)
labels <- c("Mumbai", "Pune",
"Chennai", "Bangalore")

# Plot the chart.


pie(geeks, labels)
Pie chart including
the title and colors
• To create a color and title pie chart.
• Take all parameters which are required to make a R
pie chart by giving a title to the chart and adding
labels.
• We can add more features by adding more
parameters with more colors to the points.
# Create data for the graph.
geeks<- c(23, 56, 20, 63)
labels <- c("Mumbai", "Pune", "Chennai", "Bangalore")
# Plot the chart with title and rainbow
# color pallet.
pie(geeks, labels, main = "City pie chart",
col = rainbow(length(geeks)))
Slice Percentage & Chart Legend
• To create chart legend and slice percentage, we can plot by doing
the below methods.
• There are two more properties of the pie chart:

• slice percentage
• chart legend.
• We can show the chart in the form of percentages as well as add
legends.

# Create data for the graph.


geeks <- c(23, 56, 20, 63)
labels <- c("Mumbai", "Pune", "Chennai", "Bangalore")
piepercent<- round(100 * geeks / sum(geeks), 1)
# Plot the chart.
pie(geeks, labels = piepercent, main = "City pie chart", col =
rainbow(length(geeks)))
legend("topright", c("Mumbai", "Pune", "Chennai", "Bangalore"),
Add pie chart color palettes

• With the help [Link] function of


the RColorBrewer package in R.
• Get the library.
library(RColorBrewer)

# Create data for the graph.


geeks <- c(23, 56, 20, 63)
labelss <- c("Mumbai", "Pune", "Chennai", "Bangalore")

labels<- [Link](length(geeks), "Set2")

pie(geeks, labels = labelss)


modify the line type of the borders of the plot we can make use of the lty
argument:

Get the library.


library(RColorBrewer)

# Create data for the graph.


geeks <- c(23, 56, 20, 63)
labelss <- c("Mumbai", "Pune", "Chennai",
"Bangalore")

labels<- [Link](length(geeks), "Set2")

pie(geeks, labels = labelss, col = color, lty = 2)


Add shading lines with the
density argument.
#Get the library.
library(RColorBrewer)

# Create data for the graph.


geeks <- c(23, 56, 20, 63)
labelss <- c("Mumbai", "Pune", "Chennai",
"Bangalore")

labels<- [Link](length(geeks), "Set2")

pie(geeks, labels = labelss,col = color, density =


50, angle = 45)
3D Pie Chart
Here we are going to create a 3D Pie chart using plotrix package and then we will use
pie3D() function to plot 3D plot.

# Get the library.


library(plotrix)
# Create data for the graph.
geeks <- c(23, 56, 20, 63)
labels <- c("Mumbai", "Pune", "Chennai", "Bangalore")
piepercent<- round(100 * geeks / sum(geeks), 1)
# Plot the chart.
pie3D(geeks, labels = piepercent,
main = "City pie chart", col =
rainbow(length(geeks)))
legend("topright", c("Mumbai", "Pune", "Chennai",
"Bangalore"), cex = 0.5, fill = rainbow(length(geeks)))
Time Series Graph in R
• Time-series graphs are line graphs that show repeated
measurements taken over time. Time-series graphs can
be rendered in R using the [Link]() function.

You might also like