Transition-class: A reference class for generating transition plots

Transition-classR Documentation

A reference class for generating transition plots

Description

This class simplifies the creating of transition plots. It also allows for advanced multi-column transitions.

Details

Transition plots are a type of Sankey diagrams. These are a specific type of flow diagram, in which the width of the arrows is shown proportionally to the flow quantity. See Wikipedia for details.

Fields

id

Optional id. The render uses named viewports that require a unique id if multiple transition plots are combined. In order to avoid having overlapping graphs we need to generate a unique id for each viewport and thus this variable exists. If left empty it will create a counter that is stored in the options ("Gmisc.transitionClassCounter") and each viewport will have the name preceded with tc_[0-9]+. Set this if you intend to use seekViewport.

transitions

This is a >= 3 dimensional array with the transitions. Should not be directly accessed.

box_width

The box width

box_txt

The texts of each box

box_label

Box labels

box_label_pos

The label's positions, either "top"/"bottom"

box_label_cex

The size of the box labels

box_cex

The font-size multiplier for the text within the boxes

arrow_type

The type of arrow to use, defaults to "gradient", but can also be "simple". The corresponding functions are bezierArrowGradient, and bezierArrowSmpl. Note The bezierGrob has been deprecated as it is no longer faster than the bezier arrows and there is a difference in design.

arrow_clr

The arrow color

arrow_rez

The resolution of the arrow

vertical_space

The space between the boxes

fill_clr

The box fill color

clr_bar

Shows a color bar if there are proportions. Can be "none", "top", "bottom"

clr_bar_clrs

Extracts the colors for the color bar from the fill_clr if none is provided

clr_bar_cex

The size of the ticks in the color bar

clr_bar_subspace

If little or no difference exists at the low/high proportions of the spectrum then it can be of interest to focus the color change to the center leaving the tails constant

clr_bar_labels

The labels of the color bars. Defaults to the dim names for the proportions.

txt_clr

The text color within the boxes

txt_gpar

Similar to 'txt_clr' but for more advanced styling with fontfamily (see [grid::gpar()]). *Note* that col & cex are overridden.

title

The plot title if any

title_cex

The font-size multiplier for the title

skip_shadows

Skip the shadow effect on the boxes

mar

The margins for the plot.

min_lwd

The minimum line width that is still shown. The pixels will most likely not have the same fine resolution as the data and you therefore may want to hide lines that are smaller than a certain amount.

max_lwd

The maximum line width to show

lwd_prop_type

The line can either be proportional to the "set" of transitions (group of two box columns), to "all" transitions, or to each "box". It defaults to "all".

data

Internal storage variable. Should not be accessed directly.

Methods

addBoxStyle(fill, txt, gpar)

Adds colors or extends existing one so that they match the transition matrix. The fill corresponds to the fill_clr and txt corresponds to the txt_clr. If the colors are missing and the transitions consist of only two columns the default colors will be used. If the matrix is being extended and these values are missing the values from the previous last column will be used for the default columns.

addTransitions(mtrx, label, txt, fill_clr, txt_clr, txt_gpar)

Add a transition matrix. The input has to be a numerical matrix between 2 and 3 dimensions. If you don't provide the txt field the box' text field will be deduced from the transition matrix' dimnames. The fill_clr and txt_clr are passed on to the addBoxStyle function.

arrowWidths(set_no, add_width)

Retrieves the details regarding arrow sizes for each arrow within the transition group

boxPositions(col)

The box positions as a list with scalars for the positions:

  1. x The center x-position

  2. y The center y-position

  3. right The right edge

  4. left The left edge

  5. top The top edge

  6. bottom The bottom edge

  7. height The box height

  8. width The box width

  9. unit The unit used for the values (npc)

boxSizes(col)

Gets the size of the boxes. The col argument should be either an integer or 'last'

getDim()

Gets the current dimensions of the transitions

getTransitionSet(no, reduce_dim = FALSE)

Gets a specific set of transitions. If the reduce_dim is set to TRUE it will only return a 2-dimensional matrix even if the original has a 3rd proportions dimension

getYProps(col)

Gets the proportions after removing the vertical_space between the boxes

initialize(transitions, label, txt, fill_clr, txt_clr, txt_gpar, id, ...)

Set up a Transition object. The transitions should be a 2D or 3D matrix as defined in the $addTransitions section and not as later internally stored.

noCols()

Gets the number of columns, i.e. the number of transitions

noRows(no)

Gets the number of boxes in each row. If multiple rows the number of rows may differ betwen each transition matrix we therefore need to specify what transitions that we refer to. If no value is specified it returns all of them.

render(new_page = TRUE)

Call this to render the full graph. The new_page argument is for creating a new plot, set this to FALSE if you want to combine this plot with another or if you have additional viewports that you intend to use.

trnstnSizes(set_no)

Gets the transitions per box as a 2D matrix. For the proportions it also adds an attribute attr('props', prop_mtrx) that is a 2D matrix with the corresponding proportions.

Examples

# Transitions
set.seed(1)
n <- 10
my_data <-
  data.frame(
    Var_a = sample(c(
      "Test 1",
      "Test 2",
      "Test 3"
    ),
    size = n,
    replace = TRUE,
    prob = 3:1
    ),
    Var_b = sample(c(
      "Test 1",
      "Test 2",
      "Test 3"
    ),
    size = n,
    replace = TRUE,
    prob = 1:3
    )
  )
mtrx <- with(
  my_data,
  table(Var_a, Var_b)
)

# Initialize the transition plot
transitions <- getRefClass("Transition")$new(mtrx,
                                             label = c("Before", "After"))

# Render the plot
transitions$render()

Gmisc documentation built on Aug. 26, 2023, 1:07 a.m.