slopegraph: Create Slopegraph from a data frame

Description Usage Arguments Value Author(s) References See Also Examples

View source: R/slopegraph.R

Description

Convert an R data frame (containing a panel dataset, where rows are observations and columns are time periods) into an Edward Tufte-inspired Slopegraph.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
slopegraph(data, main = NULL, xlab = "", ylab = "",
  xlabels = names(data), xlim = c(0.5, ncol(data) + 0.5),
  ylim = c(min(data, na.rm = TRUE) - diff(range(data, na.rm = TRUE))/100,
  max(data, na.rm = TRUE) + diff(range(data, na.rm = TRUE))/100), bty = "n",
  xaxt = "n", yaxt = "n", panel.first = NULL, panel.last = NULL,
  labpos.left = 2, labpos.right = 4, leftlabels = NULL,
  rightlabels = NULL, decimals = 0L, col.lines = par("fg"),
  col.lab = col.lines, col.num = col.lines, col.xaxt = par("fg"),
  offset.x = NULL, offset.lab = 0.1, cex.lab = 1, cex.num = 1,
  family = "serif", font.lab = 1, font.num = 1, lty = par("lty"),
  lwd = par("lwd"), mai = NULL, na.span = FALSE, ...)

Arguments

data

An observation-by-period data.frame, with at least two columns. Missing values are allowed.

main

The main title of the plot. Default is NULL (none). See par.

xlab

The x-axis label for the plot. Default is '' (none). See par.

ylab

The y-axis label for the plot. Default is '' (none). See par.

xlabels

The labels to use for the slopegraph periods. Default is names(data).

xlim

The x-axis limits of the plot. Automatically calculated by default.

ylim

The y-axis limits of the plot. Automatically calculated by default.

bty

The box type for the plot. Default is 'n' (none). See par.

xaxt

The x-axis type for the plot. Default is 'n' (none). See par.

yaxt

The y-axis type for the plot. Default is 'n' (none). See par.

panel.first

An expression to add something between drawing the blank canvas and adding the plot content (i.e., behind the slopegraph). Default is NULL.

panel.last

An expression to add something after adding the plot content. Default is NULL.

labpos.left

The pos (positioning) parameter for the leftside observation labels. Default is 2. See par. If NULL, labels are not drawn.

labpos.right

The pos (positioning) parameter for the rightside observation labels. Default is 2. See par. If NULL, labels are not drawn.

leftlabels

The parameter for the rightside observation labels. Default is using row indexes.

rightlabels

The parameter for the rightside observation labels. Default is using row indexes.

decimals

The number of decimals to display for values in the plot. Default is 0 (none).

col.lines

A vector of colors for the slopegraph lines. Default is par('fg').

col.lab

A vector of colors for the observation labels. Default is par('fg').

col.num

A vector of colors for the number values. Default is par('fg'). If NULL, labels are not drawn.

col.xaxt

A character string containing the x-axis color. Default is par('fg').

offset.x

A small offset for segments, to be used when positioning the numeric values. Default is NULL (set automatically based on the data.

offset.lab

A small offset for the observation labels. Default is .1.

cex.lab

A numeric value indicating the size of row labels. Default is 1. See par.

cex.num

A numeric value indicating the size of numeric labels. Default is 1. See par.

family

The font family to use in the plots. Set to “serif” by default.

font.lab

Default is 1. See ? text.

font.num

Default is 1. See ? text.

lty

A vector of line type values for the slopegraph lines. Default is par("lty"). See par.

lwd

A vector of line width values for the slopegraph lines. Default is par("lwd"). See par.

mai

A margin specification. Default is NULL. See par.

na.span

A logical indicating whether line segments should span periods with missing values. The default is FALSE, such that some segments are not drawn.

...

Additional arguments to plot.

Value

A five-variable data frame, where each row contains: the row number from data, x1, x2, y1, and y2 coordinates for each drawn segment, invisibly.

Author(s)

Thomas J. Leeper

References

http://www.edwardtufte.com/bboard/q-and-a-fetch-msg?msg_id=0003nk

Tufte, Edward. The Visual Display of Quantitative Information. Graphics Press.

Tufte, Edward. Beautiful Evidence. Graphics Press.

See Also

cancer, gdp, states For a ggplot2 version, use ggslopegraph.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
## Tufte's Cancer Graph (to the correct scale)
data(cancer)
slopegraph(cancer, col.lines = 'gray', xlim = c(-.5,5.5),
           xlabels = c('5 Year','10 Year','15 Year','20 Year'))

## Tufte's GDP Graph
data(gdp)
slopegraph(gdp, col.line='gray', xlabels = c('1970','1979'), 
    main = 'Current Receipts of Goverment as a Percentage of Gross Domestic Product')

## Ranking of U.S. State populations
data(states)
slopegraph(states, col.line='black', ylim = c(38,0),
           main = 'Relative Rank of U.S. State Populations, 1790-1870')

leeper/slopegraph documentation built on May 21, 2019, 1:39 a.m.