This document is adapted from the Bar Charts section of the Altair Example Gallery.

Our first step is to set up our environment:

library("altair")
library("tibble")
library("jsonlite")

vega_data <- import_vega_data()

Bar Chart with Highlighted Bar

Altair example

Chart

Bar Chart with Labels

Altair example

Chart

Bar Chart with Line as Mean

Altair example

Chart

Bar Chart with Line on Dual Axis

Altair example

This seems exemplary of why dual-axes should be used with caution.

Chart

Bar Chart with Negative Values

Altair example

Data

glimpse(vega_data$us_employment())
#> Observations: 120
#> Variables: 24
#> $ month                              <chr> "2006-01-01", "2006-02-01", "…
#> $ nonfarm                            <dbl> 135450, 135762, 136059, 13622…
#> $ private                            <dbl> 113603, 113884, 114156, 11430…
#> $ goods_producing                    <dbl> 22467, 22535, 22572, 22631, 2…
#> $ service_providing                  <dbl> 112983, 113227, 113487, 11359…
#> $ private_service_providing          <dbl> 91136, 91349, 91584, 91677, 9…
#> $ mining_and_logging                 <dbl> 656, 662, 669, 679, 681, 686,…
#> $ construction                       <dbl> 7601, 7664, 7689, 7726, 7713,…
#> $ manufacturing                      <dbl> 14210, 14209, 14214, 14226, 1…
#> $ durable_goods                      <dbl> 8982, 8986, 9000, 9020, 9017,…
#> $ nondurable_goods                   <dbl> 5228, 5223, 5214, 5206, 5186,…
#> $ trade_transportation_utilties      <dbl> 26162, 26196, 26239, 26230, 2…
#> $ wholesale_trade                    <dbl> 5840.4, 5854.8, 5873.3, 5886.…
#> $ retail_trade                       <dbl> 15351.5, 15361.3, 15388.0, 15…
#> $ transportation_and_warehousing     <dbl> 4420.0, 4429.4, 4429.7, 4445.…
#> $ utilities                          <dbl> 549.8, 550.1, 547.5, 548.9, 5…
#> $ information                        <dbl> 3052, 3052, 3055, 3046, 3039,…
#> $ financial_activities               <dbl> 8307, 8332, 8348, 8369, 8376,…
#> $ professional_and_business_services <dbl> 17299, 17365, 17438, 17462, 1…
#> $ education_and_health_services      <dbl> 17946, 17998, 18045, 18070, 1…
#> $ leisure_and_hospitality            <dbl> 12945, 12980, 13034, 13074, 1…
#> $ other_services                     <dbl> 5425, 5426, 5425, 5426, 5433,…
#> $ government                         <dbl> 21847, 21878, 21903, 21919, 2…
#> $ nonfarm_change                     <dbl> 282, 312, 297, 168, 31, 79, 2…

Chart

Bar and Tick Chart

Altair example

Chart

Calculating Percentage of Total

Altair example

In the Altair example, we see some code like this:

In R, supply the stuff inside the **{} as additional arguments to the function:

See the Field Guide to Python Issues for more details and more examples.

Chart

Diverging Stacked Bar Chart

Altair example

This example shows a diverging stacked bar chart for sentiments towards a set of eight questions, displayed as percentages with neutral responses straddling the 0% mark.

Data

Definition

data <- fromJSON('[
  {
    "question": "Question 1",
    "type": "Strongly disagree",
    "value": 24,
    "percentage": 0.7,
    "percentage_start": -19.1,
    "percentage_end": -18.4
  },
  {
    "question": "Question 1",
    "type": "Disagree",
    "value": 294,
    "percentage": 9.1,
    "percentage_start": -18.4,
    "percentage_end": -9.2
  },
  {
    "question": "Question 1",
    "type": "Neither agree nor disagree",
    "value": 594,
    "percentage": 18.5,
    "percentage_start": -9.2,
    "percentage_end": 9.2
  },
  {
    "question": "Question 1",
    "type": "Agree",
    "value": 1927,
    "percentage": 59.9,
    "percentage_start": 9.2,
    "percentage_end": 69.2
  },
  {
    "question": "Question 1",
    "type": "Strongly agree",
    "value": 376,
    "percentage": 11.7,
    "percentage_start": 69.2,
    "percentage_end": 80.9
  },
  
  {
    "question": "Question 2",
    "type": "Strongly disagree",
    "value": 2,
    "percentage": 18.2,
    "percentage_start": -36.4,
    "percentage_end": -18.2
  },
  {
    "question": "Question 2",
    "type": "Disagree",
    "value": 2,
    "percentage": 18.2,
    "percentage_start": -18.2,
    "percentage_end": 0
  },
  {
    "question": "Question 2",
    "type": "Neither agree nor disagree",
    "value": 0,
    "percentage": 0,
    "percentage_start": 0,
    "percentage_end": 0
  },
  {
    "question": "Question 2",
    "type": "Agree",
    "value": 7,
    "percentage": 63.6,
    "percentage_start": 0,
    "percentage_end": 63.6
  },
  {
    "question": "Question 2",
    "type": "Strongly agree",
    "value": 11,
    "percentage": 0,
    "percentage_start": 63.6,
    "percentage_end": 63.6
  },
  
  {
    "question": "Question 3",
    "type": "Strongly disagree",
    "value": 2,
    "percentage": 20,
    "percentage_start": -30,
    "percentage_end": -10
  },
  {
    "question": "Question 3",
    "type": "Disagree",
    "value": 0,
    "percentage": 0,
    "percentage_start": -10,
    "percentage_end": -10
  },
  {
    "question": "Question 3",
    "type": "Neither agree nor disagree",
    "value": 2,
    "percentage": 20,
    "percentage_start": -10,
    "percentage_end": 10
  },
  {
    "question": "Question 3",
    "type": "Agree",
    "value": 4,
    "percentage": 40,
    "percentage_start": 10,
    "percentage_end": 50
  },
  {
    "question": "Question 3",
    "type": "Strongly agree",
    "value": 2,
    "percentage": 20,
    "percentage_start": 50,
    "percentage_end": 70
  },
  
  {
    "question": "Question 4",
    "type": "Strongly disagree",
    "value": 0,
    "percentage": 0,
    "percentage_start": -15.6,
    "percentage_end": -15.6
  },
  {
    "question": "Question 4",
    "type": "Disagree",
    "value": 2,
    "percentage": 12.5,
    "percentage_start": -15.6,
    "percentage_end": -3.1
  },
  {
    "question": "Question 4",
    "type": "Neither agree nor disagree",
    "value": 1,
    "percentage": 6.3,
    "percentage_start": -3.1,
    "percentage_end": 3.1
  },
  {
    "question": "Question 4",
    "type": "Agree",
    "value": 7,
    "percentage": 43.8,
    "percentage_start": 3.1,
    "percentage_end": 46.9
  },
  {
    "question": "Question 4",
    "type": "Strongly agree",
    "value": 6,
    "percentage": 37.5,
    "percentage_start": 46.9,
    "percentage_end": 84.4
  },
  
  {
    "question": "Question 5",
    "type": "Strongly disagree",
    "value": 0,
    "percentage": 0,
    "percentage_start": -10.4,
    "percentage_end": -10.4
  },
  {
    "question": "Question 5",
    "type": "Disagree",
    "value": 1,
    "percentage": 4.2,
    "percentage_start": -10.4,
    "percentage_end": -6.3
  },
  {
    "question": "Question 5",
    "type": "Neither agree nor disagree",
    "value": 3,
    "percentage": 12.5,
    "percentage_start": -6.3,
    "percentage_end": 6.3
  },
  {
    "question": "Question 5",
    "type": "Agree",
    "value": 16,
    "percentage": 66.7,
    "percentage_start": 6.3,
    "percentage_end": 72.9
  },
  {
    "question": "Question 5",
    "type": "Strongly agree",
    "value": 4,
    "percentage": 16.7,
    "percentage_start": 72.9,
    "percentage_end": 89.6
  },
  
  {
    "question": "Question 6",
    "type": "Strongly disagree",
    "value": 1,
    "percentage": 6.3,
    "percentage_start": -18.8,
    "percentage_end": -12.5
  },
  {
    "question": "Question 6",
    "type": "Disagree",
    "value": 1,
    "percentage": 6.3,
    "percentage_start": -12.5,
    "percentage_end": -6.3
  },
  {
    "question": "Question 6",
    "type": "Neither agree nor disagree",
    "value": 2,
    "percentage": 12.5,
    "percentage_start": -6.3,
    "percentage_end": 6.3
  },
  {
    "question": "Question 6",
    "type": "Agree",
    "value": 9,
    "percentage": 56.3,
    "percentage_start": 6.3,
    "percentage_end": 62.5
  },
  {
    "question": "Question 6",
    "type": "Strongly agree",
    "value": 3,
    "percentage": 18.8,
    "percentage_start": 62.5,
    "percentage_end": 81.3
  },
  
  {
    "question": "Question 7",
    "type": "Strongly disagree",
    "value": 0,
    "percentage": 0,
    "percentage_start": -10,
    "percentage_end": -10
  },
  {
    "question": "Question 7",
    "type": "Disagree",
    "value": 0,
    "percentage": 0,
    "percentage_start": -10,
    "percentage_end": -10
  },
  {
    "question": "Question 7",
    "type": "Neither agree nor disagree",
    "value": 1,
    "percentage": 20,
    "percentage_start": -10,
    "percentage_end": 10
  },
  {
    "question": "Question 7",
    "type": "Agree",
    "value": 4,
    "percentage": 80,
    "percentage_start": 10,
    "percentage_end": 90
  },
  {
    "question": "Question 7",
    "type": "Strongly agree",
    "value": 0,
    "percentage": 0,
    "percentage_start": 90,
    "percentage_end": 90
  },
  
  {
    "question": "Question 8",
    "type": "Strongly disagree",
    "value": 0,
    "percentage": 0,
    "percentage_start": 0,
    "percentage_end": 0
  },
  {
    "question": "Question 8",
    "type": "Disagree",
    "value": 0,
    "percentage": 0,
    "percentage_start": 0,
    "percentage_end": 0
  },
  {
    "question": "Question 8",
    "type": "Neither agree nor disagree",
    "value": 0,
    "percentage": 0,
    "percentage_start": 0,
    "percentage_end": 0
  },
  {
    "question": "Question 8",
    "type": "Agree",
    "value": 0,
    "percentage": 0,
    "percentage_start": 0,
    "percentage_end": 0
  },
  {
    "question": "Question 8",
    "type": "Strongly agree",
    "value": 2,
    "percentage": 100,
    "percentage_start": 0,
    "percentage_end": 100
  }
]')

Chart

Grouped Bar Chart

Altair example

Chart

Grouped Bar Chart with Error Bars

Altair example

This example shows how to show error bars using confidence intervals. The confidence intervals are computed internally in vega by a non-parametric bootstrap of the mean.

Chart

Horizontal Bar Chart

Altair example

Note that the argument to the transform_filter() function is a JavaScript expression that refers to a variable in the data using the datum. prefix.

Chart

Horizontal Grouped Bar Chart

Altair example

Chart

Horizontal Stacked Bar Chart

Altair example

Chart

Layered Bar Chart

Altair example

Chart

Normalized Stacked Bar Chart

Altair example

Chart

Sorted Bar Chart

Altair example

Chart

Stacked Bar Chart

Altair example

Chart

Stacked Bar Chart with Sorted Segments

Altair example

Chart

Stacked Bar Chart with Text Overlay

Altair example

Chart

Trellis Stacked Bar Chart

Altair example

Chart