CanvasXpress GGPlot2 Interface

Transform Your Static GGPlots into Interactive Visualizations

Getting Started

For many R users, ggplot2 is the gold standard for creating elegant, publication-quality static plots. Its "grammar of graphics" provides a powerful and intuitive way to build complex visualizations layer by layer. But what if you could take those carefully crafted plots and make them fully interactive with just one extra line of code? The canvasXpress R package makes this possible.

This guide will show you how to enhance your existing ggplot2 workflow by converting your static plots into dynamic, web-ready charts with tooltips, zooming, and interactive legends.

Installation

If you haven't already, install both canvasXpress and ggplot2 from CRAN.

install.packages("canvasXpress")
install.packages("ggplot2")

Then, load both libraries to get started.

library(canvasXpress)
library(ggplot2)

How It Works: The canvasXpress Translator

The magic behind the conversion lies in the canvasXpress() function's built-in intelligence. When you pass a ggplot object to it, the function acts as a sophisticated translator. It automatically parses the structure of your plot, interpreting the various layers (like geom_point or geom_bar) and aesthetic mappings (such as aes(x=..., y=..., color=...)) you've defined. It then converts these elements into the specific JSON configuration required by the underlying CanvasXpress JavaScript engine, effectively rebuilding your static plot as a fully interactive HTML widget.

A Step-by-Step Example

Let's walk through the process with a simple example using the iris dataset.

1. Create Your ggplot2 Object

First, build a standard ggplot object just as you normally would. Here, we'll create a scatter plot of petal length versus petal width, colored by species.

# Create a standard ggplot2 object
my_gg_plot <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width, color = Species)) +
  geom_point(size = 3) +
  labs(title = "Iris Petal Length vs. Width") +
  theme_minimal()

2. The One-Liner Conversion

Now, simply pass the my_gg_plot object directly to the canvasXpress() function.

# Convert the ggplot object to an interactive CanvasXpress chart
canvasXpress(my_gg_plot)

Raincloud (ggrain-style) Plots

Raincloud plots — a half-violin density "cloud", a boxplot, and the raw jittered data points ("rain") side by side — were popularized in R by the ggrain package's geom_rain() layer. CanvasXpress builds the same figure natively and interactively, including the paired "repeated-measures" variant where lines connect the same subject across conditions (ggrain's f1x1 flanking layout). You do not pass a geom_rain() object to the converter — geom_rain's custom GeomHalfViolin layer is not auto-translated — instead you assemble the raincloud directly from CanvasXpress parameters, which gives you tooltips, zooming, and interactive legends for free.

The building blocks

A raincloud is a Boxplot graph with violins turned on, plus three parameters that place each element on the band:

  • violinSide — which half of the band the cloud occupies (both, lt, rb, or split). Give it an object keyed by condition name to flank conditions outward, e.g. {"Baseline":"lt","Followup":"rb"}.
  • showBoxplotOriginalData + jitter — draw the raw points (the "rain") on the opposite half from the cloud, jittered.
  • showViolinBoxplot — overlay the boxplot between the cloud and the rain.
  • connectBy (optional) — the subject/id annotation whose points are linked across conditions for a paired raincloud.

In R

library(canvasXpress)

# y = numeric values; x carries the grouping annotations (Condition, Subject, Cohort)
canvasXpress(
  data              = raincloud_data,          # list(y = ..., x = ...)
  graphType         = "Boxplot",
  graphOrientation  = "vertical",
  groupingFactors   = list("Condition"),
  colorBy           = "Cohort",
  violinSide        = list(Baseline = "lt", Followup = "rb"),  # flank conditions outward
  showViolinBoxplot = TRUE,
  showBoxplotOriginalData = TRUE,
  jitter            = TRUE,
  jitterFactor      = 0.7,
  violinScale       = "area",
  violinTrim        = FALSE,
  connectBy         = "Subject",               # paired lines across conditions
  connectByPointColor = TRUE,
  title             = "Connected Raincloud (Baseline vs Followup)"
)

As a portable JSON config

The same chart as a {data, config} figure (JavaScript, Python, or the cxplot CLI all accept this):

"config": {
  "graphType": "Boxplot",
  "graphOrientation": "vertical",
  "groupingFactors": ["Condition"],
  "colorBy": "Cohort",
  "violinSide": { "Baseline": "lt", "Followup": "rb" },
  "showViolinBoxplot": true,
  "showBoxplotOriginalData": true,
  "jitter": true, "jitterFactor": 0.7,
  "violinScale": "area", "violinTrim": false,
  "connectBy": "Subject", "connectByPointColor": true
}

A live, interactive version of this raincloud is in the gallery: Connected Raincloud (Baseline vs Followup). For a single-condition raincloud, drop connectBy and set violinSide to a single value such as "lt".

Examples

⇧