group2NA: Separate groups with missing values

View source: R/group2NA.R

group2NAR Documentation

Separate groups with missing values

Description

This function is used internally by plotly, but may also be useful to some power users. The details section explains when and why this function is useful.

Usage

group2NA(
  data,
  groupNames = "group",
  nested = NULL,
  ordered = NULL,
  retrace.first = inherits(data, "GeomPolygon")
)

Arguments

data

a data frame.

groupNames

character vector of grouping variable(s)

nested

other variables that group should be nested (i.e., ordered) within.

ordered

a variable to arrange by (within nested & groupNames). This is useful primarily for ordering by x

retrace.first

should the first row of each group be appended to the last row? This is useful for enclosing polygons with lines.

Details

If a group of scatter traces share the same non-positional characteristics (i.e., color, fill, etc), it is more efficient to draw them as a single trace with missing values that separate the groups (instead of multiple traces), In this case, one should also take care to make sure connectgaps is set to FALSE.

Value

a data.frame with rows ordered by: nested, then groupNames, then ordered. As long as groupNames contains valid variable names, new rows will also be inserted to separate the groups.

Examples



# note the insertion of new rows with missing values 
group2NA(mtcars, "vs", "cyl")

# need to group lines by city somehow!
plot_ly(txhousing, x = ~date, y = ~median) %>% add_lines()

# instead of using group_by(), you could use group2NA()
tx <- group2NA(txhousing, "city")
plot_ly(tx, x = ~date, y = ~median) %>% add_lines()

# add_lines() will ensure paths are sorted by x, but this is equivalent
tx <- group2NA(txhousing, "city", ordered = "date")
plot_ly(tx, x = ~date, y = ~median) %>% add_paths()


plotly documentation built on May 29, 2024, 2:23 a.m.