| 1 | # ClevelandPlotForDataframe.r
|
|---|
| 2 | #
|
|---|
| 3 | # Copyright (C) 2011 Jason J. Roberts
|
|---|
| 4 | #
|
|---|
| 5 | # This program is free software; you can redistribute it and/or
|
|---|
| 6 | # modify it under the terms of the GNU General Public License
|
|---|
| 7 | # as published by the Free Software Foundation; either version 2
|
|---|
| 8 | # of the License, or (at your option) any later version.
|
|---|
| 9 | #
|
|---|
| 10 | # This program is distributed in the hope that it will be useful,
|
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 13 | # GNU General Public License (available in the file LICENSE.TXT)
|
|---|
| 14 | # for more details.
|
|---|
| 15 | #
|
|---|
| 16 | # You should have received a copy of the GNU General Public License
|
|---|
| 17 | # along with this program; if not, write to the Free Software
|
|---|
| 18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|---|
| 19 |
|
|---|
| 20 | ClevelandPlot <- function(df, transforms=NULL)
|
|---|
| 21 | {
|
|---|
| 22 | library(stats)
|
|---|
| 23 |
|
|---|
| 24 | # Apply the transforms (if any).
|
|---|
| 25 |
|
|---|
| 26 | labs <- names(df)
|
|---|
| 27 |
|
|---|
| 28 | if (!is.null(transforms))
|
|---|
| 29 | for (i in 1:length(names(df)))
|
|---|
| 30 | {
|
|---|
| 31 | name <- names(df)[[i]]
|
|---|
| 32 | if (name %in% names(transforms))
|
|---|
| 33 | {
|
|---|
| 34 | d <- df[[name]]
|
|---|
| 35 | df[[name]] <- eval(parse(text=as.character(transforms[[name]])))
|
|---|
| 36 | labs[i] <- deparse(eval(parse(text=paste("substitute(", transforms[[name]], ", list(d = as.name(\"", name, "\")))", sep=""))))
|
|---|
| 37 | }
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | # Open a graphics device for a suitable number of plots.
|
|---|
| 41 |
|
|---|
| 42 | numPlots <- length(names(df))
|
|---|
| 43 | par(mfrow=c(ceiling(numPlots / ceiling(sqrt(numPlots))), ceiling(sqrt(numPlots))), mar=c(2, 0, 2, 0))
|
|---|
| 44 |
|
|---|
| 45 | # Create the plots.
|
|---|
| 46 |
|
|---|
| 47 | for (i in 1:length(names(df)))
|
|---|
| 48 | {
|
|---|
| 49 | dotchart(df[[names(df)[[i]]]], lcolor=NULL, bg="black", main=labs[i])
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | ClevelandPlotToFile <- function(filename, df, transforms, res=1000.0, width=3000.0, height=3000.0, pointSize=10.0, bg="white")
|
|---|
| 54 | {
|
|---|
| 55 | if (tolower(substr(filename, nchar(filename) - 3, nchar(filename))) == ".emf")
|
|---|
| 56 | win.metafile(filename, width=width, height=height, pointsize=pointSize)
|
|---|
| 57 | else
|
|---|
| 58 | png(filename, res=res, width=width, height=height, pointsize=pointSize, bg=bg)
|
|---|
| 59 |
|
|---|
| 60 | tryCatch(ClevelandPlot(df, transforms), finally=dev.off())
|
|---|
| 61 | }
|
|---|