Skip to contents

Add a jitter to a scatter trace with the mode of markers.

Usage

add_jitter(
  p,
  x = NULL,
  y = NULL,
  z = NULL,
  data = NULL,
  x_jitter = NULL,
  y_jitter = NULL,
  z_jitter = NULL,
  ...
)

Arguments

p

a plotly object

x, y, z

an optional x, y, and/or z variable. Defaults to the data inherited from the plotly object p.

data

an optional data frame. Defaults to the data inherited from the plotly object p.

x_jitter, y_jitter, z_jitter

Amount of vertical, horizontal, and depth jitter. The jitter is added in both positive and negative directions, so the total spread is twice the value specified here. If omitted, defaults to 40% of the spread in the data, so the jitter values will occupy 80% of the implied bins.

...

Arguments (i.e., attributes) passed along to the trace type. See schema() for a list of acceptable attributes for a given trace type (by going to traces -> type -> attributes). Note that attributes provided at this level may override other arguments (e.g. plot_ly(x = 1:10, y = 1:10, color = I("red"), marker = list(color = "blue"))).

Value

a plotly object

Details

This adds a small amount of random variation to the location of each point, and is a useful way of handling overplotting caused by discreteness. It is based on ggplot's ggplot2::geom_jitter().

The arguments x_jitter, y_jitter, z_jitter are not from plotly's syntax. If these arguments are misspelled, plot_ly will generate a warning message listing all valid arguments, but note that plotly uses the term attributes instead of arguments. Since regress3d is an add on to plotly, this list of valid attributes does not include the attributes/arguments created in this function.

Examples

library(plotly)
plot_ly( data = hair_data,
              x = ~isFemale_num,
              y = ~isMale_num,
              z = ~length) %>%
     add_jitter( x_jitter = 0, z_jitter = 0, color = ~gender,
                colors = c("pink", "skyblue", "purple"))