Data science for Engineers
Data visualization in R
Basic graphics
Basic graphics NPTEL NOC18-CS28 1
Data science for Engineers
In this lecture
Basic graphics
◦ Scatter
◦ Line
◦ Bar
Need for sophisticated graphics
Basic graphics NPTEL NOC18-CS28 2
Data science for Engineers
Scatter plot
R – code:
X = 1:10
Y= X^2
plot (Y)
Basic graphics NPTEL NOC18-CS28 3
Data science for Engineers
Scatter plot
dataset ‘mtcars’:
Basic graphics NPTEL NOC18-CS28 4
Data science for Engineers
Scatter plot
R – code : Corresponds to different shapes
for points, for more such
plot( mtcars$wt, mtcars$mpg , options check ‘graphics
parameters’ in help
main="Scatterplot Example",
xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)
Basic graphics NPTEL NOC18-CS28 5
Data science for Engineers
Line plot
R – code :
X = 1:10
Y= X^2
plot(X,Y,type = ‘l’)
Basic graphics NPTEL NOC18-CS28 6
Data science for Engineers
Bar plot
Syntax:
barplot(H, names.arg, xlab, ylab, main, names.arg, col)
R – code :
H <- c(7,12,28,3,41)
M <- c("Mar","Apr","May","Jun","Jul")
barplot(H,names.arg = M, xlab = "Month", ylab = "Revenue",
col = "blue", main = "Revenue chart",border = "red")
Basic graphics NPTEL NOC18-CS28 7
Data science for Engineers
Bar plot
Basic graphics NPTEL NOC18-CS28 8
Data science for Engineers
Need for sophisticated graphics
Lets us say there is a need for you to show multiple plots in
a single figure such as the following:
Basic graphics NPTEL NOC18-CS28 9
Data science for Engineers
Challenges
The exact figure as per the previous slide can be reproduced
with the following code:
par(mfrow=c(2,4))
days <- c("Thur", "Fri", "Sat", "Sun")
sexes <- unique(tips$sex)
for (i in 1:length(sexes)) {
for (j in 1:length(days)) {
currdata <- tips[tips$day == days[j] & tips$sex == sexes[i],]
plot(currdata$total_bill, currdata$tip/currdata$total_bill,
main=paste(days[j], sexes[i], sep=", "), ylim=c(0,0.7), las=1)
}
}
Basic graphics NPTEL NOC18-CS28 10
Data science for Engineers
Challenges
But the code requires work such as :
• Knowing when to introduce a for loop
• Which columns of the data.frame to select
• The positioning of each graph in the grid etc
• Less pleasing visuals
Basic graphics NPTEL NOC18-CS28 11
Data science for Engineers
Summary
1) Scatter plots
2) Line plots
3) Bar plots
4) Challenges and disadvantages of basic
graphics
Basic graphics NPTEL NOC18-CS28 12