Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

plotlyM

plotly Multiple


Description

Generates multiple plotly graphics, driven by specs in a data frame

Usage

plotlyM(
  data,
  x = ~x,
  y = ~y,
  xhi = ~xhi,
  yhi = ~yhi,
  htext = NULL,
  multplot = NULL,
  strata = NULL,
  fitter = NULL,
  color = NULL,
  size = NULL,
  showpts = !length(fitter),
  rotate = FALSE,
  xlab = NULL,
  ylab = NULL,
  ylabpos = c("top", "y"),
  xlim = NULL,
  ylim = NULL,
  shareX = TRUE,
  shareY = FALSE,
  height = NULL,
  width = NULL,
  nrows = NULL,
  ncols = NULL,
  colors = NULL,
  alphaSegments = 1,
  alphaCline = 0.3,
  digits = 4,
  zeroline = TRUE
)

Arguments

data

input data frame

x

formula specifying the x-axis variable

y

formula for y-axis variable

xhi

formula for upper x variable limits (x taken to be lower value)

yhi

formula for upper y variable limit (y taken to be lower value)

htext

formula for hovertext variable

multplot

formula specifying a variable in data that when stratified on produces a separate plot

strata

formula specifying an optional stratification variable

fitter

a fitting such as loess that comes with a predict method. Alternatively specify fitter='ecdf' to use an internal function for computing and displaying ECDFs, which moves the analysis variable from the y-axis to the x-axis

color

plotly formula specifying a color variable or e.g. ~ I('black'). To keep colors constant over multiple plots you will need to specify an AsIs color when you don't have a variable representing color groups.

size

plotly formula specifying a symbol size variable or AsIs

showpts

if fitter is given, set to TRUE to show raw data points in addition to smooth fits

rotate

set to TRUE to reverse the roles of x and y, for example to get horizontal dot charts with error bars

xlab

x-axis label. May contain html.

ylab

a named vector of y-axis labels, possibly containing html (see example below). The names of the vector must correspond to levels of the multplot variable. ylab can be unnamed if multplot is not used.

ylabpos

position of y-axis labels. Default is on top left of plot. Specify ylabpos='y' for usual y-axis placement.

xlim

2-vector of x-axis limits, optional

ylim

2-vector of y-axis limits, optional

shareX

specifies whether x-axes should be shared when they align vertically over multiple plots

shareY

specifies whether y-axes should be shared when they align horizontally over multiple plots

height

height of the combined image in pixels

width

width of the combined image in pixels

nrows

the number of rows to produce using subplot

ncols

the number of columns to produce using subplot (specify at most one of nrows,ncols)

colors

the color palette. Leave unspecified to use the default plotly palette

alphaSegments

alpha transparency for line segments (when xhi or yhi is not NA)

alphaCline

alpha transparency for lines used to connect points

digits

number of significant digits to use in constructing hovertext

zeroline

set to FALSE to suppress vertical line at x=0

Details

Generates multiple plotly traces and combines them with plotly::subplot. The traces are controlled by specifications in data frame data plus various arguments. data must contain these variables: x, y, and tracename (if color is not an "AsIs" color such as ~ I('black')), and can contain these optional variables: xhi, yhi (rows containing NA for both xhi and yhi represent points, and those with non-NA xhi or yhi represent segments, connect (set to TRUE for rows for points, to connect the symbols), legendgroup (see plotly documentation), and htext (hovertext). If the color argument is given and it is not an "AsIs" color, the variable named in the color formula must also be in data. Likewise for size. If the multplot is given, the variable given in the formula must be in data. If strata is present, another level of separate plots is generated by levels of strata, within levels of multplot.

If fitter is specified, x,y coordinates for an individual plot are run through fitter, and a line plot is made instead of showing data points. Alternatively you can specify fitter='ecdf' to compute and plot emirical cumulative distribution functions.

Value

plotly object produced by subplot

Author(s)

Frank Harrell

Examples

## Not run: 
set.seed(1)
pts     <- expand.grid(v=c('y1', 'y2', 'y3'), x=1:4, g=c('a', 'b'), yhi=NA,
                       tracename='mean', legendgroup='mean',
                       connect=TRUE, size=4)

pts$y   <- round(runif(nrow(pts)), 2)

segs     <- expand.grid(v=c('y1', 'y2', 'y3'), x=1:4, g=c('a', 'b'),
                        tracename='limits', legendgroup='limits',
                        connect=NA, size=6)
segs$y   <- runif(nrow(pts))
segs$yhi <- segs$y + runif(nrow(pts), .05, .15)

z <- rbind(pts, segs)

xlab <- labelPlotmath('X<sub>12</sub>', 'm/sec<sup>2</sup>', html=TRUE)
ylab <- c(y1=labelPlotmath('Y1', 'cm', html=TRUE),
          y2='Y2',
          y3=labelPlotmath('Y3', 'mm', html=TRUE))

W=plotlyM(z, multplot=~v, color=~g, xlab=xlab, ylab=ylab, ncols=2,
          colors=c('black', 'blue'))

W2=plotlyM(z, multplot=~v, color=~I('black'), xlab=xlab, ylab=ylab,
           colors=c('black', 'blue'))


## End(Not run)

Hmisc

Harrell Miscellaneous

v4.5-0
GPL (>= 2)
Authors
Frank E Harrell Jr <fh@fharrell.com>, with contributions from Charles Dupont and many others.
Initial release
2021-02-27

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.