## R : Graphics Tutorial Series ( Part 3 )

- 04/01/2017
- 908
- 0 Like

**Published In**

- Big Data
- Analytics
- Artificial Intelligence

Ad: 50000 Data Science Jobs Globally | Over 10000 Hours of Free Data Science Video Tutorials:- Register Now

In this first part of the Tutorial Series *R : Graphics Tutorial Series ( Part 1 )* , we learnt the basics of R Base Graphics while in the second part of the Series R : Graphics Tutorial Series ( Part 2 ) we saw various graphical methods for displaying relationships between two variables (bivariate relationships) and between many variables (multivariate relationships).

This is an extension of the Part 2 where we will start with scatter plots and scatter plot matrices. Then, we’ll explore line charts of various types. These approaches are well known and widely used in research. Next, we’ll review the use of ** correlograms **for visualizing correlations and mosaic plots for visualizing multivariate relationships among categorical variables.

Scatter plots

The basic function for creating a scatter plot in R is plot(x, y), where x and y are numeric vectors denoting the (x, y) points to plot.

# A scatter plot with best fit lines # attach(mtcars) plot(wt, mpg,, xlab="Car Weight (lbs/1000)",, pch=19) abline(lm(mpg~wt),, lwd=2, lty=1) lines(lowess(wt,mpg),, lwd=2, lty=2)

# R has two functions for producing lowess fits: lowess() and loess().The loess() function is a newer, formula-based version of lowess() and is more powerful. The two functions have different defaults, so be careful not to confuse them. #

The scatterplot() function in the car package offers many enhanced features and convenience functions for producing scatter plots, including fit lines, marginal box plots, confidence ellipses, plotting by subgroups, and interactive point identification.

library(car)

## Warning: package 'car' was built under R version 3.2.5

scatterplot(mpg ~ wt | cyl, data=mtcars, lwd=2, , , , legend.plot=TRUE, id.method="identify", labels=row.names(mtcars), )

## Warning in smoother(.x[subs], .y[subs], col = col[i], log.x = ## logged("x"), : could not fit smooth

### Scatter plot matrices

pairs(~mpg+disp+drat+wt, data=mtcars, main="Basic Scatter Plot Matrix")

### All the variables on the right of the ~ are included in the plot

pairs(~mpg+disp+drat+wt, data=mtcars,upper.panel=NULL, main="Basic Scatter Plot Matrix")

library(car) scatterplotMatrix(~ mpg + disp + drat + wt, data=mtcars, spread=FALSE, lty.smooth=2,)

library(car) scatterplotMatrix(~ mpg + disp + drat + wt | cyl, data=mtcars, spread=FALSE,, main="Scatter Plot Matrix via car Package")

library(car) scatterplotMatrix(~ mpg + disp + drat + wt | cyl, data=mtcars, spread=FALSE,,by.groups = TRUE, main="Scatter Plot Matrix via car Package")

## Warning in smoother(x[subs], y[subs], col = col[i], log.x = FALSE, log.y = ## FALSE, : could not fit smooth ## Warning in smoother(x[subs], y[subs], col = col[i], log.x = FALSE, log.y = ## FALSE, : could not fit smooth ## Warning in smoother(x[subs], y[subs], col = col[i], log.x = FALSE, log.y = ## FALSE, : could not fit smooth

### High-density scatter plots

set.seed(1234) n <- 10000 c1 <- matrix(rnorm(n, mean=0, sd=.5), ncol=2) c2 <- matrix(rnorm(n, mean=3, sd=2), ncol=2) mydata <- rbind(c1, c2) mydata <- as.data.frame(mydata) names(mydata) <- c("x", "y") # with(mydata, plot(x, y, pch=19,))

with(mydata, smoothScatter(x, y,))

Using a different approach, the hexbin() function in the hexbin package provides bivariate binning into hexagonal cells (it looks better than it sounds).

library(hexbin)

## Warning: package 'hexbin' was built under R version 3.2.5

with(mydata, { bin <- hexbin(x, y, xbins=50) plot(bin,) })

Finally, the iplot() function in the IDPmisc package can be used to display density (the number of data points at a specific spot) using color like a Heat Map-

library(IDPmisc)

## Warning: package 'IDPmisc' was built under R version 3.2.5

## Loading required package: grid

## Loading required package: lattice

with(mydata, iplot(x, y,))

# # Another example (this time using ipairs function) from IDPmisc package # ipairs(mydata, ztransf=function(x){x[x<1] <- 1; log2(x)})

# # ipairs(mtcars[,1:5],pixs=2)

## 3D scatter plots

par(mfrow=c(1,1)) library(scatterplot3d) attach(mtcars) scatterplot3d(wt, disp, mpg,)

# The scatterplot3d() function offers many options, including the ability to specify symbols, axes, colors, lines, grids, highlighting, and angles. # scatterplot3d(wt, disp, mpg, pch=16, highlight.3d=TRUE, , )

# # Let us add a regression plane. # s3d <-scatterplot3d(wt, disp, mpg, pch=16, highlight.3d=TRUE,, main="3D Scatter Plot with Vertical Lines and Regression Plane") fit <- lm(mpg ~ wt+disp) s3d$plane3d(fit)

The graph allows you to visualize the prediction of miles per gallon from automobile weight and displacement using a multiple regression equation. The plane represents the predicted values, and the points are the actual values. The vertical distances from the plane to the points are the residuals. Points that lie above the plane are underpredicted, while points that lie below the line are over-predicted.

### SPINNING 3D SCATTER PLOTS

library(rglwidget)

## Warning: package 'rglwidget' was built under R version 3.2.5

library(knitr)

## Warning: package 'knitr' was built under R version 3.2.5

library(rgl)

## Warning: package 'rgl' was built under R version 3.2.5

knit_hooks$set(webgl = hook_webgl)

list.of.packages <- c("rgl","knitr","rglwidget") lapply(list.of.packages,function(x){library(x,character.only=TRUE)})

## [[1]] ## [1] "rgl" "knitr" "rglwidget" "scatterplot3d" ## [5] "IDPmisc" "lattice" "grid" "hexbin" ## [9] "car" "stats" "graphics" "grDevices" ## [13] "utils" "datasets" "methods" "base" ## ## [[2]] ## [1] "rgl" "knitr" "rglwidget" "scatterplot3d" ## [5] "IDPmisc" "lattice" "grid" "hexbin" ## [9] "car" "stats" "graphics" "grDevices" ## [13] "utils" "datasets" "methods" "base" ## ## [[3]] ## [1] "rgl" "knitr" "rglwidget" "scatterplot3d" ## [5] "IDPmisc" "lattice" "grid" "hexbin" ## [9] "car" "stats" "graphics" "grDevices" ## [13] "utils" "datasets" "methods" "base"

attach(mtcars)

## The following objects are masked from mtcars (pos = 6): ## ## am, carb, cyl, disp, drat, gear, hp, mpg, qsec, vs, wt

## The following objects are masked from mtcars (pos = 13): ## ## am, carb, cyl, disp, drat, gear, hp, mpg, qsec, vs, wt

plot3d(wt, disp, mpg,, size=5) # # # User can rotate the image by clicking and dragging with the mouse, and zoom in and out with the scroll wheel. # plot3d(mtcars$wt, mtcars$disp, mtcars$mpg,, size=1, lit=TRUE, main = "Car Weight Vs Engine Displacement Vs Mileage",sub="3-D Plot") # # Add some more parameters # plot3d(mtcars$wt, mtcars$disp, mtcars$mpg,, size=5, lit=FALSE,box=FALSE, col = c("red","blue","green"),expand = 1, main = "Car Weight Vs Engine Displacement Vs Mileage",sub="3-D Plot", xlab = "Weight",,zlab = "Mileage") # # # Add bounding box decoration rgl.bbox(color=c("#333377","black"),, , shininess=5, alpha=0.8, nticks = 3 ) # # # Change the axis aspect ratios # aspect3d(1,1,1) # # In order to Animate this 3D plot, you may use the following commands which would automatically roate the plot # # play3d(spin3d()) # This has been commented as it would otherwise not allow to move ahead with script execution # # Spin on x-axis, at 4 rpm, for 20 seconds # # play3d(spin3d(axis=c(1,0,0), rpm=4), duration=20) #

For more details on 3-D graphs using ‘rgl’ package, refer this link - 3-Dimensional (3-D) Visualization using R.

- 04/01/2017
- 908
- 0 Like

## R : Graphics Tutorial Series ( Part 3 )

- 04/01/2017
- 908
- 0 Like

#### Ankit Agarwal

Analytics Manager - Deloitte Advisory at Deloitte

Opinions expressed by Grroups members are their own.

#### Top Authors

Ad: 50000 Data Science Jobs Globally | Over 10000 Hours of Free Data Science Video Tutorials:- Register Now

In this first part of the Tutorial Series *R : Graphics Tutorial Series ( Part 1 )* , we learnt the basics of R Base Graphics while in the second part of the Series R : Graphics Tutorial Series ( Part 2 ) we saw various graphical methods for displaying relationships between two variables (bivariate relationships) and between many variables (multivariate relationships).

This is an extension of the Part 2 where we will start with scatter plots and scatter plot matrices. Then, we’ll explore line charts of various types. These approaches are well known and widely used in research. Next, we’ll review the use of ** correlograms **for visualizing correlations and mosaic plots for visualizing multivariate relationships among categorical variables.

Scatter plots

The basic function for creating a scatter plot in R is plot(x, y), where x and y are numeric vectors denoting the (x, y) points to plot.

# A scatter plot with best fit lines # attach(mtcars) plot(wt, mpg,, xlab="Car Weight (lbs/1000)",, pch=19) abline(lm(mpg~wt),, lwd=2, lty=1) lines(lowess(wt,mpg),, lwd=2, lty=2)

# R has two functions for producing lowess fits: lowess() and loess().The loess() function is a newer, formula-based version of lowess() and is more powerful. The two functions have different defaults, so be careful not to confuse them. #

The scatterplot() function in the car package offers many enhanced features and convenience functions for producing scatter plots, including fit lines, marginal box plots, confidence ellipses, plotting by subgroups, and interactive point identification.

library(car)

## Warning: package 'car' was built under R version 3.2.5

scatterplot(mpg ~ wt | cyl, data=mtcars, lwd=2, , , , legend.plot=TRUE, id.method="identify", labels=row.names(mtcars), )

## Warning in smoother(.x[subs], .y[subs], col = col[i], log.x = ## logged("x"), : could not fit smooth

### Scatter plot matrices

pairs(~mpg+disp+drat+wt, data=mtcars, main="Basic Scatter Plot Matrix")

### All the variables on the right of the ~ are included in the plot

pairs(~mpg+disp+drat+wt, data=mtcars,upper.panel=NULL, main="Basic Scatter Plot Matrix")

library(car) scatterplotMatrix(~ mpg + disp + drat + wt, data=mtcars, spread=FALSE, lty.smooth=2,)

library(car) scatterplotMatrix(~ mpg + disp + drat + wt | cyl, data=mtcars, spread=FALSE,, main="Scatter Plot Matrix via car Package")

library(car) scatterplotMatrix(~ mpg + disp + drat + wt | cyl, data=mtcars, spread=FALSE,,by.groups = TRUE, main="Scatter Plot Matrix via car Package")

## Warning in smoother(x[subs], y[subs], col = col[i], log.x = FALSE, log.y = ## FALSE, : could not fit smooth ## Warning in smoother(x[subs], y[subs], col = col[i], log.x = FALSE, log.y = ## FALSE, : could not fit smooth ## Warning in smoother(x[subs], y[subs], col = col[i], log.x = FALSE, log.y = ## FALSE, : could not fit smooth

### High-density scatter plots

set.seed(1234) n <- 10000 c1 <- matrix(rnorm(n, mean=0, sd=.5), ncol=2) c2 <- matrix(rnorm(n, mean=3, sd=2), ncol=2) mydata <- rbind(c1, c2) mydata <- as.data.frame(mydata) names(mydata) <- c("x", "y") # with(mydata, plot(x, y, pch=19,))

with(mydata, smoothScatter(x, y,))

Using a different approach, the hexbin() function in the hexbin package provides bivariate binning into hexagonal cells (it looks better than it sounds).

library(hexbin)

## Warning: package 'hexbin' was built under R version 3.2.5

with(mydata, { bin <- hexbin(x, y, xbins=50) plot(bin,) })

Finally, the iplot() function in the IDPmisc package can be used to display density (the number of data points at a specific spot) using color like a Heat Map-

library(IDPmisc)

## Warning: package 'IDPmisc' was built under R version 3.2.5

## Loading required package: grid

## Loading required package: lattice

with(mydata, iplot(x, y,))

# # Another example (this time using ipairs function) from IDPmisc package # ipairs(mydata, ztransf=function(x){x[x<1] <- 1; log2(x)})

# # ipairs(mtcars[,1:5],pixs=2)

## 3D scatter plots

par(mfrow=c(1,1)) library(scatterplot3d) attach(mtcars) scatterplot3d(wt, disp, mpg,)

# The scatterplot3d() function offers many options, including the ability to specify symbols, axes, colors, lines, grids, highlighting, and angles. # scatterplot3d(wt, disp, mpg, pch=16, highlight.3d=TRUE, , )

# # Let us add a regression plane. # s3d <-scatterplot3d(wt, disp, mpg, pch=16, highlight.3d=TRUE,, main="3D Scatter Plot with Vertical Lines and Regression Plane") fit <- lm(mpg ~ wt+disp) s3d$plane3d(fit)

The graph allows you to visualize the prediction of miles per gallon from automobile weight and displacement using a multiple regression equation. The plane represents the predicted values, and the points are the actual values. The vertical distances from the plane to the points are the residuals. Points that lie above the plane are underpredicted, while points that lie below the line are over-predicted.

### SPINNING 3D SCATTER PLOTS

library(rglwidget)

## Warning: package 'rglwidget' was built under R version 3.2.5

library(knitr)

## Warning: package 'knitr' was built under R version 3.2.5

library(rgl)

## Warning: package 'rgl' was built under R version 3.2.5

knit_hooks$set(webgl = hook_webgl)

list.of.packages <- c("rgl","knitr","rglwidget") lapply(list.of.packages,function(x){library(x,character.only=TRUE)})

## [[1]] ## [1] "rgl" "knitr" "rglwidget" "scatterplot3d" ## [5] "IDPmisc" "lattice" "grid" "hexbin" ## [9] "car" "stats" "graphics" "grDevices" ## [13] "utils" "datasets" "methods" "base" ## ## [[2]] ## [1] "rgl" "knitr" "rglwidget" "scatterplot3d" ## [5] "IDPmisc" "lattice" "grid" "hexbin" ## [9] "car" "stats" "graphics" "grDevices" ## [13] "utils" "datasets" "methods" "base" ## ## [[3]] ## [1] "rgl" "knitr" "rglwidget" "scatterplot3d" ## [5] "IDPmisc" "lattice" "grid" "hexbin" ## [9] "car" "stats" "graphics" "grDevices" ## [13] "utils" "datasets" "methods" "base"

attach(mtcars)

## The following objects are masked from mtcars (pos = 6): ## ## am, carb, cyl, disp, drat, gear, hp, mpg, qsec, vs, wt

## The following objects are masked from mtcars (pos = 13): ## ## am, carb, cyl, disp, drat, gear, hp, mpg, qsec, vs, wt

plot3d(wt, disp, mpg,, size=5) # # # User can rotate the image by clicking and dragging with the mouse, and zoom in and out with the scroll wheel. # plot3d(mtcars$wt, mtcars$disp, mtcars$mpg,, size=1, lit=TRUE, main = "Car Weight Vs Engine Displacement Vs Mileage",sub="3-D Plot") # # Add some more parameters # plot3d(mtcars$wt, mtcars$disp, mtcars$mpg,, size=5, lit=FALSE,box=FALSE, col = c("red","blue","green"),expand = 1, main = "Car Weight Vs Engine Displacement Vs Mileage",sub="3-D Plot", xlab = "Weight",,zlab = "Mileage") # # # Add bounding box decoration rgl.bbox(color=c("#333377","black"),, , shininess=5, alpha=0.8, nticks = 3 ) # # # Change the axis aspect ratios # aspect3d(1,1,1) # # In order to Animate this 3D plot, you may use the following commands which would automatically roate the plot # # play3d(spin3d()) # This has been commented as it would otherwise not allow to move ahead with script execution # # Spin on x-axis, at 4 rpm, for 20 seconds # # play3d(spin3d(axis=c(1,0,0), rpm=4), duration=20) #

For more details on 3-D graphs using ‘rgl’ package, refer this link - 3-Dimensional (3-D) Visualization using R.

- 04/01/2017
- 908
- 0 Like

## Ankit Agarwal

Analytics Manager - Deloitte Advisory at Deloitte

Opinions expressed by Grroups members are their own.