grottaBar: grottaBar

View source: R/grottaBar.R

grottaBarR Documentation

grottaBar

Description

Automates the production of a Grotta Bar using ggplot()

Usage

grottaBar(
  x,
  groupName,
  scoreName,
  strataName = NULL,
  colorScheme = "Blues",
  colorScheme.reverse = FALSE,
  printNumbers = "count",
  nCol = 1,
  dir = "v",
  width = 0.9,
  textSize = 15,
  numberSize = 5,
  textFace = "plain",
  textColor = NULL,
  lineSize = 0.5,
  lineColor = "black",
  drawLines = TRUE,
  returnData = FALSE,
  ...
)

Arguments

x

a 2- or 3- dimensional table, returned by the table() function

groupName

a character string giving the name of the group variable

scoreName

a character string giving outcome labels

strataName

a character string giving the strata variable name

colorScheme

a character string indicating the colors that should be used by the plot, or a discrete fill scale returned by ggplot2.

colorScheme.reverse

A logical indicating if the colour scheme should be reversed.

printNumbers

a character string indicating if numbers should be printed for each category.

nCol

an integer indicating the number of columns to use for displaying stratified results. Has no effect if no stratification is used.

dir

a character indicating if stratified results should be laid out vertically ("v") or horizontally "h".

width

a number adjusting the width of the lines between bars

textSize

a number indicating the size of text labels

numberSize

a number indicating the size of printed numbers

textFace

a character string indicating font face of printed numbers. Can be "plain", "bold", "italic" or "bold.italic".

textColor

vector of colors for text labels

lineSize

a number indicating the thickness of lines in the plot

lineColor

vector color for lines in the plot

drawLines

boolean indicating if connecting lines should be drawn or not

returnData

a boolean indicating if the data used to create the plot should be returned. For expert users only.

...

additional arguments. Ignored except for colourScheme and textColour which will override their counterpart arguments.

Details

This tool produces a "Grotta" bar chart based on a table of count data. A Grotta bar chart is a common data visualisation tool in stroke research, and is in essence a horizontally stacked proportional bar chart showing the distribution of ordinal outcome data (typically the modified Rankin Scale) across groups, with lines drawn connecting categories across groups.

colorScheme supports all color schemes from ColorBrewer. See https://colorbrewer2.org for more information, or run RColorBrewer::display.brewer.all() to display available options. If the number of categories exceeds what is provided by ColorBrewer, additional values are interpolated as needed.

In addition, setting colorScheme to a ggplot2 discrete scale (e.g. ggplot2::scale_fill_brewer()) allows for a user-specified color scheme using the ggplot2 family of scale_fill_ functions.

The options for printNumbers are:

count

The raw counts in the table.

proportion

The within-group proportion, rounded to 2 decimal places.

percentage

The within-group percentage, rounded to 2 decimal places.

count.percentage

The raw count with percentage in parentheses.

none

Do not print any numbers.

These options may be abbreviated. "p" is not a valid abbreviation as it matches to multiple options. The minimal abbreviation for "count.percentage" is "c.p"

Value

If returnData is FALSE

A ggplot object

If returnData is TRUE

A list containing a ggplot object and the data used to generate it

References

National Institute of Neurological Disorders and Stroke rt-PA Stroke Study Group. "Tissue plasminogen activator for acute ischemic stroke." New England Journal of Medicine 333.24 (1995): 1581-1588.

Rohmann, Jessica L., et al. "Adjusted horizontal stacked bar graphs ("Grotta bars") for consistent presentation of observational stroke study results." European Stroke Journal 8.1 (2023): 370-379.

Forrest, Meghan R., et al. "Use of stacked proportional bar graphs ("Grotta bars") in observational neurology research: a meta-research study." Neurology 104.4 (2025): e210169.

Examples

df <- alteplase

x <- table(mRS=df$mRS,
          Group=df$treat,
          Time=df$time)

grottaBar(x,groupName="Group",
         scoreName = "mRS",
         strataName="Time"
)


grottaBar(x,groupName="Time",
         scoreName = "mRS",
         strataName="Group",
         textColor = c(rep("black",4),rep("white",3))
)


x <- table(mRS=df$mRS,
          Group=df$treat)


grottaBar(x,groupName="Group",
         scoreName = "mRS",
         colorScheme = FALSE
)

grottaBar(x,groupName="Group",
         scoreName = "mRS",
         colorScheme = ggplot2::scale_fill_brewer(palette = "Spectral", direction=-1)
)


grottaBar(x,groupName="Group",
         scoreName = "mRS",
         colorScheme = FALSE
)+ ggplot2::scale_fill_brewer(palette = "Spectral", direction=-1)


grottaBar(x,groupName="Group",
         scoreName = "mRS",
         printNumbers = "count.percentage",
         colorScheme = "Greys"
)

# Example with IPTW weights
 
 alteplase_confounded <- alteplase
 ps = c("0-90" = 0.7,
        "91-180" = 0.6,
        "181-270" = 0.3,
        "271-360" = 0.7
 )
 set.seed(123)
 alteplase_confounded <- by(alteplase_confounded,alteplase_confounded$time,function(x){
 
         n_alt <- round(nrow(x)*ps[unique(x$time)])
         n_pbo <- round(nrow(x)*(1-ps[unique(x$time)]))
 
         this_probs <- prop.table(table(x$mRS,x$treat),margin=2)
        
        rbind(
                data.frame(treat="Alteplase",
                            mRS = sample(as.numeric(rownames(this_probs)),
                                        size = n_alt, replace=TRUE,
                                        prob=this_probs[,"Alteplase"]),
                            time=unique(x$time)
                            ),
                data.frame(treat="Placebo",
                            mRS = sample(as.numeric(rownames(this_probs)),
                                        size = n_pbo, replace=TRUE,
                                        prob=this_probs[,"Placebo"]),
                            time=unique(x$time)
                            )
        )
})
alteplase_confounded <- do.call("rbind",alteplase_confounded)
alteplase_confounded$treat <- factor(alteplase_confounded$treat,levels=levels(alteplase$treat))

# Raw table, with confounding
x_confounded <- xtabs(~ mRS + treat, data=alteplase_confounded) 

# Using IPTW to remove confounding

propensity <- ps[alteplase_confounded$time]
alteplase_confounded$iptw_weight <- ifelse(alteplase_confounded$treat == "Alteplase",
                                           1/propensity,
                                           1/(1-propensity)
                                          )
x_weighted <- xtabs(iptw_weight ~ mRS + treat, data=alteplase_confounded) 

grottaBar(x_confounded, groupName = "treat", scoreName = "mRS", printNumbers = "percentage")
grottaBar(x_weighted, groupName = "treat", scoreName = "mRS",printNumbers = "percentage")

# The original dataset for comparison
x <- xtabs(~ mRS + treat, data=alteplase) 
grottaBar(x, groupName = "treat", scoreName = "mRS", printNumbers = "percentage")


rankinPlot documentation built on Oct. 9, 2026, 5:06 p.m.