Skip to content

Conversation

@rich-iannone
Copy link
Member

The changes here allow for nanoplots that are essentially single-valued bars (when plot_type = "bar" and there is just a single value to plot) to display in positive and negative directions. A zero-value line marker is now displayed. Negative values have a different color (obtained from the data_bar_negative_stroke_color arg in nanoplot_options()) and other settable display attributes.

Here are some examples:

library(tidyverse)
library(gt)

# Positive, zero-value, NA
dplyr::tibble(
  a = c(4.6, 0.2, 5.6, 6.3, 0, NA)
) |>
  gt() |>
  cols_nanoplot(columns = a, plot_type = "bar") |>
  tab_style(
    style = cell_borders(sides = c("left", "right"), weight = "2px"),
    locations = cells_body(columns = nanoplots)
  )
example-1
# Negative, zero-value, NA
dplyr::tibble(
  a = c(-4.6, -0.2, -5.6, -6.3, 0, NA)
) |>
  gt() |>
  cols_nanoplot(columns = a, plot_type = "bar") |>
  tab_style(
    style = cell_borders(sides = c("left", "right"), weight = "2px"),
    locations = cells_body(columns = nanoplots)
  )
example-2
# Positive, zero-value, negative 
dplyr::tibble(
  a = c(-20, -10, 0, 10, 5)
) |>
  gt() |>
  cols_nanoplot(columns = a, plot_type = "bar") |>
  tab_style(
    style = cell_borders(sides = c("left", "right"), weight = "2px"),
    locations = cells_body(columns = nanoplots)
  )
example-3
# All zero nanoplots
dplyr::tibble(
  a = c(0, 0, 0, 0, 0)
) |>
  gt() |>
  cols_nanoplot(columns = a, plot_type = "bar") |>
  tab_style(
    style = cell_borders(sides = c("left", "right"), weight = "2px"),
    locations = cells_body(columns = nanoplots)
  )
example-4
# Constant non-zero value (positive)
dplyr::tibble(
  a = c(5, 5, 5, 5, 5)
) |>
  gt() |>
  cols_nanoplot(columns = a, plot_type = "bar") |>
  tab_style(
    style = cell_borders(sides = c("left", "right"), weight = "2px"),
    locations = cells_body(columns = nanoplots)
  )
example-5
# Constant non-zero value (negative)
dplyr::tibble(
  a = c(-5, -5, -5, -5, -5)
) |>
  gt() |>
  cols_nanoplot(columns = a, plot_type = "bar") |>
  tab_style(
    style = cell_borders(sides = c("left", "right"), weight = "2px"),
    locations = cells_body(columns = nanoplots)
  )
example-6
# Single value bars combined with multiple bars
dplyr::tibble(
  a = c(4.6, 0.2, 5.6, "3.4,-2.4,7.8,1.2,-6.8,0,2.4,-7.4,5.2", 6.3, 0, NA)
) |>
  gt() |>
  cols_nanoplot(columns = a, plot_type = "bar") |>
  tab_style(
    style = cell_borders(sides = c("left", "right"), weight = "2px"),
    locations = cells_body(columns = nanoplots)
  )
example-7

@rich-iannone rich-iannone merged commit 3753c5f into master Dec 23, 2023
@rich-iannone rich-iannone deleted the nanoplots-the-revenge branch December 23, 2023 04:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants