ggDualAxis: ggplot dual axis

Description Usage Arguments Author(s) References Examples

View source: R/aveytoolkit_ggDualAxis.R

Description

aveytoolkit_ggDualAxis Takes two ggplot objects and combines them onto a single plot with dual x or y axes.

Usage

1
ggDualAxis(plot1, plot2, which.axis = c("y", "x"))

Arguments

plot1

a ggplot2 object

plot2

a ggplot2 object

which.axis

character vector of length 1 specifying "x" or "y" axis. Default if "y".

Author(s)

Jon Lefcheck

References

https://gist.github.com/jslefche/e4c0e9f57f0af49fca87

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
## Example for x axis
# Create fake data.frame
data.add.x = data.frame(
  y1 = runif(100, 0, 100),
  x1 = runif(100, 0, 100)
)

# Add second x-axis that scales with first
data.add.x$x2 = (data.add.x$x1 + 50)^0.75

# Create plots
plot1.x = qplot(y = y1, x = x1, data = data.add.x)
plot2.x = qplot(y = y1, x = x2, data = data.add.x, col = 2)

# Run function
ggDualAxis(plot1.x, plot2.x, "x")

## Example for y axis
# Add second y-axis that scales with first
data.add.x$y2 = (data.add.x$y^0.5) / 500

# Create plots
plot1.y = qplot(y = y1, x = x1, data = data.add.x)
plot2.y = qplot(y = y2, x = x1, data = data.add.x)

# Run function
ggDualAxis(plot1.y, plot2.y, "y")

stefanavey/aveytoolkit documentation built on March 5, 2020, 12:49 a.m.