by Joseph Rickert
library(rgl) open3d() attach(mtcars) #head(mtcars) plot3d(disp,wt,mpg, col = rainbow(10))
Developers are continuing to build out the infrastructure of web based graphics, and now it is possible to select environments that offer a rich set of features all in one place.
An R user can load a htmlwidgets library and generate a web based plot by calling a function that looks like any other R plotting function. For example, after installing and loading the three.js library, a few lines of code will produce an interactive 3D scatter plot that can be displayed in a webpage, a markdown document or in the RStudio plot window. The following code generates a more contemporary version of the rotating 3D scatterplot.
library(stringr) library(htmltools) install.packages("devtools",repos="http://cran.rstudio.com/") devtools::install_github("bwlewis/rthreejs") library(threejs) data(mtcars) # load the mtcars data set data <- mtcars[order(mtcars$cyl),] #sort the data set for plotting head(data) uv <- tabulate(mtcars$cyl) # figure our how many observarions for each cylindar type col <- c(rep("red",uv),rep("yellow",uv),rep("blue",uv)) #set the colors row.names(mtcars) # see what models of cars are in the data set scatterplot3js(data[,c(3,6,1)], labels=row.names(mtcars), # mousing over a point will show what model car it is size=mtcars$hp/100, # the size of a point maps to horsepower flip.y=TRUE, color=col,renderer="canvas") # point color indicates number of cylindars
This kind of visualization packs a lot of information into a relatively small space. Not only does the ability to rotate the plot produce a satisfying 3 dimensional rendering, but using color, size and mouse movement to convey information provides three additional dimensions.
As exciting as this kind of visualization is, however, I don’t mean to imply that it is somehow going to make static graphics obsolete. Rob Kabacoff's 2012 post using the scatterplot3d package provides an example of a 3D scatterplot of the mtcars data that has a timeless, elegant look and clearly displays the data without distraction.