Digital humanities


Maintained by: David J. Birnbaum (djbpitt@gmail.com) [Creative Commons BY-NC-SA 3.0 Unported License] Last modified: 2012-12-26T22:42:22+0000


Sample R script to generate scatterplot from CSV input

Procedure

The procedure for using R to calculate and graph statistical and other quantitative reports involves the following steps:

  1. Prepare the data in a form that R understands. We use comma separated values (CSV) below. XML data can be converted to CSV with XSLT (set the output method to text).
  2. Run an R process to generate the output. In the example below we generate a scatterplot in SVG using standard R functions and save it to disk. See below for a discussion of other options.
  3. The R code below generates valid SVG that begins with an XML declaration. This is fine if the file is going to be viewed in stand-alone mode, but if it is to be imported into, say, an XHTML document, the XML declaration will have to be removed, since XML permits only one XML declaration in a document, and it must be at the very beginning. There appears to be no option within R to suppress the generation of the XML declaration, so it needs to be removed after the fact.
  4. The SVG can then be rendered directly or included in another document. This page uses a server side include (SSI) to render the SVG below. XHTML documents that include namespaced SVG documents directly should have the extension xhtml, rather than html, to ensure that the mime type will be reported correctly by the server.

Notes

R script to generate and write the scatterplot

stuff<-read.csv(file.choose(),header=TRUE)
# or full path to filename in place of file.choose() function
attach(stuff)
stuff$Year <- stuff$Year * rep(-1, 31) # years are BC, so make them negative
library(car)
svg("r-transparent.svg",bg="transparent",width=5,height=5)
scatterplot(Word.Count~Year)
dev.off() # file will be saved in working directory (no screen display)

Source CSV for preceding example

(downloaded from http://daedalus.umkc.edu/StatisticalMethods/datasets/GreekDramaLength.csv)

Genre,Author,Play,Year,Doc ID,Word Count
Tragedy,Euripides,Cyclops,438,1999.01.0093,4104
Tragedy,Aeschylus,The Suppliants,463,1999.01.0015,4939
Tragedy,Aeschylus,The Seven Against Thebes,467,1999.01.0013,5115
Tragedy,Aeschylus,The Persians,472,1999.01.0011,5189
Tragedy,Aeschylus,Eumenides,458,1999.01.0005,5297
Tragedy,Euripides,The Suppliants,421,1999.01.0121,5426
Tragedy,Aeschylus,Libation Bearers,458,1999.01.0007,5447
Tragedy,Euripides,Heracleidae,428,1999.01.0103,6240
Tragedy,Euripides,Alcestis,438,1999.01.0087,6603
Tragedy,Euripides,Trojan Women,415,1999.01.0123,7077
Tragedy,Sophocles,Ajax,451,1999.01.0183,7177
Tragedy,Euripides,Hecuba,425,1999.01.0097,7279
Tragedy,Sophocles,Electra,409,1999.01.0187,7363
Tragedy,Euripides,Andromanche,427,1999.01.0089,7398
Tragedy,Euripides,Bacchae,405,1999.01.0091,7597
Tragedy,Euripides,Electra,413,1999.01.0095,7672
Tragedy,Euripides,Heracles,422,1999.01.0101,7902
Tragedy,Sophocles,Antigone,441,1999.01.0185,7914
Tragedy,Euripides,Medea,431,1999.01.0113,8032
Tragedy,Euripides,Hippolytus,428,1999.01.0105,8157
Tragedy,Aeschylus,Agamemnon,458,1999.01.0003,8187
Tragedy,Euripides,Iphigenia in Tauris,413,1999.01.0111,8396
Tragedy,Sophocles,Oedipus at Colonus,401,1999.01.0189,8702
Tragedy,Sophocles,The Trachiniae,409,1999.01.0195,8830
Tragedy,Euripides,Ion,417,1999.01.0109,9240
Tragedy,Sophocles,Philoctetes,409,1999.01.0193,9280
Tragedy,Euripides,Iphigenia in Aulis,405,1999.01.0107,9430
Tragedy,Euripides,Phoenician Women,410,1999.01.0117,9879
Tragedy,Euripides,Helena,412,1999.01.0099,9927
Tragedy,Euripides,Orestes,408,1999.01.0115,10030
Tragedy,Sophocles,Oedipus Rex,409,1999.01.0191,10385

Output of script