DataVisualization in R
DataVisualization in R
Data visualization in R
Basic graphics
In this lecture
Basic graphics
◦ Scatter
◦ Line
◦ Bar
Need for sophisticated graphics
Scatter plot
R – code:
X = 1:10
Y= X^2
plot (Y)
Scatter plot
dataset ‘mtcars’:
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)
Line plot
R – code :
X = 1:10
Y= X^2
plot(X,Y,type = ‘l’)
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")
Bar plot
Challenges
The exact figure as per the previous slide can be reproduced
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
Summary
1) Scatter plots
2) Line plots
3) Bar plots
graphics