Professional Documents
Culture Documents
Unit 4 Part B
Unit 4 Part B
Unit 4 Part B
SYNTAX : pie(arguments)
edges = 5
radius = -0.5 (value between −1 and +1).
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)
•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)
# 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
• 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)
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)
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))
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")
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")
•