Skip to contents

This vignette covers the customization options available across dashboardr visualization functions. We use General Social Survey (GSS) data from the gssr package to demonstrate each feature.

Data preparation (click to expand)
library(gssr)

data(gss_all)

gss <- gss_all %>%
  filter(year >= 2000) %>%
  select(year, sex, race, degree, happy, rincome, age, marital) %>%
  mutate(
    sex = haven::as_factor(sex),
    race = haven::as_factor(race),
    degree = haven::as_factor(degree),
    happy = haven::as_factor(happy),
    rincome = haven::as_factor(rincome),
    marital = haven::as_factor(marital)
  ) %>%
  filter(!is.na(sex), !is.na(race), !is.na(degree))

Color Palettes

Use color_palette to control the colors used in any visualization. You can pass:

  • A named vector to map specific categories to specific colors
  • An unnamed vector for a sequential palette

Named palette (category mapping)

viz_bar(
  data = gss,
  x_var = "race",
  title = "Respondents by Race",
  color_palette = c(
    "White" = "#4E79A7",
    "Black" = "#F28E2B",
    "Other" = "#76B7B2"
  )
)

Grouped bar with palette

viz_bar(
  data = gss,
  x_var = "degree",
  group_var = "sex",
  title = "Education by Gender",
  color_palette = c("Male" = "#4E79A7", "Female" = "#E15759")
)

Unnamed sequential palette

viz_bar(
  data = gss,
  x_var = "race",
  title = "Sequential Palette",
  color_palette = c("#264653", "#2A9D8F", "#E9C46A")
)

Tooltips

The tooltip() helper gives you full control over hover information.

Format strings

Use {placeholders} to insert dynamic values:

viz_bar(
  data = gss,
  x_var = "race",
  title = "Custom Tooltip Format",
  tooltip = tooltip(format = "{category}: {value} respondents")
)

Prefix and suffix shortcuts

viz_bar(
  data = gss,
  x_var = "degree",
  bar_type = "percent",
  title = "Education Distribution",
  tooltip = tooltip(suffix = "%")
)

Styled tooltips

viz_bar(
  data = gss,
  x_var = "happy",
  title = "Happiness Levels",
  tooltip = tooltip(
    format = "<b>{category}</b><br/>Count: {value}",
    backgroundColor = "#2d3436",
    borderColor = "#636e72",
    borderRadius = 8,
    style = list(color = "white", fontSize = "13px")
  )
)

Shared tooltips for grouped charts

viz_bar(
  data = gss,
  x_var = "degree",
  group_var = "sex",
  title = "Shared Tooltip",
  tooltip = tooltip(shared = TRUE)
)

Legends

Control the legend with legend_position:

viz_bar(
  data = gss,
  x_var = "degree",
  group_var = "sex",
  title = "Legend at Bottom",
  legend_position = "bottom"
)
viz_bar(
  data = gss,
  x_var = "degree",
  group_var = "sex",
  title = "Legend on the Right",
  legend_position = "right"
)

Data Labels

Toggle data labels with data_labels_enabled and control decimal precision with label_decimals:

viz_bar(
  data = gss,
  x_var = "race",
  title = "Without Data Labels",
  data_labels_enabled = FALSE
)
viz_bar(
  data = gss,
  x_var = "degree",
  bar_type = "percent",
  title = "Percentage Labels (1 decimal)",
  label_decimals = 1
)

Axis Labels and Formatting

Customize axis labels with x_label and y_label:

viz_bar(
  data = gss,
  x_var = "degree",
  title = "Custom Axis Labels",
  x_label = "Highest Degree Earned",
  y_label = "Number of Respondents"
)

Error Bars

Add error bars to bar charts with error_bars. Options include "se" (standard error), "sd" (standard deviation), and "ci" (confidence interval):

# Need a numeric value variable for error bars
degree_age <- gss %>%
  filter(!is.na(age))

viz_bar(
  data = degree_age,
  x_var = "degree",
  value_var = "age",
  bar_type = "mean",
  title = "Mean Age by Education (with 95% CI)",
  error_bars = "ci",
  ci_level = 0.95,
  y_label = "Mean Age"
)
viz_bar(
  data = degree_age,
  x_var = "degree",
  value_var = "age",
  bar_type = "mean",
  title = "Mean Age by Education (SE bars)",
  error_bars = "se",
  error_bar_color = "#E15759"
)

Horizontal Bars

Flip the orientation with horizontal = TRUE:

viz_bar(
  data = gss,
  x_var = "degree",
  title = "Horizontal Bar Chart",
  horizontal = TRUE
)

Sorting

Sort bars by value rather than category order:

viz_bar(
  data = gss,
  x_var = "marital",
  title = "Sorted Descending",
  sort_by_value = TRUE,
  sort_desc = TRUE
)
viz_bar(
  data = gss,
  x_var = "marital",
  title = "Sorted Ascending",
  sort_by_value = TRUE,
  sort_desc = FALSE
)

Custom Category Order

Use x_order to specify a custom display order:

viz_bar(
  data = gss,
  x_var = "degree",
  title = "Custom Degree Order",
  x_order = c("Graduate", "Bachelor", "Junior College", "High School", "Lt High School")
)

Missing Values

Include NA values in charts using include_na and na_label:

viz_bar(
  data = gss,
  x_var = "happy",
  title = "Happiness (including missing)",
  include_na = TRUE,
  na_label = "Not Reported"
)

Themes and Fonts

Use theme functions to apply consistent styling across a dashboard. These are applied at the dashboard level:

dashboard <- create_dashboard(
  title = "GSS Analysis",
  data = gss
) %>%
  apply_theme(theme_modern(
    font_family = "Inter"
  ))

Available built-in themes:

Chart Backends

dashboardr supports multiple rendering backends. Pass backend to any viz function:

viz_bar(
  data = gss,
  x_var = "race",
  title = "Highcharter (default)",
  backend = "highcharter"
)
viz_bar(
  data = gss,
  x_var = "race",
  title = "ECharts Backend",
  backend = "echarts4r"
)

See vignette("chart-backends") for a full comparison of available backends and their capabilities.

Combining Customizations

Most options can be combined freely:

viz_bar(
  data = gss,
  x_var = "degree",
  group_var = "sex",
  bar_type = "percent",
  title = "Education by Gender",
  subtitle = "GSS 2000-2021",
  x_label = "Highest Degree",
  y_label = "Percentage",
  horizontal = TRUE,
  color_palette = c("Male" = "#4E79A7", "Female" = "#E15759"),
  legend_position = "bottom",
  label_decimals = 0,
  tooltip = tooltip(suffix = "%", shared = TRUE)
)