gg_barplot: Bar plots

Description Usage Arguments Details Value Author(s) Examples

Description

Creates a bar plot with vertical or horizontal bars

Usage

1
2
3
4
5
6
7
8
gg_barplot(data, var, var_levels = NULL, group = NULL,
  group_levels = NULL, facet_r = NULL, facet_c = NULL,
  facet_r_levels = NULL, facet_c_levels = NULL, facet_scale = "free",
  facet_space = "free", x_lab = NULL, y_lab = NULL, group_lab = group,
  title = NULL, add_counts = TRUE, counts_pos = "inside-bar",
  bar_label_align = "left", bar_label_angle = NULL,
  group_bar_position = "dodge", grids = "on", bw_theme = TRUE,
  horizontal = TRUE)

Arguments

data

Data frame: default dataset to use for plot

var

Character: name of a data column whose counts provided as the height of bars

var_levels

Vector/List: a named vector/list that specifies the levels and labels of var

group

Character: name of a data column mapped to the fill of bars

group_levels

Vector/List: a named vector/list that specifies the levels and labels of group

facet_r

Character: name of a data column mapped to the facet row in panel plot layout. Check facet_grid for more details

facet_c

Character: name of a data column mapped to the facet column in panel plot layout. Check facet_grid for more details

facet_r_levels

Vector/List: a named vector/list that specifies the levels and labels of facet_r

facet_c_levels

Vector/List: a named vector/list that specifies the levels and labels of facet_c

facet_scale

Character: Are scales shared across all facets. Refer to the 'scale' argument in facet_grid. Default 'free' means that scales are not shared

facet_space

Character: Refer to the 'space' argument in facet_grid. Default 'free' means both height and width will vary

x_lab

Character: x-axis label

y_lab

Character: y-axis label

group_lab

Character: group variable label

title

Character: barplot title

add_counts

Logical: If 'TRUE' (default), counts will be added to the bars

counts_pos

Character: position to add counts. If 'inside-bar' (default), counts will be inside the bars; if 'outside-bar', counts be placed outside the bars. When group is present and group_bar_position is specified as 'stack', counts will be placed in the center of the bars

bar_label_align

Character: alignment of the bar labels. If 'left' (default), labels will be left-aligned; if 'center', labels will be center-aligned; if 'right', labels will be right-aligned

bar_label_angle

Numeric: the orientation angle (in [0, 360]) of the bar labels on the axis. By default, the label will be perpendicular to the axis

group_bar_position

Character: position adjustment for bars of same group when group is present. If 'dodge' (default), bars of same group are placed side by side; if 'stack', bars of same group are stacked one on top of another.

grids

Character: control the grids. If 'on' (default), grids will be drawn; if 'x', only grids on x-axis will be drawn; if 'y', only grids on y-axis will be drawn; if 'off', no grids will be drawn

bw_theme

Logical: If 'TRUE' (default), black-and-white theme will be used. Refer to theme_bw for more details

horizontal

Logical: If 'TRUE' (default), horizontal barplot is drawn; if 'FALSE', vertical barplot is drawn

Details

This function relies on ggplot2 package to create a bar plot with vertical or horizontal bars. The function supports panel plot layout. Counts information can be added to the corresponding bars. By default black-and-white theme is used.

Value

An object of class ggplot. Can be directly sent to plot with print

Author(s)

Feiyang Niu (Feiyang.Niu@gilead.com)

Examples

1
2
3
4
5
6
gg_barplot(mpg, var = 'class', x_lab = '', horizontal = FALSE)
gg_barplot(mpg, var = 'class', group = 'cyl', group_lab = 'Cylinders')
var_levels <- c('Mid-size' = 'midsize', 'Minivan' = 'minivan', 'SUV' = 'suv')
gg_barplot(mpg, var = 'class', var_levels = var_levels,
           group = 'cyl', group_lab = 'Cylinders', add_counts = FALSE)
gg_barplot(mpg, var = 'class', group = 'cyl', group_bar_position = 'stack')

statech/CommonPlots documentation built on May 6, 2019, 1:32 a.m.