Unit 4 Part B

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 63

Unit 4

Charts and graphs


BY
KSL
Charts
• Charts are mainly used to generate a graphical
representation of the given data sets in the
form of a Circle, Bar, Line etc.

• Its helps user to understand the information


better and make data visualization simpler.
Types of charts
• Pie
• Bar Chart
• Histogram
• Scatter Plot
• Box Plot
Pie chart
# Pie Charts give more visibility as judging lenth is more precise than judging volume

SYNTAX : pie(arguments)

pie(part, labels, edges, radius, clockwise, init.angle, density,


angle, col, main)
# part: is a vector containing the non-negative numeric values to tell the size of parts
# labels: is used to give description to each part (slices).
# edges: used to change the outer circle of the pie, default = 200
# radius: indicates the radius of the circle of the pie chart. (value between −1 and +1).
# init.angle: used to specify the initial angle, deafult = 0
# density: used to show the density of shading lines, default = NULL
# main: indicates the title of the chart.
# col: indicates the color palette.
# clockwise: It is a logical value indicating if the slices are drawn clockwise or anti
clockwise.
Pie chart
• parts <- c(8,12,16,4,10)
lbls <- c("Walking", "Car", "Bus", "Cycle", "Train")
pie(parts,labels = lbls)
Pie chart with edges and main
• pie(parts,labels = lbls, edges = 5, main =
"Startegies utilized for travelling to Office")

edges = 5
radius = -0.5 (value between −1 and +1).

• pie(parts,labels = lbls, radius = -0.5, main =


"Startegies utilized for travelling to Office")

radius
clockwise = TRUE (default is FALSE)
• pie(parts, labels = lbls, clockwise = TRUE,
main = "Startegies utilized for travelling to Office")

clockwise
density = 20
• density: used to show the density of shading lines, default =
NULL
• pie(parts,labels = lbls, density = 20, main =
"Startegies utilized for travelling to Office")

density
density = 20, angle = 90
• pie(parts,labels = lbls, density = 20, angle = 90,
main = "Startegies utilized for travelling to
Office")

angle
Angle =0
• pie(parts,labels = lbls, density = 20, angle = 0,
main = "Startegies utilized for travelling to
Office")

Angle =0
Customized colors
• clor <-c("Red","Green","Blue","Orange","Cyan")
• pie(parts,labels = lbls, col = clor, main =
"Startegies utilized for travelling to Office")
Border=“red”
• pie(parts,labels = lbls, border = "red", main =
"Startegies utilized for travelling to Office")

border
Chart Legend
• SYNTAX : legend(graphics)
pie(parts,labels = lbls, col=clor, main = "Startegies utilized for travelling to Office")

legend("topright", fill=clor, c("Walking", "Car", "Bus", "Cycle", "Train"))

Chart Legend
3D Pie Chart
• SYNTAX : pie3d(paremeters)
install.packages("plotrix")
library(plotrix)
• pie3D(parts,labels = lbls,radius = 1,explode =
0.1, main = "Startegies utilized for travelling
to Office" )
# BAR CHART

# A bar chart represents data in rectangular bars with length of the bar proportional to the
value of the variable.

# SYNTAX : barplot(arguments)

barplot(part, names.arg, xlab, ylab, main, col)

•part: is a vector containing the non-negative numeric values to tell the size of parts
main: indicates the title of the chart.
xlab: is the label for x axis.
ylab: is the label for y axis.
names.arg: is a vector of names appearing under each bar.
col: is used to give colors to the bars in the graph.
space: used to give space between each bar
horiz: to display the chart in vertical or horizontal position
density: used to show shading lines inside bars
angle: used to assign the angle of shading lines
border: to specify a colour for border
sub: to dispaly subtitle for the bar chart
xlim: used to specify the limits for the X-axis, ex: c(0,10)
• ylim: used to specify the limits for the Y-axis, ex: c(1,0)
legend: List of arguments we want to add to the legend() function
Bar plot
• part <- c(20,10,16,4,10)
barplot(part, main = "Startegies utilized for
travelling to Office" )
Xla, ylab
• barplot(part, main = "Startegies utilized for
travelling to Office", xlab = "Vehicles", ylab =
"Numbers" )
With labels
• labels <- c("Car", "Bus", "Bike","Walking", "Train")
barplot(part, main = "Startegies utilized for travelling
to Office", xlab = "Vehicles", ylab = "Numbers",
names.arg = labels )

labels
Density and horiz
• barplot(part, main = "Startegies utilized for travelling
to Office", xlab = "Vehicles", ylab = "Numbers",
names.arg = labels, density = 10, horiz = TRUE)
col
• barplot(part, main = "Startegies utilized for
travelling to Office", xlab = "Vehicles", ylab =
"Numbers", names.arg = labels, col = clor)
angle
• barplot(part, main = "Startegies utilized for
travelling to Office", xlab = "Vehicles", ylab =
"Numbers",names.arg = labels, col = clor,
density = 20, angle = 45)
legend
• legend("topright", labels, cex = 0.7, fill = clor)
stacked bar plot
# Designing a stacked bar plot using matrix
values
• vec <- c(1:9)
values <- matrix(vec,3,3)

barplot(values, col = c("red","green","blue"))


# Designing a group bar plot using matrix
values
barplot(values, col = c("red","green","blue"),
beside = TRUE)

group bar plot


HISTOGRAM

# 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.
# Each bar in histogram represents the height of the number of values present in that range.

# SYNTAX: hist(arguments)

hist(v,main,xlab,xlim,ylim,breaks,col,border)
# v: is a vector containing numeric values used in histogram.
# main: indicates title of the chart.
# col: is used to set color of the bars.
# border: is used to set border color of each bar.
# xlab: is used to give description of x-axis.
# ylab: is used to give description of y-axis.
# xlim: is used to specify the range of values on the x-axis.
# ylim: is used to specify the range of values on the y-axis.
# breaks: is used to mention the width of each bar.
# labels: used to display the values at the top of the block, Boolean
hist()
• hist(cars$speed, main = "Cars Dataset",
col = c("Red","Green","Blue","Orange","Cyan"))
• hist(cars$speed, main = "Cars Dataset", xlab =
"Car speed : mph", ylab = "Frequency",
col = c("white","yellow"), border = "blue")
• hist(cars$speed, main = "Cars Dataset", xlab =
"Car speed : mph", ylab = "Frequency",
col = c("white","yellow"), border = "blue",
labels = T, xlim = c(0,15), ylim = c(0,30))

border = "blue"
labels
Labels=TRUE
• hist(cars$speed, main = "Cars Dataset", xlab =
"Car speed : mph", ylab = "Frequency",
col = c("white","yellow"), border = "blue",
labels = TRUE, ylim = c(0,30),
density = 20, breaks = 10, angle = 45)
# Designing a density curve

hist(cars$speed, main = "Cars Dataset", xlab =
"Car speed : mph", ylab = "Frequency",
col = c("Red","yellow"), border = "blue",xlim
= c(0,30), ylim = c(0,.07))
lines(density(cars$speed))
Combining multiple histograms
• # Combining multiple histograms

• hist(cars$speed, main = "Cars Dataset", xlab =


"Car speed : mph", ylab = "Frequency",
col = c("white","yellow"), border = "blue")

• d<-c(12,5,6,7,25,1,13,9,8,1,2,3,4,5)
• hist(d, col = "blue", add = T)
d<-c(12,5,6,7,25,1,13,9,8,1,2,3,4,5)
hist(d, col = "blue", add = T)
LINE GRAPH

# Line charts are usually used in identifying the trends in data.

# SYNTAX: plot(arguments)

plot(v,type,col,xlab,ylab)

# v: is a vector containing the numeric values.


# type: takes the value "p" to draw only the points,
# "l" to draw only the lines and
# "o" to draw both points and lines.
# "h" to draw line in vertical format
# "s" to draw line in square wave format
# xlab: is the label for x axis.
# ylab: is the label for y axis.
# main: is the Title of the chart.
# col: is used to give colors to both the points and lines.
plot(cars, main = "Cars Dataset")
plot(cars, main = "Cars Dataset", type = "p" , xlab
= "Speed in mph",
ylab = "distance taken to stop in ft")

Xlab and ylab


plot(cars, main = "Cars Dataset", type = "l" , xlab =
"Speed in mph", ylab = "distance taken to stop in ft")

type = "l"
plot(cars, main = "Cars Dataset", type = "o" , xlab =
"Speed in mph", ylab = "distance taken to stop in ft")

type = "o"
plot(cars, main = "Cars Dataset", type = "s" , xlab =
"Speed in mph", ylab = "distance taken to stop in ft")

type = "s"
plot(cars, main = "Cars Dataset", type = "h" , xlab =
"Speed in mph", ylab = "distance taken to stop in ft")

type = "h"
# Multiple lines in Line Graph
• plot(cars, main = "Cars Dataset", type = "l" , xlab = "Speed in mph", ylab =
"distance taken to stop in ft", col = "green")
• lines(d, type = "o", col = "blue")
# SCATTER PLOT
• Scatter plots are used to show the relation between 2
variables of the given sets of data.
• Data are displayed as a group of points.
• They can be used when one of the two variables are
both dependent or independent of each other.
• They are used to display info related to marketing,
finance, etc

# SYNTAX: plot(arguments)
• plot(x, y, main, xlab, ylab, xlim, ylim, axes)

# x: is the data set whose values are the horizontal coordinates.


# y: is the data set whose values are the vertical coordinates.
# main: is the tile of the graph.
# xlab: is the label in the horizontal axis.
# ylab: is the label in the vertical axis.
# xlim: is the limits of the values of x used for plotting.
# ylim: is the limits of the values of y used for plotting.
# axes: indicates whether both axes should be drawn on the plot.
# sub: to display the subtitle
# col: used to assign colour
# frame.plot: used to a box around the scatter plot
# asp: aspect ratio of the scatter plot
Cars data set
• plot(x=cars$speed, y=cars$dist, main = "Cars
Dataset", sub = "Data")
plot(x=cars$speed, y=cars$dist, main = "Cars
Dataset", sub = "Data", col = "red", frame.plot = F)

col = "red"
plot(x=cars$speed, y=cars$dist, main = "Cars Dataset", sub =
"Data", col = "red", frame.plot = F, xlab = "Speed", ylab =
"Distance", xlim = c(0,20), ylim = c(0,60))

xlim = c(0,20), ylim = c(0,60)


plot(x=cars$speed, y=cars$dist, main = "Cars Dataset", sub =
"Data", col = "red", frame.plot = F, xlab = "Speed", ylab =
"Distance", xlim = c(0,20), ylim = c(0,60), asp = 5/20)

asp = 5/20
plot(x=cars$speed, y=cars$dist, main = "Cars Dataset",
sub = "Data", col = "red", frame.plot = F, xlab = "Speed",
ylab = "Distance", axes = F)

axes = F
# BOX PLOT
• # Boxplots are a measure of how well distributed is
the data in a data set.
# It divides the data set into three quartiles.
# This graph represents the minimum, maximum,
median, first quartile and third quartile in the data
set.
# It is also useful in comparing the distribution of
data across data sets by drawing boxplots for each of
them.
# SYNTAX : boxplot(arguments)
boxplot(x, data, notch, varwidth, names,
main)

# x: is a vector or a formula.
# data: is the data frame.
# notch: used to design a line on each side of the boxes.
# Set as TRUE to draw a notch.
# varwidth: is a logical value. Set as true to draw width
# of the box proportionate to the sample size
# (Sqare roots of the no of observations) .
# names: are the group labels which will be printed under each boxplot.
# xlab: label for x-axis
# ylab: label for y-axis
# main: is used to give a title to the graph.
# subset: used to limit the bar plots by providing a vector of values
# horizontal: to display the chart in vertical or horizontal position
# border: to specify a colour for border
• > cars
• > head(cars)
• speed dist
• 1 4 2
• 2 4 10
• 3 7 4
• 4 7 22
• 5 8 16
• 6 9 10
> boxplot(speed~dist,data = cars)
boxplot(speed~dist,data = cars, xlab = "Speed in
mph", ylab = "distance taken to stop in ft",
main = "Cars Dataset")

Xlab, ylab, main


boxplot(speed~dist,data = cars, xlab = "Speed in mph",
ylab = "distance taken to stop in ft", main = "Cars Dataset",
col = c("Red", "Green"), notch = TRUE)

notch = TRUE
boxplot(speed~dist,data = cars, xlab = "Speed in mph",
ylab = "distance taken to stop in ft", main = "Cars
Dataset", col = c("Red", "Green"), horizontal = TRUE)

horizontal = TRUE
boxplot(speed~dist,data = cars, xlab = "Speed in mph",
ylab = "distance taken to stop in ft", main = "Cars
Dataset", col = c("Red", "Green"), notch = F, varwidth =
T, border = "blue")

notch = F, varwidth = T, border = "blue")

You might also like