Learn R Programming

depower (version 2025.1.20)

plot.depower: Plot power objects

Description

An automatic plot method for objects returned by power().

Usage

# S3 method for depower
plot(
  x,
  x_axis = NULL,
  y_axis = NULL,
  color = NULL,
  facet_row = NULL,
  facet_col = NULL,
  hline = NULL,
  caption = TRUE,
  caption_width = 70L,
  ...
)

Value

A ggplot2::ggplot() object.

Arguments

x

(depower)
The data frame returned by power().

x_axis

(string: NULL; names(x))
The name of the column to be used for the x-axis. Automatically chosen if NULL.

y_axis

(string: NULL; names(x))
The name of the column to be used for the y-axis. Automatically chosen if NULL. Generally, "power" (default) should be used for the y-axis.

color

(string: NULL; names(x))
The name of the column to be used for the ggplot2::aes() color aesthetic. Automatically chosen if NULL. Use NA to turn off.

facet_row

(string: NULL; names(x))
The name of the column to be used for the ggplot2::facet_grid() row. Automatically chosen if NULL. Use NA to turn off.

facet_col

(string: NULL; names(x))
The name of the column to be used for the ggplot2::facet_grid() column. Automatically chosen if NULL. Use NA to turn off.

hline

(numeric: NULL; (0, 1))
The y-intercept at which to draw a horizontal line.

caption

(Scalar logical: TRUE)
If TRUE (default), a caption is added to the plot. The caption includes information on parameter values that were conditioned on to generate the plot. If FALSE, the caption is not included.

caption_width

(Scalar integer: 70L)
The target column number for wrapping the caption text.

...

Unused additional arguments.

Details

If you are limited by the output from plot.depower(), keep in mind that the object returned by power() is a standard data frame. This allows you to easily plot all results with standard plotting functions. In addition, because plot.depower() uses ggplot2, you can modify the plot as you normally would. For example:

set.seed(1234)
sim_log_lognormal(
  n1 = c(10, 15),
  n2 = c(10, 15),
  ratio = c(1.3, 1.5),
  cv1 = c(0.3),
  cv2 = c(0.3, 0.5),
  nsims = 1000
) |>
  power(alpha = 0.05) |>
  plot(hline = 0.8, caption_width = 60) +
  ggplot2::theme_bw() +
  ggplot2::theme(plot.caption = ggplot2::element_text(hjust = 0)) +
  ggplot2::labs(title = "Power for the ratio of geometric means")

Example of extending plot() with ggplot2 functions

See Also

Examples

Run this code
#----------------------------------------------------------------------------
# plot() examples
#----------------------------------------------------------------------------
library(depower)

# Power for independent two-sample t-test
set.seed(1234)
sim_log_lognormal(
  n1 = c(10, 15),
  n2 = c(10, 15),
  ratio = c(1.3, 1.5),
  cv1 = c(0.3),
  cv2 = c(0.3, 0.5),
  nsims = 500
) |>
  power(alpha = 0.05) |>
  plot()

# Power for dependent two-sample t-test
set.seed(1234)
sim_log_lognormal(
  n1 = c(10, 15),
  n2 = c(10, 15),
  ratio = c(1.3, 1.5),
  cv1 = c(0.3, 0.5),
  cv2 = c(0.3, 0.5),
  cor = c(0.3),
  nsims = 500
) |>
  power(alpha = 0.01) |>
  plot()

# Power for two-sample independent AND two-sample dependent t-test
set.seed(1234)
sim_log_lognormal(
  n1 = c(10, 15),
  n2 = c(10, 15),
  ratio = c(1.3, 1.5),
  cv1 = c(0.3),
  cv2 = c(0.3),
  cor = c(0, 0.3, 0.6),
  nsims = 500
) |>
  power(alpha = c(0.05, 0.01)) |>
  plot(facet_row = "cor", color = "test")

# Power for one-sample t-test
set.seed(1234)
sim_log_lognormal(
  n1 = c(10, 15),
  ratio = c(1.2, 1.4),
  cv1 = c(0.3, 0.5),
  nsims = 500
) |>
  power(alpha = c(0.05, 0.01)) |>
  plot()

# \donttest{
# Power for independent two-sample NB test
set.seed(1234)
sim_nb(
  n1 = c(10, 15),
  mean1 = 10,
  ratio = c(1.8, 2),
  dispersion1 = 10,
  dispersion2 = 3,
  nsims = 100
) |>
  power(alpha = 0.01) |>
  plot()

# Power for BNB test
set.seed(1234)
sim_bnb(
  n = c(10, 12),
  mean1 = 10,
  ratio = c(1.3, 1.5),
  dispersion = 5,
  nsims = 100
) |>
  power(alpha = 0.01) |>
  plot()
# }

Run the code above in your browser using DataLab