Learn R Programming

ggthemes (version 2.1.0)

bank_slopes: Bank Slopes to 45 degrees

Description

Calculate the optimal aspect ratio of a line graph by banking the slopes to 45 degrees as suggested by W.S. Cleveland. This maximizes the ability to visually differentiate differences in slope. This function will calculate the optimal aspect ratio for a line plot using any of the methods described in Herr and Argwala (2006). In their review of the methods they suggest using median absolute slope banking ("ms"), which produces aspect ratios which are generally the median of the various methods provided here.

Usage

bank_slopes(x, y, cull = FALSE, method = "ms", weight = TRUE, ...)

Arguments

x
x values
y
y values
cull
logical. Remove all slopes of 0 or Inf.
method
One of "ms" (Median Absolute Slope), "as" (Average Absolute Slope), "ao" (Average Orientation), "lor" (Local Orientation Resolution), "gor" (Global Orientation Resolution).
weight
logical. Weight line segments by their length. Only used when method="ao".
...
Passed to nlm in methods "ao", "lor" and "gor".

Value

  • numeric The aspect ratio (x , y).

Methods

As written, all of these methods calculate the aspect ratio (x /y), but bank_slopes will return (y / x) to be compatible with link[ggplot2]{coord_fixed}.

Median Absolute Slopes Banking

Let the aspect ratio be $\alpha = \frac{w}{h}$ then the median absolute slop banking is the $\alpha$ such that, $$median \left| \frac{s_i}{\alpha} \right| = 1$$

Let $R_z = z_{max} - z_{min}$ for $z = x, y$, and $M = median \| s_i \|$. Then, $$\alpha = M \frac{R_x}{R_y}$$

Average-Absolute-Orientation Banking

This method finds the aspect ratio by setting the average orientation to 45 degrees. For an aspect ratio $\alpha$, let the orientation of a line segment be $\theta_i(\alpha) = \arctan(s_i / \alpha)$.

$$\frac{ \sum_i \theta_i(\alpha) l_i}{\sum_i l_i} = \frac{\pi}{4} rad$$ where $l_i = 1$ if unweighted, and $l_i = \sqrt{x_i^2 + y_i^2}$ (length of the line segment), if weighted. The value of $\alpha$ is found with nlm.

Average Absolute Slope Banking

Let the aspect ratio be $\alpha = \frac{w}{h}$. then the mean absolute slope banking is the $\alpha$ such that, $$mean \left| \frac{s_i}{\alpha} \right| = 1$$

Let $R_z = z_{max} - z_{min}$ for $z = x, y$, and $M = mean \| s_i \|$. Then, $$\alpha = M R_x / R_y$$

Banking by Optimizing Orientation Resolution

The angle between line segments i and j is $r_{i,j} = \|\theta_i(\alpha) - \theta_j(\alpha)\|$, where $\theta_i(\alpha) = \arctan(s_i / \alpha)$ and $s_i$ is the slope of line segment i. This function finds the $\alpha$ that maximizes the sum of the angles between all pairs of line segments.

$$\max_{\alpha} \sum_i \sum_{j: j < 1} r_{i,j}$$

The local optimization only includes line-segments that are next to each other. Suppose there are n line segments, then the local orientation orientation has the following objective function.

$$\max_{\alpha} \sum_{i=2}^{n} r_{i,i-1}$$

References

Cleveland, W. S., M. E. McGill, and R. McGill. The Shape Parameter of a Two-Variable Graph. Journal of the American Statistical Association, 83:289-300, 1988

Heer, Jeffrey and Maneesh Agrawala, 2006. "Multi-Scale Banking to 45" IEEE Transactions On Visualization And Computer Graphics.

Cleveland, W. S. 1993. "A Model for Studying Display Methods of Statistical Graphs." Journal of Computational and Statistical Graphics.

Cleveland, W. S. 1994. The Elements of Graphing Data, Revised Edition.

See Also

banking

Examples

Run this code
# Use the classic sunspot data from Cleveland's orig paper
x <- seq_along(sunspot.year)
y <- as.numeric(sunspot.year)
# Without banking
m <- qplot(x, y, geom="line")
m

## Using the default method, Median Absolute Slope
ratio <- bank_slopes(x, y)
m + coord_fixed(ratio = ratio)

## Alternative methods to calculate the banking
bank_slopes(x, y, method="ms")
## Using culling
bank_slopes(x, y, method="ms", cull=TRUE)
## Average Absolute Slope
bank_slopes(x, y, method="as")
bank_slopes(x, y, method="as", cull=TRUE)
## Average Orientation
bank_slopes(x, y, method="ao")
bank_slopes(x, y, method="ao", cull=TRUE)
## Average Orientation (Weighted)
bank_slopes(x, y, method="ao", weight=TRUE)
bank_slopes(x, y, method="ao", cull=TRUE, weight=TRUE)
## Global Orientation Resolution
bank_slopes(x, y, method="gor")
bank_slopes(x, y, method="gor", cull=TRUE)
## Local Orientation Resolution
bank_slopes(x, y, method="lor")
bank_slopes(x, y, method="lor", cull=TRUE)

Run the code above in your browser using DataLab