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

Our first step is to set up our environment:

Bar Chart with Highlighted Bar

Altair example

Data

glimpse(vega_data$wheat())
#> Rows: 52
#> Columns: 3
#> $ year  <dbl> 1565, 1570, 1575, 1580, 1585, 1590, 1595, 1600, 1605, 1610, 161…
#> $ wheat <dbl> 41.0, 45.0, 42.0, 49.0, 41.5, 47.0, 64.0, 27.0, 33.0, 32.0, 33.…
#> $ wages <dbl> 5.00, 5.05, 5.08, 5.12, 5.15, 5.25, 5.54, 5.61, 5.69, 5.78, 5.9…

Chart

chart <- 
  alt$Chart(vega_data$wheat())$
  mark_bar()$
  encode(
    x = "year:O",
    y = "wheat:Q",
    # The highlight will be set on the result of a conditional statement
    color=alt$condition(
      "datum.year == 1810",  # If the year is 1810 this test returns True,
      alt$value("orange"),     # which sets the bar orange.
      alt$value("steelblue")   # If it's not True, it sets the bar steelblue.
    )
  )$properties(width=600)

chart

Bar Chart with Labels

Altair example

Data

glimpse(vega_data$wheat())
#> Rows: 52
#> Columns: 3
#> $ year  <dbl> 1565, 1570, 1575, 1580, 1585, 1590, 1595, 1600, 1605, 1610, 161…
#> $ wheat <dbl> 41.0, 45.0, 42.0, 49.0, 41.5, 47.0, 64.0, 27.0, 33.0, 32.0, 33.…
#> $ wages <dbl> 5.00, 5.05, 5.08, 5.12, 5.15, 5.25, 5.54, 5.61, 5.69, 5.78, 5.9…

Chart

bars <- 
  alt$Chart(vega_data$wheat())$
  mark_bar()$
  encode(
    x = "wheat:Q",
    y = "year:O"
  )

text <- 
  bars$
  mark_text(align = "left", baseline = "middle", dx = 3)$
  encode(
    text = "wheat:Q"
  )

chart <- (bars + text)$properties(height = 800)

chart

Bar Chart with Line as Mean

Altair example

Data

glimpse(vega_data$wheat())
#> Rows: 52
#> Columns: 3
#> $ year  <dbl> 1565, 1570, 1575, 1580, 1585, 1590, 1595, 1600, 1605, 1610, 161…
#> $ wheat <dbl> 41.0, 45.0, 42.0, 49.0, 41.5, 47.0, 64.0, 27.0, 33.0, 32.0, 33.…
#> $ wages <dbl> 5.00, 5.05, 5.08, 5.12, 5.15, 5.25, 5.54, 5.61, 5.69, 5.78, 5.9…

Chart

source <- vega_data$wheat()

bar <- 
  alt$Chart(source)$
  mark_bar()$
  encode(
    x = "year:O",
    y = "wheat:Q"
  )

rule <- 
  alt$Chart(source)$
  mark_rule(color = "red")$
  encode(
    y = "mean(wheat):Q"
  )

chart <- (bar + rule)$properties(width = 600)

chart

Bar Chart with Line on Dual Axis

Altair example

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

Data

glimpse(vega_data$wheat())
#> Rows: 52
#> Columns: 3
#> $ year  <dbl> 1565, 1570, 1575, 1580, 1585, 1590, 1595, 1600, 1605, 1610, 161…
#> $ wheat <dbl> 41.0, 45.0, 42.0, 49.0, 41.5, 47.0, 64.0, 27.0, 33.0, 32.0, 33.…
#> $ wages <dbl> 5.00, 5.05, 5.08, 5.12, 5.15, 5.25, 5.54, 5.61, 5.69, 5.78, 5.9…

Chart

base <- 
  alt$Chart(vega_data$wheat())$
  encode(
    x = "year:O"
  )

bar <- 
  base$
  mark_bar()$
  encode(
    y = "wheat:Q"
  )


line <-  
  base$
  mark_line(color = "red")$
  encode(
    y = "wages:Q"
  )

chart <-
  (bar + line)$
  properties(width = 600)

chart

Bar Chart with Negative Values

Altair example

Data

glimpse(vega_data$us_employment())
#> Rows: 120
#> Columns: 24
#> $ month                              <chr> "2006-01-01", "2006-02-01", "2006-…
#> $ nonfarm                            <dbl> 135450, 135762, 136059, 136227, 13…
#> $ private                            <dbl> 113603, 113884, 114156, 114308, 11…
#> $ goods_producing                    <dbl> 22467, 22535, 22572, 22631, 22597,…
#> $ service_providing                  <dbl> 112983, 113227, 113487, 113596, 11…
#> $ private_service_providing          <dbl> 91136, 91349, 91584, 91677, 91735,…
#> $ mining_and_logging                 <dbl> 656, 662, 669, 679, 681, 686, 690,…
#> $ construction                       <dbl> 7601, 7664, 7689, 7726, 7713, 7699…
#> $ manufacturing                      <dbl> 14210, 14209, 14214, 14226, 14203,…
#> $ durable_goods                      <dbl> 8982, 8986, 9000, 9020, 9017, 9028…
#> $ nondurable_goods                   <dbl> 5228, 5223, 5214, 5206, 5186, 5185…
#> $ trade_transportation_utilties      <dbl> 26162, 26196, 26239, 26230, 26223,…
#> $ wholesale_trade                    <dbl> 5840.4, 5854.8, 5873.3, 5886.9, 58…
#> $ retail_trade                       <dbl> 15351.5, 15361.3, 15388.0, 15348.5…
#> $ transportation_and_warehousing     <dbl> 4420.0, 4429.4, 4429.7, 4445.4, 44…
#> $ utilities                          <dbl> 549.8, 550.1, 547.5, 548.9, 548.3,…
#> $ information                        <dbl> 3052, 3052, 3055, 3046, 3039, 3036…
#> $ financial_activities               <dbl> 8307, 8332, 8348, 8369, 8376, 8362…
#> $ professional_and_business_services <dbl> 17299, 17365, 17438, 17462, 17512,…
#> $ education_and_health_services      <dbl> 17946, 17998, 18045, 18070, 18100,…
#> $ leisure_and_hospitality            <dbl> 12945, 12980, 13034, 13074, 13052,…
#> $ other_services                     <dbl> 5425, 5426, 5425, 5426, 5433, 5432…
#> $ government                         <dbl> 21847, 21878, 21903, 21919, 21926,…
#> $ nonfarm_change                     <dbl> 282, 312, 297, 168, 31, 79, 206, 1…

Chart

chart <- 
  alt$Chart(vega_data$us_employment())$
  mark_bar()$
  encode(
    x = "month:T",
    y = "nonfarm_change:Q",
    color = alt$condition(
      "datum.nonfarm_change > 0",  
      alt$value("steelblue"), # positive color
      alt$value("orange")     # negative color
    )
  )$properties(width = 600)

chart

Bar and Tick Chart

Altair example

Data

Definition

data <- 
  data_frame(
    project = c("a", "b", "c", "d", "e", "f", "g"),
    score = c(25, 57, 23, 19, 8, 47, 8),
    goal = c(25, 47, 30, 27, 38, 19, 4)
  )
#> Warning: `data_frame()` is deprecated as of tibble 1.1.0.
#> Please use `tibble()` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_warnings()` to see where this warning was generated.
glimpse(data)
#> Rows: 7
#> Columns: 3
#> $ project <chr> "a", "b", "c", "d", "e", "f", "g"
#> $ score   <dbl> 25, 57, 23, 19, 8, 47, 8
#> $ goal    <dbl> 25, 47, 30, 27, 38, 19, 4

Chart

bar <- 
  alt$Chart(data = data)$
  mark_bar()$
  encode(
    x = "project:O",
    y = "score:Q"
  )$
  properties(width = alt$Step(40))

tick <- 
  alt$Chart(data = data)$
  mark_tick(
    color = "red",
    thickness = 2,
    size = 40 * 0.9
  )$
  encode(
    x = "project:O",
    y = "goal:Q"
  )

chart <- (bar + tick)

chart

Calculating Percentage of Total

Altair example

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

alt.WindowFieldDef(op='sum', field='Time', **{'as': 'TotalTime'})

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

alt$WindowFieldDef(op = "sum", field = "Time", as = "TotalTime")

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

Data

Definition

activities <- 
  data_frame(
    Activity = c("Sleeping", "Eating", "TV", "Work", "Exercise"),
    Time = c(8, 2, 4, 8, 2)
  )
glimpse(activities)
#> Rows: 5
#> Columns: 2
#> $ Activity <chr> "Sleeping", "Eating", "TV", "Work", "Exercise"
#> $ Time     <dbl> 8, 2, 4, 8, 2

Chart

chart <- 
  alt$Chart(activities)$
  mark_bar()$
  encode(
    alt$X("PercentOfTotal:Q", axis = alt$Axis(format = ".0%")),
    y = "Activity:N"
  )$
  transform_window(
    window = list(alt$WindowFieldDef(op = "sum", field = "Time", as = "TotalTime")),
    frame = list(NULL, NULL)
  )$
  transform_calculate(PercentOfTotal = "datum.Time / datum.TotalTime")

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([6557 chars quoted with '''])
glimpse(data)
#> Rows: 40
#> Columns: 6
#> $ question         <chr> "Question 1", "Question 1", "Question 1", "Question …
#> $ type             <chr> "Strongly disagree", "Disagree", "Neither agree nor …
#> $ value            <int> 24, 294, 594, 1927, 376, 2, 2, 0, 7, 11, 2, 0, 2, 4,…
#> $ percentage       <dbl> 0.7, 9.1, 18.5, 59.9, 11.7, 18.2, 18.2, 0.0, 63.6, 0…
#> $ percentage_start <dbl> -19.1, -18.4, -9.2, 9.2, 69.2, -36.4, -18.2, 0.0, 0.…
#> $ percentage_end   <dbl> -18.4, -9.2, 9.2, 69.2, 80.9, -18.2, 0.0, 0.0, 63.6,…

Chart

color_scale <-
  alt$Scale(
    domain = list(
      "Strongly disagree",
      "Disagree",
      "Neither agree nor disagree",
      "Agree",
      "Strongly agree"
    ),
    range = list("#c30d24", "#f3a583", "#cccccc", "#94c6da", "#1770ab")
  )

y_axis <-
  alt$Axis(
    title = "Question",
    offset = 5,
    ticks = FALSE,
    minExtent = 60,
    domain = FALSE
  )

chart <- 
  alt$Chart(data)$
  mark_bar()$
  encode(
    x = "percentage_start:Q",
    x2 = "percentage_end:Q",
    y = alt$Y("question:N", axis = y_axis),
    color = alt$Color(
      "type:N",
      legend = alt$Legend(title = "Response"),
      scale = color_scale
    )
  )

chart

Grouped Bar Chart

Altair example

Data

source <- vega_data$barley()

glimpse(source)
#> Rows: 120
#> Columns: 4
#> $ yield   <dbl> 27.00000, 48.86667, 27.43334, 39.93333, 32.96667, 28.96667, 4…
#> $ variety <chr> "Manchuria", "Manchuria", "Manchuria", "Manchuria", "Manchuri…
#> $ year    <dbl> 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1…
#> $ site    <chr> "University Farm", "Waseca", "Morris", "Crookston", "Grand Ra…

Chart

chart <- 
  alt$Chart(vega_data$barley())$
  mark_bar()$
  encode(
    x = "year:O",
    y = "sum(yield):Q",
    color = "year:N",
    column = "site:N"
  )

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.

Data

source <- vega_data$barley()

glimpse(source)
#> Rows: 120
#> Columns: 4
#> $ yield   <dbl> 27.00000, 48.86667, 27.43334, 39.93333, 32.96667, 28.96667, 4…
#> $ variety <chr> "Manchuria", "Manchuria", "Manchuria", "Manchuria", "Manchuri…
#> $ year    <dbl> 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1…
#> $ site    <chr> "University Farm", "Waseca", "Morris", "Crookston", "Grand Ra…

Chart

bars <- 
  alt$Chart()$
  mark_bar()$
  encode(
    x = "year:O",
    y = alt$Y(
      "mean(yield):Q",
      axis = alt$Axis(title = "Mean Yield")
    ),
    color = "year:N"
  )

error_bars <- 
  alt$Chart()$
  mark_errorbar(extent = "ci")$
  encode(
    x = "year:O",
    y = "yield:Q"
  )

chart <- 
  alt$layer(bars, error_bars, data = source)$
  facet(
    column = "site:N"
  )


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.

Data

glimpse(vega_data$wheat())
#> Rows: 52
#> Columns: 3
#> $ year  <dbl> 1565, 1570, 1575, 1580, 1585, 1590, 1595, 1600, 1605, 1610, 161…
#> $ wheat <dbl> 41.0, 45.0, 42.0, 49.0, 41.5, 47.0, 64.0, 27.0, 33.0, 32.0, 33.…
#> $ wages <dbl> 5.00, 5.05, 5.08, 5.12, 5.15, 5.25, 5.54, 5.61, 5.69, 5.78, 5.9…

Chart

chart <- 
  alt$Chart(vega_data$wheat())$
  mark_bar()$
  encode(
    x = "wheat:Q",
    y = "year:O"
  )$
  properties(height = 700)

chart

Horizontal Grouped Bar Chart

Altair example

Data

glimpse(vega_data$barley())
#> Rows: 120
#> Columns: 4
#> $ yield   <dbl> 27.00000, 48.86667, 27.43334, 39.93333, 32.96667, 28.96667, 4…
#> $ variety <chr> "Manchuria", "Manchuria", "Manchuria", "Manchuria", "Manchuri…
#> $ year    <dbl> 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1…
#> $ site    <chr> "University Farm", "Waseca", "Morris", "Crookston", "Grand Ra…

Chart

chart <- 
  alt$Chart(vega_data$barley())$
  mark_bar()$
  encode(
    x = "sum(yield):Q",
    y = "year:O",
    color = "year:N",
    row = "site:N"
  )

chart

Horizontal Stacked Bar Chart

Altair example

Data

glimpse(vega_data$barley())
#> Rows: 120
#> Columns: 4
#> $ yield   <dbl> 27.00000, 48.86667, 27.43334, 39.93333, 32.96667, 28.96667, 4…
#> $ variety <chr> "Manchuria", "Manchuria", "Manchuria", "Manchuria", "Manchuri…
#> $ year    <dbl> 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1…
#> $ site    <chr> "University Farm", "Waseca", "Morris", "Crookston", "Grand Ra…

Chart

chart <- 
  alt$Chart(vega_data$barley())$
  mark_bar()$
  encode(
    x = "sum(yield)",
    y = "variety",
    color = "site"
  )

chart

Layered Bar Chart

Altair example

Data

glimpse(vega_data$iowa_electricity())
#> Rows: 51
#> Columns: 3
#> $ year           <dttm> 2001-01-01, 2002-01-01, 2003-01-01, 2004-01-01, 2005-…
#> $ source         <chr> "Fossil Fuels", "Fossil Fuels", "Fossil Fuels", "Fossi…
#> $ net_generation <dbl> 35361, 35991, 36234, 36205, 36883, 37014, 41389, 42734…

Chart

chart <- 
  alt$Chart(vega_data$iowa_electricity())$
  mark_bar(opacity = 0.7)$
  encode(
    x = "year:O",
    y = alt$Y("net_generation:Q", stack = NULL),
    color = "source"
  )

chart

Normalized Stacked Bar Chart

Altair example

Data

glimpse(vega_data$barley())
#> Rows: 120
#> Columns: 4
#> $ yield   <dbl> 27.00000, 48.86667, 27.43334, 39.93333, 32.96667, 28.96667, 4…
#> $ variety <chr> "Manchuria", "Manchuria", "Manchuria", "Manchuria", "Manchuri…
#> $ year    <dbl> 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1…
#> $ site    <chr> "University Farm", "Waseca", "Morris", "Crookston", "Grand Ra…

Chart

chart <- 
  alt$Chart(vega_data$barley())$
  mark_bar()$
  encode(
    x = alt$X("sum(yield)", stack = "normalize"),
    y = "variety",
    color = "site"
  )

chart

Sorted Bar Chart

Altair example

glimpse(vega_data$barley())
#> Rows: 120
#> Columns: 4
#> $ yield   <dbl> 27.00000, 48.86667, 27.43334, 39.93333, 32.96667, 28.96667, 4…
#> $ variety <chr> "Manchuria", "Manchuria", "Manchuria", "Manchuria", "Manchuri…
#> $ year    <dbl> 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1…
#> $ site    <chr> "University Farm", "Waseca", "Morris", "Crookston", "Grand Ra…

Chart

chart <- 
  alt$Chart(vega_data$barley())$
  mark_bar()$
  encode(
    x = "sum(yield):Q",
    y = alt$Y(
      "site:N",
      sort = alt$EncodingSortField(
        field = "yield",     # field to use for the sort
        op = "sum",          # operation to run on the field prior to sorting
        order = "descending" # order to sort in
      )
    )
  )

chart

Stacked Bar Chart

Altair example

glimpse(vega_data$barley())
#> Rows: 120
#> Columns: 4
#> $ yield   <dbl> 27.00000, 48.86667, 27.43334, 39.93333, 32.96667, 28.96667, 4…
#> $ variety <chr> "Manchuria", "Manchuria", "Manchuria", "Manchuria", "Manchuri…
#> $ year    <dbl> 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1…
#> $ site    <chr> "University Farm", "Waseca", "Morris", "Crookston", "Grand Ra…

Chart

chart <- 
  alt$Chart(vega_data$barley())$
  mark_bar()$
  encode(
    x = "variety",
    y = "sum(yield)",
    color = "site"
  )

chart

Stacked Bar Chart with Sorted Segments

Altair example

glimpse(vega_data$barley())
#> Rows: 120
#> Columns: 4
#> $ yield   <dbl> 27.00000, 48.86667, 27.43334, 39.93333, 32.96667, 28.96667, 4…
#> $ variety <chr> "Manchuria", "Manchuria", "Manchuria", "Manchuria", "Manchuri…
#> $ year    <dbl> 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1…
#> $ site    <chr> "University Farm", "Waseca", "Morris", "Crookston", "Grand Ra…

Chart

chart <- 
  alt$Chart(vega_data$barley())$
  mark_bar()$
  encode(
    y = "variety",
    x = "sum(yield)",
    color = "site",
    order = alt$Order(
      # Sort the segments of the bars by this field
      "site",
      sort ="ascending"
    )
  )

chart

Stacked Bar Chart with Text Overlay

Altair example

glimpse(vega_data$barley())
#> Rows: 120
#> Columns: 4
#> $ yield   <dbl> 27.00000, 48.86667, 27.43334, 39.93333, 32.96667, 28.96667, 4…
#> $ variety <chr> "Manchuria", "Manchuria", "Manchuria", "Manchuria", "Manchuri…
#> $ year    <dbl> 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1…
#> $ site    <chr> "University Farm", "Waseca", "Morris", "Crookston", "Grand Ra…

Chart

source <- vega_data$barley()

bars <- 
  alt$Chart(source)$
  mark_bar()$
  encode(
    x = alt$X("sum(yield):Q", stack = "zero"),
    y = "variety:N",
    color ="site:N"
  )

text = alt$Chart(source)$
  mark_text(dx = -15, dy = 3, color = "white")$
  encode(
    x =alt$X("sum(yield):Q", stack = "zero"),
    y = "variety:N",
    detail = "site:N",
    text=alt$Text("sum(yield):Q", format = ".1f")
  )

chart <- (bars + text)

chart

Trellis Stacked Bar Chart

Altair example

glimpse(vega_data$barley())
#> Rows: 120
#> Columns: 4
#> $ yield   <dbl> 27.00000, 48.86667, 27.43334, 39.93333, 32.96667, 28.96667, 4…
#> $ variety <chr> "Manchuria", "Manchuria", "Manchuria", "Manchuria", "Manchuri…
#> $ year    <dbl> 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1931, 1…
#> $ site    <chr> "University Farm", "Waseca", "Morris", "Crookston", "Grand Ra…

Chart

chart <- 
  alt$Chart(vega_data$barley())$
  mark_bar()$
  encode(
    x = "yield",
    y = "variety",
    color = "site",
    column = "year"
  )$
  properties(width = 220)

chart