mk_dumbbell: Create a function for making publishable ggplot2 horizontal...

View source: R/mk_dumbbell.R

mk_dumbbellR Documentation

Create a function for making publishable ggplot2 horizontal dumbbell charts, showing range of values of a continuous variable by a categorical variable.

Description

mk_dumbbell takes a data frame as input and returns a function for making dumbbell charts with any categorical variable from the data frame on the y-axis and two continuous variables (one for left end point, and the other for right end point) on the x-axis. The resulting chart, by default, will have the y categories ordered by their alphanumerical order. User can choose to order the y categories in ascending/descending order of the difference between the right and left end points of the x values. The chart produced will also have labels for the end points, and a vertical label to the far right showing the difference between right and left end points. If the x values are between 0 and 1, user can choose to format the x-axis and the labels as percent ( and clear fonts. It also uses color-blind friendly palettes.

Usage

mk_dumbbell(df)

Arguments

df

A data frame.

Value

function(xvar_left, xvar_right, yvar, yorder = "alphanumeric", show_pct = FALSE, color_left = "#1170aa", color_right = "#fc7d0b", size_left = 3, size_right = 3, segline_color = "#e3e2e1", segline_size = 3, dot_guide = TRUE, dot_guide_size = 0.5, label_decimals = 1, label_size = 3, font_size = 14)

  • xvar_left. String, name of a continuous x variable (left pt)

  • xvar_right. String, name of a continuous x variable (right pt)

  • yvar. String, name of a categorical variable for y-axis.

  • yorder. String, "alphanumeric", "ascend" or "descend". It specifies how categories are ordered on the y-axis. Default = "alphanumeric".

  • show_pct. Logical, if TRUE, format x-axis and point labels as otherwise, format them as comma. Default is FALSE.

  • color_left. String, color of left point. Default is a colorblind friendly blue.

  • color_right. String, color of right point. Default is a colorblind friendly orange.

  • size_left. String, size of left point. Default is 3.

  • size_right. String, size of right point. Default is 3.

  • segline_color. String, color of the line segment connecting left and right points. Default is a gray color.

  • segline_size. String, size of the line segment connecting left and right points. Default is 3.

  • dot_guide. Logical, if TRUE, draw dotted guide line from 0 to left point.

  • dot_guide_size. Number, size of the dotted guide line. Default is 0.5.

  • label_decimals. Integer, the number of decimal points shown on the bar labels. Default = 1.

  • label_size. Integer, size of bar label text. Default = 3. Hide bar labels when its value is 0.

  • font_size. Overall font size. Default = 14. The font size of the axes and legend text is a fraction of this value.

See Also

scale_axis for adding different scales to the y-axis.

Examples

inst/examples/ex-mk_dumbbell.R

gmlang/ezplot documentation built on Sept. 18, 2022, 6:33 a.m.