Graphics as a Computation

A general definition is that a computation is a transformation from inputs to outputs. Let's explore one kind of computation that's important in science: the construction of graphics from data.

The inputs are going to be information on which the graphics are to be based, the output is a graphic. You'll have to be thoughtful, though, about what “information” means and what a “graphic” is.

You might think about a graphic as “ink on a page.” Behind the “ink” is data as interpreted by software. The data might be in the form of a graphics or image file, perhaps in 'jpg' or 'png' or 'svg' or 'pdf' format. The computation to produce the graphic is then a transformation from the data you want to plot to corresponding data in the graphics file format. Printers are machines that transform an input, the graphics file, into an output: actual ink on the page. The printer is programmed for this transformation. Similarly, computer operating systems provide software to carry out the transformation of the graphics file into the arrangement of pixels on the computer screen; displaying a file is a computaton, too.

An important type of input involves HTML and JavaScript. This is the kind of input that is transformed into images and text on the screen by web browsers such as Chrome, Firefox, Safari, and Internet Explorer. HTML/JavaScript is important because it provides a way to tie together different types of information for presentation and interaction.

The computation you're seeing here is one from a format called “R/Markdown” into HTML/JavaScript. The HTML/JavaScript is very difficult for a person to write directly. In contrast, the R/Markdown information is in a format that's pretty easy to read and write. Transformation from R/Markdown to HTML/JavaScript is provided by a suite of programs that have been organized for this purpose.

So far, the R/Markdown content has been simple text — you can likely figure out how it works without any study or training.

The content to generate graphics is more obscure. Learning this will be harder and will require some systematic study in a graduated way. But the content itself, once it's produced by a programmer, is easily transformed into an output.

By way of example, the following graphics commands use the rCharts package written by Ramnath Vaidyanathan at McGill University. There are many other ways to generate graphics output. rCharts was designed to produce graphics in a format that makes them interactive within a web browser.

Play with the following graphics to try out their interactivity. In addition to being able to interact with the graphics themselves using a mouse, you can see the commands that were used to generate them. Don't expect to understand those commands right now. But eventually, you'll be able to do so and to construct your own sets of commands that get the computer to do the work you want it to do.


A famous dataset in statistics is Edgar Anderson's iris data, collected in 1935 and published by RA Fisher in 1936 in an article, “The use of multiple measurements in taxonomic problems”.

## Example 1 Facetted Scatterplot
names(iris) = gsub("\\.", "", names(iris))
r1 <- rPlot(SepalLength ~ SepalWidth | Species, data = iris, color = 'Species', type = 'point')

## Example 2 Facetted Barplot
hair_eye = as.data.frame(HairEyeColor)
r2 <- rPlot(Freq ~ Hair | Eye, color = 'Eye', data = hair_eye, type = 'bar')

data(economics, package = 'ggplot2')
econ <- transform(economics, date = as.character(date))
m1 <- mPlot(x = 'date', y = c('psavert', 'uempmed'), type = 'Line',
  data = econ)
m1$set(pointSize = 0, lineWidth = 1)

hair_eye_male <- subset(as.data.frame(HairEyeColor), Sex == "Male")
n1 <- nPlot(Freq ~ Hair, group = "Eye", data = hair_eye_male, 
  type = 'multiBarChart')