CHAPTER 4
DATA VISUALIZATION IN R PROGRAMMING
PREPARED BY: DR NIK NUR FATIN FATIHAH BINTI SAPRI
Data Visualization
Qualitative Data Quantitative Data
is a non-numeric data such as (gender, is a numerical data such as (numbe rof
races, occupation, etc) students, monthly salary, etc)
Stem and leaf plot
Frequency table
Box and whisker plot
Histogram
Contingency table
Normal curve
Normal distribution plot
Pie chart
Scatter plot
Normal q-q plot
Bar chart
Ogive
Re me mb e r to sta rt w ith b a sic R co d e
Qualitative Data
Instructions: Using BMI.sav, tabulate the count of
students in each program enrolled
Frequency table #for frequency table, use table (v1)
table(Program) or table(dat$Program)
Output:
Qualitative Data
Instructions: Using BMI.sav, tabulate the count of
students according to each program enrolled
Contingency table #for cross tabulation, use table (v1,v2)
table(gender,Program)
Output:
Qualitative Data
Instructions: Using BMI.sav, display a pie chart of
program
Pie chart #use pie(table(var))
pie(table(Program))#to create simple pie chart
Output:
Qualitative Data
Instructions: Using BMI.sav, display a pie chart of
program with percentage
Pie chart freq=table(Program);freq
pct=(freq/sum(freq))*100;pct
pct=round(pct,0);pct
pct2=paste0(pct,"%");pct2
lab=levels(Program);lab
lab=paste(lab,pct2);lab
pie(freq,label=lab,main="Program Enrolled") #add title
Output:
Qualitative Data
Exercises
1) Using IRIS dataset in R. Perform an R command to create a Pie chart with legends as
shown in the Figure.
2) Create a 3D pie chart as shown in the Figure. The pie chart should have a legend.
Species of Plants
Hint: use “plotrix” package setosa
versicolor
virginica
33%
33%
33%
Qualitative Data
Instructions: Using BMI.sav, display a bar chart of
gender of students
#use barplot(freq.tab,xlab=,ylab=,main=)
Bar chart freq1=table(gender);freq1
barplot(freq1) #basic barplot
#Horizontal mode
barplot(freq1,horiz=TRUE)
Output:
Challenge:
Using the same data, create a bar chart
of program enrolled by students
Qualitative Data
Instructions: Using BMI.sav, display a stacked bar
chart of program enrolled according to gender of
students
#Cross table/contingency Table
Stacked Bar chart cont=table(gender,Program);cont
barplot(cont)#basic stacked barchart
Output:
Qualitative Data
Instructions: Using BMI.sav, display a clustered bar
chart of program enrolled according to gender of
students
#To plot clustered bar plot
Clustered Bar chart barplot(cont,beside=T)
Output:
Quantitative Data
Instructions: Using road dataset available in MASS
package, create a stem and leaf plot for temperature
(temp) variable
library(MASS)
dat4=roadstr(road)
Stem and Leaf Plot attach(road)
stem(temp,scale=0.5)
Output:
Quantitative Data
Instructions: Using BMI.sav, create a box and whisker
plot for variable height of students.
#Vertical box plot
boxplot(height)#basic function
Box and Whisker
Plot #horizontal box plot
boxplot(height,horizontal=T)
Output:
Challenge:
Using the same data, create a box plot
for variable weight of students
Quantitative Data
Instructions: Using BMI.sav, create a box and whisker plot
for variable height of students according to gender.
#Vertical box plot
boxplot(height~gender)#basic function
Box and Whisker
Plot #horizontal box plot
boxplot(height~gender,horizontal=T)
Output:
Challenge:
Using the same data, create a box plot
for variable weight of students according
to gender
Quantitative Data
Instructions: Using BMI.sav, create a histogram for
variable height of students
#Using hist(var)
Histogram hist(height)#basic function
Output:
Challenge:
Using the same data, create a histogram
for variable weight of students
Quantitative Data
Instructions: Using BMI.sav, display a stacked bar
chart of program enrolled according to gender of
students
hist(height,prob=T)
Histogram +
curve(dnorm(x,mean=mean(height),sd=sd(height)),
Normal Curve add=T,col="red",lwd=2,lty=1)
Output:
Challenge:
Using the same data, create a histogram
with normal curve for variable weight of
students
Quantitative Data
Instructions: Using BMI.sav, create a normal density
plot with normal curve height of students
plot(density(height))
curve(dnorm(x,mean=mean(height),sd=sd(height)),
Normal
add=T,col="red",lwd=2,lty=2)
Distribution Plot
Output:
Challenge:
Using the same data, create a normal
density plot with normal curve for
variable weight of students
Quantitative Data
Instructions: Using Ozone.csv, create a scatter plot
to observe the relationship between temperature and
wind speed.
#using plot(x,y) function
Scatter Plot plot(wind,temp)#basic function
abline(lm(temp~wind),col="red",lwd=2)
Output:
Challenge:
Based on the figure, what can you
conclude on the relationship?
#https://r-lang.com/pch-in-
r/#:~:text=The%20pch%20stands%20for%20plot,point%20symbols%20(or%20shapes).
Quantitative Data
Instructions: Using Ozone.csv, create a normal q-q
plot to determine the distribution of the wind speed.
#Using qqnorm(var)
qqnorm(wind)
Normal Q-Q Plot qqline(wind)#adding the straight line
Output:
Challenge:
Based on the figure, what can you
conclude on the distribution of widn
speed?
Quantitative Data
Instructions: Using Ozone.csv, create an ogive of wind speed.
plot(ecdf(wind),col.hor="white",main="Cummulative frequency
distribution", xlab="Wind Speed", ylab="standard normal
Ogive deviate")
range1<-seq(0,20)
distr<-pnorm(range1,mean=mean(wind), sd=sd(wind))
lines(range1,distr, col="red", lwd=2)
Output:
Quantitative Data
ADDING TEXT LABEL IN PLOT
x1=c(124,118,130,127,103,141,114)
x2=c(75,80,95,77,68,105,84)
plot(x1,x2,xlab="systolic",ylab="diastolic",xlim=c(90,150),ylim=c(60,110),
main="Arterial pressure measurement")
text(x1,x2,c("A","C","B","D","E","F","G"),pos=4)
axis(side=1,col="gray",tck=1,lty="dotted")#add grid x-axis
axis(side=2,col="gray",tck=1,lty="dotted")#add grid y-axis
Output:
References
Arguments Description
lty = Line type
cex = Text size
col = Color
pch = Plot type
lwd = Line width
type = Graph type
#https://r-lang.com/pch-in-
r/#:~:text=The%20pch%20stands%
20for%20plot,point%20symbols%2
0(or%20shapes).