Skip to content
yihui edited this page Aug 25, 2011 · 7 revisions

Utilities in cranvas

Break character strings

break_str() can break characters strings into "several lines" by replacing certain characters with \n, which may be useful to wrap long (axis) labels. This is the default way to deal with labels in par-coords plot. See ?break_str for details.

Extend ranges by a certain amount

extend_ranges() can accept a numeric vector or a vector of length 2 or a matrix and return the extended limits. The form of the matrix matches with qrect in qtbase, i.e.

x0 y0
x1 y1

We aim to replace the old make_window_ranges() in the near future.

A simpler function to match keys

The function match_key() was designed to save some typing efforts like event$key() == Qt$Qt$Key_PageDown; we can use match_key('PageDown') inside an event callback function to do the same thing. See ?match_key or the source code of qparallel() for examples.

Reorder variables in a data frame

reorder_var() can reorder a data frame in various ways: ANOVA, MDS or random forest.

Axis locations

axis_loc() calculate the "pretty" locations of tick marks.

Draw a brush rectangle

As long as you have a correct meta data structure, you can call draw_brush(painter, brush, meta) to draw a brush rectangle. See the source code of qbar() or qparallel() for applications.

Draw labels for the identified cases

Similarly we can use draw_identify() to draw labels.

Sync limits of layers

The function sync_limits() can sync the limits of an arbitrary number of layers; all the limits are set to meta$limits, so it is important that you remember to update this component in the meta data.

Switch values of two variables

switch_value('x', 'y') can switch the values of the variables x and y; this function is designed to make it easier to handle horizontal and vertical plots, in which we only need to switch some variables in the meta data and the drawing remains the same. See the source code of qbar() for example.

Check validity of data

Use check_data() in the first place in a plotting function; if the data provided was not created from qdata(), it will do the conversion and issue a message.

Access the last used data

last_data() should be a reasonable default for the data argument of a plotting function.

Set the cursor shape

set_cursor() can change the cursor on a plot view, as long as you attach an event on brush$cursor (see qhist() for an example).

Make lighter colors

lighter() does the job.

Common interactions

A series of functions like common_xxx_xxx() can reduce the length of code and make all plots more consistent in terms of interactions.