Type: Package
Title: Fast and Beautiful Interactive Visualization for 'Markdown' and 'Shiny'
Version: 1.0.1
Description: Apache licensed alternative to 'Highcharter' which provides functions for both fast and beautiful interactive visualization for 'Markdown' and 'Shiny'.
Depends: htmlwidgets, magrittr, R (≥ 3.5)
URL: https://pacha.dev/d3po/
BugReports: https://github.com/pachadotdev/d3po/issues
License: Apache License (≥ 2.0)
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.3
NeedsCompilation: no
Imports: assertthat, dplyr, purrr, rlang
Suggests: knitr, igraph, shiny, golem, geojsonsf, sf, jsonlite, rmarkdown, tintin
VignetteBuilder: knitr
Packaged: 2025-11-23 03:31:10 UTC; pacha
Author: Mauricio Vargas Sepulveda ORCID iD [aut, cre, cph], John Coene [aut], Ariel Alvarado [ctb], Sylvain Lesage [ctb], Curran Kelleher [ctb], Fernando Becerra [ctb], Natural Earth [dtc], R Consortium [fnd] (Funded for the 2016-2017 ISC grants cycle)
Maintainer: Mauricio Vargas Sepulveda <m.vargas.sepulveda@gmail.com>
Repository: CRAN
Date/Publication: 2025-11-23 22:10:08 UTC

An htmlwidget interface to the d3po javascript chart library

Description

Apache licensed alternative to 'Highcharter' which provides functions for both fast and beautiful interactive visualization for 'Markdown' and 'Shiny'.

Provides 'd3po' methods from R console

Usage

d3po(data = NULL, ..., width = NULL, height = NULL, element_id = NULL)

Arguments

data

d3po need explicit specified data objects formatted as JSON, and this parameter passed it from R.

...

Aesthetics to pass, see daes()

width

Must be a valid CSS unit (like '100%', '400px', 'auto') or a number, which will be coerced to a string and have 'px' appended.

height

Same as width parameter.

element_id

Dummy string parameter. Useful when you have two or more charts on the same page.

Value

Creates a basic 'htmlwidget' object for simple visualization

Author(s)

Maintainer: Mauricio Vargas Sepulveda m.vargas.sepulveda@gmail.com (ORCID) [copyright holder]

Authors:

Other contributors:

See Also

Useful links:


D3po (re)exported methods

Description

D3po (re)exported methods


Shiny bindings for 'd3po'

Description

Output and render functions for using d3po within Shiny applications and interactive Rmd documents.

Usage

d3po_output(output_id, width = "100%", height = "400px")

render_d3po(expr, env = parent.frame(), quoted = FALSE)

d3po_proxy(id, session = shiny::getDefaultReactiveDomain())

Arguments

output_id

output variable to read from

width, height

Must be a valid CSS unit (like '100%', '400px', 'auto') or a number, which will be coerced to a string and have 'px' appended.

expr

An expression that generates a d3po object

env

The environment in which to evaluate expr.

quoted

Is expr a quoted expression (with quote())? This is useful if you want to save an expression in a variable.

id

Id of plot to create a proxy of.

session

A valid shiny session.

Value

Creates a basic 'htmlwidget' object for 'Shiny' and interactive documents


Aesthetics

Description

Aesthetics of the chart.

Usage

daes(x, y, ...)

Arguments

x

x-axis mapping.

y

y-axis mapping.

...

Other aesthetic mappings. See the 'Aesthetics' section.

Value

Aesthetics for the plots such as axis (x,y), group, color and/or size

Aesthetics

Valid aesthetics (depending on the geom)


National Boundaries Map

Description

National boundaries for all countries in the 'Natural Earth' repository. The topology has been simplified for better performance in web visualizations and reduced file size.

Usage

national

Format

An sf object with 202 observations and 4 variables.

Variables

Source

Derived from https://www.naturalearthdata.com/.

Examples

national[national$country_iso == "GBR", ]

Area

Description

Plot an area chart.

Usage

po_area(d3po, ..., data = NULL, inherit_daes = TRUE)

Arguments

d3po

Either the output of d3po() or d3po_proxy().

...

Aesthetics, see daes().

data

Any dataset to use for plot, overrides data passed to d3po().

inherit_daes

Whether to inherit aesthetics previous specified.

Value

an 'htmlwidgets' object with the desired interactive plot

Examples

if (interactive()) {
  trade_by_continent <- d3po::trade
  trade_by_continent <- aggregate(
    trade ~ year + reporter_continent,
    data = trade_by_continent,
    FUN = sum
  )

  # Assign colors to continents
  my_pal <- tintin::tintin_pal(option = "Cigars of the Pharaoh")(7)

  names(my_pal) <- c(
    "Africa", "Antarctica", "Asia",
    "Europe", "North America", "Oceania", "South America"
  )

  d3po(trade_by_continent, width = 800, height = 600) %>%
    po_area(daes(
      x = year, y = trade, group = reporter_continent, color = my_pal
    )) %>%
    po_labels(
      x = "Year",
      y = "Trade (USD billion)",
      title = "Trade Distribution by Reporter Continent in 2019 and 2023"
    )
}

Bar

Description

Draw a bar chart.

Usage

po_bar(d3po, ..., data = NULL, inherit_daes = TRUE)

Arguments

d3po

Either the output of d3po() or d3po_proxy().

...

Aesthetics, see daes().

data

Any dataset to use for plot, overrides data passed to d3po().

inherit_daes

Whether to inherit aesthetics previous specified.

Value

an 'htmlwidgets' object with the desired interactive plot

Examples

if (interactive()) {
  trade_by_continent <- d3po::trade
  trade_by_continent <- aggregate(
    trade ~ reporter_continent,
    data = d3po::trade,
    FUN = sum
  )

  # Assign colors to continents
  my_pal <- tintin::tintin_pal()(7)

  names(my_pal) <- c(
    "Africa", "Antarctica", "Asia",
    "Europe", "North America", "Oceania", "South America"
  )

  d3po(trade_by_continent, width = 800, height = 600) %>%
    po_bar(daes(x = reporter_continent, y = trade, color = my_pal)) %>%
    po_labels(
      x = "Continent",
      y = "Trade (USD billion)",
      title = "Total Trade by Reporter Continent in 2023"
    )
}

Boxplot

Description

Draw a boxplot.

Usage

po_box(d3po, ..., data = NULL, inherit_daes = TRUE)

Arguments

d3po

Either the output of d3po() or d3po_proxy().

...

Aesthetics, see daes().

data

Any dataset to use for plot, overrides data passed to d3po().

inherit_daes

Whether to inherit aesthetics previous specified.

Value

an 'htmlwidgets' object with the desired interactive plot

Examples

if (interactive()) {
  trade_continent <- d3po::trade
  trade_continent <- aggregate(
    trade ~ reporter_continent + reporter,
    data = trade_continent,
    FUN = sum
  )

  my_pal <- tintin::tintin_pal(option = "Destination Moon")(7)

  names(my_pal) <- c(
    "Africa", "Antarctica", "Asia",
    "Europe", "North America", "Oceania", "South America"
  )

  d3po(trade_continent, width = 800, height = 600) %>%
    po_box(daes(
      x = reporter_continent, y = trade, color = my_pal,
      tooltip = reporter_continent
    )) %>%
    po_labels(
      x = "Continent",
      y = "Trade (USD billion)",
      title = "Trade Distribution by Reporter Continent"
    )
}

Donut

Description

Plot a donut

Usage

po_donut(d3po, ..., data = NULL, inherit_daes = TRUE)

Arguments

d3po

Either the output of d3po() or d3po_proxy().

...

Aesthetics, see daes().

data

Any dataset to use for plot, overrides data passed to d3po().

inherit_daes

Whether to inherit aesthetics previous specified.

Value

an 'htmlwidgets' object with the desired interactive plot

Examples

if (interactive()) {
  trade_by_continent <- d3po::trade[d3po::trade$year == 2023L, ]
  trade_by_continent <- aggregate(
    trade ~ reporter_continent,
    data = d3po::trade,
    FUN = sum
  )

  # Assign colors to continents
  my_pal <- tintin::tintin_pal(option = "The Black Island")(7)

  names(my_pal) <- c(
    "Africa", "Antarctica", "Asia",
    "Europe", "North America", "Oceania", "South America"
  )

  trade_by_continent$color <- my_pal[trade_by_continent$reporter_continent]

  d3po(trade_by_continent, width = 800, height = 600) %>%
    po_donut(daes(size = trade, group = reporter_continent, inner_radius = 0.3, color = color)) %>%
    po_labels(title = "Trade Share by Reporter Continent in 2023")
}

Download

Description

Show/hide the download button.

Usage

po_download(d3po, show = TRUE)

Arguments

d3po

A 'd3po' or 'd3proxy' object.

show

Logical indicating whether to show (TRUE) or hide (FALSE) the download button.

Value

Appends download button settings to an 'htmlwidgets' object


Font

Description

Edit the font used in a chart.

Usage

po_font(d3po, family = "Fira Sans", size = 16, transform = "none")

Arguments

d3po

Either the output of d3po() or d3po_proxy().

family

family font to use ("Roboto", "Merriweather", etc.).

size

size to use (10, 11, 12, etc. overrides auto-sizing).

transform

transformation to use for the title ("lowercase", "uppercase", "capitalize", "none").

Value

Appends custom font to an 'htmlwidgets' object


Format

Description

Precompute formatted label columns from expressions and attach them to the widget data. Accepts named expressions like x = round(varx, 2) or y = format(varY, big.mark = ","). The formatted columns are added to d3po$x$data with names ⁠__label_<name>⁠ and registered in d3po$x$formatted_cols for the renderer to use.

Usage

po_format(d3po, ...)

Arguments

d3po

Either the output of d3po() or d3po_proxy().

...

Named formatting expressions (as quosures). Each name should correspond to an aesthetic (e.g. x, y, tooltip, etc.).


Geomap

Description

Plot a geomap using sf spatial objects

Usage

po_geomap(d3po, ..., data = NULL, inherit_daes = TRUE, limits = NULL)

Arguments

d3po

Either the output of d3po() or d3po_proxy().

...

Aesthetics, see daes().

data

Any dataset to use for plot, overrides data passed to d3po().

inherit_daes

Whether to inherit aesthetics previous specified.

limits

A numeric vector of length 2 specifying the minimum and maximum values for the color scale.

Value

an 'htmlwidgets' object with the desired interactive plot

Examples

if (interactive()) {
  world <- d3po::national

  # Fix geometries that cross the antimeridian (date line) to avoid horizontal lines
  # This affects Russia, Fiji, and other countries spanning the 180° meridian
  world$geometry <- sf::st_wrap_dateline(world$geometry, options = c("WRAPDATELINE=YES"))

  total_trade <- d3po::trade[
    d3po::trade$year == 2023L,
    c("reporter", "reporter_continent", "trade")
  ]
  total_trade <- aggregate(trade ~ reporter, data = total_trade, FUN = sum)
  colnames(total_trade) <- c("country", "trade")

  world <- merge(
    world,
    total_trade,
    by = "country",
    all.x = TRUE,
    all.y = FALSE
  )

  my_pal <- tintin::tintin_pal(option = "The Calculus Affair")(7)

  names(my_pal) <- c(
    "Africa", "Antarctica", "Asia",
    "Europe", "North America", "Oceania", "South America"
  )

  d3po(world, width = 800, height = 600) %>%
    po_geomap(daes(group = country, size = trade, color = my_pal, tooltip = country)) %>%
    po_labels(title = "Trade Volume by Country in 2023")
}

Labels

Description

Edit labels positioning in a treemap.

Usage

po_labels(
  d3po,
  x = NULL,
  y = NULL,
  title = NULL,
  subtitle = NULL,
  labels = NULL,
  align = "left-top"
)

Arguments

d3po

Either the output of d3po() or d3po_proxy().

x

Optional x-axis label.

y

Optional y-axis label.

title

Optional title for the chart.

subtitle

Optional subtitle for the chart.

labels

Optional character vector or JavaScript function for custom label fields for treemaps.

align

Label alignment for treemaps. Must be one of "left-top", "center-middle", or "right-top".

Value

Appends custom labels to an 'htmlwidgets' object


Line

Description

Plot an line chart.

Usage

po_line(d3po, ..., data = NULL, inherit_daes = TRUE)

Arguments

d3po

Either the output of d3po() or d3po_proxy().

...

Aesthetics, see daes().

data

Any dataset to use for plot, overrides data passed to d3po().

inherit_daes

Whether to inherit aesthetics previous specified.

Value

an 'htmlwidgets' object with the desired interactive plot

Examples

if (interactive()) {
  trade_by_continent <- d3po::trade
  trade_by_continent <- aggregate(
    trade ~ year + reporter_continent,
    data = trade_by_continent,
    FUN = sum
  )

  # Assign colors to continents
  my_pal <- tintin::tintin_pal(option = "The Broken Ear")(7)

  names(my_pal) <- c(
    "Africa", "Antarctica", "Asia",
    "Europe", "North America", "Oceania", "South America"
  )

  d3po(trade_by_continent, width = 800, height = 600) %>%
    po_line(daes(x = year, y = trade, group = reporter_continent, color = my_pal)) %>%
    po_labels(
      x = "Year",
      y = "Trade (USD billion)",
      title = "Trade Distribution by Reporter Continent in 2019 and 2023"
    )
}

Network

Description

Draw a network graph showing relationships between entities. Requires an igraph object with nodes (vertices) and links (edges). Node size can represent counts or other metrics.

Usage

po_network(d3po, ..., data = NULL, inherit_daes = TRUE)

Arguments

d3po

Either the output of d3po() or d3po_proxy().

...

Aesthetics, see daes().

data

Any dataset to use for plot, overrides data passed to d3po().

inherit_daes

Whether to inherit aesthetics previous specified.

Value

Appends nodes arguments to a network-specific 'htmlwidgets' object

Examples

if (interactive()) {
  trade_network <- d3po::trade[d3po::trade$year == 2023L, ]
  trade_network <- aggregate(
    trade ~
      reporter_iso + partner_iso + reporter_continent + partner_continent,
    data = trade_network, FUN = sum
  )

  # subset to 10 largest connection per reporter country
  trade_network <- do.call(
    rbind,
    lapply(
      split(trade_network, trade_network$reporter_iso),
      function(df) head(df[order(-df$trade), ], 10)
    )
  )

  # Create vertex (node) attributes for coloring and sizing
  # Get unique countries with their continents and trade volumes
  vertices <- unique(rbind(
    data.frame(
      name = trade_network$reporter_iso,
      continent = trade_network$reporter_continent,
      stringsAsFactors = FALSE
    ),
    data.frame(
      name = trade_network$partner_iso,
      continent = trade_network$partner_continent,
      stringsAsFactors = FALSE
    )
  ))

  # Remove duplicates
  vertices <- vertices[!duplicated(vertices$name), ]

  # Calculate total trade volume per country (as reporter)
  trade_volume <- aggregate(trade ~ reporter_iso, data = trade_network, FUN = sum)
  colnames(trade_volume) <- c("name", "trade_volume")

  # Merge trade volume with vertices
  vertices <- merge(vertices, trade_volume, by = "name", all.x = TRUE)
  vertices$trade_volume[is.na(vertices$trade_volume)] <- 0

  # Assign colors to continents
  my_pal <- tintin::tintin_pal(option = "The Blue Lotus")(7)

  names(my_pal) <- c(
    "Africa", "Antarctica", "Asia",
    "Europe", "North America", "Oceania", "South America"
  )

  # Add color column based on continent
  vertices$color <- my_pal[vertices$continent]

  # Create igraph object with vertex attributes
  g <- graph_from_data_frame(trade_network, directed = TRUE, vertices = vertices)

  # Create the network visualization
  d3po(g, width = 800, height = 600) %>%
    po_network(daes(size = trade_volume, color = color, layout = "fr")) %>%
    po_labels(title = "Trade Network by Country in 2023")
}


Pie

Description

Plot a pie

Usage

po_pie(d3po, ..., data = NULL, inherit_daes = TRUE)

Arguments

d3po

Either the output of d3po() or d3po_proxy().

...

Aesthetics, see daes().

data

Any dataset to use for plot, overrides data passed to d3po().

inherit_daes

Whether to inherit aesthetics previous specified.

Value

an 'htmlwidgets' object with the desired interactive plot

Examples

if (interactive()) {
  trade_by_continent <- d3po::trade[d3po::trade$year == 2023L, ]
  trade_by_continent <- aggregate(
    trade ~ reporter_continent,
    data = d3po::trade,
    FUN = sum
  )

  # Assign colors to continents
  my_pal <- tintin::tintin_pal(option = "The Black Island")(7)

  names(my_pal) <- c(
    "Africa", "Antarctica", "Asia",
    "Europe", "North America", "Oceania", "South America"
  )

  d3po(trade_by_continent, width = 800, height = 600) %>%
    po_pie(daes(size = trade, group = reporter_continent, color = my_pal)) %>%
    po_labels(title = "Trade Share by Reporter Continent in 2023")
}

Scatter

Description

Plot an scatter chart.

Usage

po_scatter(d3po, ..., data = NULL, inherit_daes = TRUE)

Arguments

d3po

Either the output of d3po() or d3po_proxy().

...

Aesthetics, see daes().

data

Any dataset to use for plot, overrides data passed to d3po().

inherit_daes

Whether to inherit aesthetics previous specified.

Value

an 'htmlwidgets' object with the desired interactive plot

Examples

if (interactive()) {
  # Create a wide dataset with x = 2019 and y = 2023 trade values
  trade_wide_2019 <- d3po::trade[d3po::trade$year == 2019L, c("reporter", "trade")]
  trade_wide_2019 <- aggregate(trade ~ reporter, data = trade_wide_2019, FUN = sum)

  trade_wide_2023 <- d3po::trade[d3po::trade$year == 2023L, c("reporter", "trade")]
  trade_wide_2023 <- aggregate(trade ~ reporter, data = trade_wide_2023, FUN = sum)

  trade_wide <- merge(
    trade_wide_2019,
    trade_wide_2023,
    by = "reporter",
    suffixes = c("_2019", "_2023")
  )

  my_pal <- tintin::tintin_pal(option = "red_rackhams_treasure")(7)

  d3po(trade_wide, width = 800, height = 600) %>%
    po_scatter(daes(x = trade_2019, y = trade_2023, group = reporter, color = my_pal)) %>%
    po_labels(
      x = "Trade in 2019 (USD billion)",
      y = "Trade in 2023 (USD billion)",
      title = "Trade Volume by Country in 2019 and 2023"
    )
}

Theme

Description

Manually set colors used by D3po for axis/axis-labels/title, tooltips/download menu background, and chart background. This allows you to override page themes (Tabler/Shiny) and force D3po to render with readable contrast.

Usage

po_theme(d3po, axis = NULL, tooltips = NULL, background = NULL)

Arguments

d3po

Either the output of d3po() or d3po_proxy().

axis

Hex color string for axis lines and axis/label/title fill (e.g. "#fff").

tooltips

Hex color string for tooltip / download menu background (e.g. "#000").

background

Hex color string for chart background (e.g. "#fff").

Value

Appends theme settings to an 'htmlwidgets' object


Tooltip

Description

Set a custom tooltip template for a chart. The template can be a literal string (e.g. ⁠<b>{name}</b><br>Value: {value}⁠) which will be evaluated server-side by substituting column values.

Usage

po_tooltip(d3po, template)

Arguments

d3po

Either the output of d3po() or d3po_proxy().

template

A character string template or formatter expression.

Value

Appends tooltip settings to an 'htmlwidgets' object


Treemap

Description

Plot a treemap

Usage

po_treemap(d3po, ..., data = NULL, inherit_daes = TRUE)

Arguments

d3po

Either the output of d3po() or d3po_proxy().

...

Aesthetics, see daes().

data

Any dataset to use for plot, overrides data passed to d3po().

inherit_daes

Whether to inherit aesthetics previous specified.

Value

an 'htmlwidgets' object with the desired interactive plot

Examples

if (interactive()) {
  trade_by_continent <- d3po::trade[d3po::trade$year == 2023L, ]
  trade_by_continent <- aggregate(trade ~ reporter_continent, data = trade_by_continent, FUN = sum)

  my_pal <- tintin::tintin_pal(option = "The Secret of the Unicorn")(7)

  names(my_pal) <- c(
    "Africa", "Antarctica", "Asia",
    "Europe", "North America", "Oceania", "South America"
  )

  d3po(trade_by_continent, width = 800, height = 600) %>%
    po_treemap(daes(
      size = trade, group = reporter_continent,
      color = my_pal, tiling = "Squarify"
    )) %>%
    po_labels(title = "Trade Share by Continent in 2023")
}

Subnational Boundaries Map

Description

Subnational boundaries for the available countries in the 'Natural Earth' repository. The topology has been simplified for better performance in web visualizations and reduced file size. Subnational regions include states, provinces, and equivalent administrative divisions.

Usage

subnational

Format

An sf object with 4,596 observations and 6 variables.

Variables

Source

Derived from https://www.naturalearthdata.com/.

Examples

subnational[subnational$country_iso == "GBR", ]

World Trade 2019 and 2023

Description

Bilateral trade flows between countries for the years 2019 and 2023. Trade values are expressed in USD billion dollars.

Usage

trade

Format

A data frame with 5,223 observations and 8 variables.

Variables

Source

Derived from the UN Comtrade database (https://comtrade.un.org/).

Examples

head(trade[trade$year == 2023L & trade$reporter_iso == "gbr", ])