plot_stats: Add labels on top of plot bars or boxes

View source: R/plot_stats.R

plot_statsR Documentation

Add labels on top of plot bars or boxes

Description

Adds text labels on top of bars or boxes of a ggplot2-based plot. Useful for annotating statistical analysis information.

Usage

plot_stats(plot, d, labels, position = "identity", size = 1, y.adj = 0)

Arguments

plot

Plot onto which to add labels. Requires a discrete x scale, optionally dodged by "fill" aesthetic.

d

data.frame containing the labels to be plotted. Must have categorical fields corresponding to the data used for the original plot.

labels

Character. The name of the column with the labels to be plotted.

position

Character, either "identity" or "dodge". Sets where (horizontally) the labels should be plotted. Defaults to "identity". If plot is dodged, "dodge" plots labels above each bar or box. "identity" plots them above the middle of each group of bars or boxes. If plot is not dodged, position is automatically set to "identity".

size

Numeric. Sets the size of labels. Defaults to 1.

y.adj

Numeric. Adjusts the height of labels above each bar, box or group. Defaults to 0. Positive sets it higher, negative sets it lower.

Details

plot_stats detects the maximum plotted value of each data group (bar, box, error bar or individual data point), and plots given labels above it.

The supplied plot needs to be based on ggplot2 and to have a discrete x scale. The supplied data.frame needs to have a character field with the labels to be plotted (whose name is specified in the labels argument) and factor field(s) for the groups in which each label belongs.

Value

A plot based on ggplot2.


dimitriskokoretsis/datavis documentation built on Oct. 14, 2022, 3:35 p.m.