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

Our first step is to set up our environment:

Horizon Graph

Altair example

This shows how a Horizon Graph with two layers. See this reference for more details on Horizon Graphs.

Data

Definition
data <- fromJSON('[
  {"x": 1,  "y": 28}, {"x": 2,  "y": 55},
  {"x": 3,  "y": 43}, {"x": 4,  "y": 91},
  {"x": 5,  "y": 81}, {"x": 6,  "y": 53},
  {"x": 7,  "y": 19}, {"x": 8,  "y": 87},
  {"x": 9,  "y": 52}, {"x": 10, "y": 48},
  {"x": 11, "y": 24}, {"x": 12, "y": 49},
  {"x": 13, "y": 87}, {"x": 14, "y": 66},
  {"x": 15, "y": 17}, {"x": 16, "y": 27},
  {"x": 17, "y": 68}, {"x": 18, "y": 16},
  {"x": 19, "y": 49}, {"x": 20, "y": 15}
]')
glimpse(data)
#> Rows: 20
#> Columns: 2
#> $ x <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
#> $ y <int> 28, 55, 43, 91, 81, 53, 19, 87, 52, 48, 24, 49, 87, 66, 17, 27, 68, …

Chart

area1 <- 
  alt$Chart(data)$
  mark_area(clip = TRUE, interpolate = "monotone")$
  encode(
    x = alt$X("x", scale = alt$Scale(zero = FALSE, nice = FALSE)),
    y = alt$Y(
      "y", 
      scale = alt$Scale(domain = list(0, 50)), 
      axis = alt$Axis(title = "y")
    ),
    opacity = alt$value(0.6)
  )$
  properties(
    width = 500,
    height = 75
  )

area2 <- 
  area1$
  encode(
    y = alt$Y("ny:Q", scale=alt$Scale(domain=list(0, 50)))
  )$
  transform_calculate("ny", "datum.y - 50")

chart <- (area1 + area2)

chart

Interval Selection Example

Altair example

This is an example of creating a stacked chart for which the domain of the top chart can be selected by interacting with the bottom chart.

Data

glimpse(vega_data$sp500())
#> Rows: 123
#> Columns: 2
#> $ date  <dttm> 2000-01-01, 2000-02-01, 2000-03-01, 2000-04-01, 2000-05-01, 200…
#> $ price <dbl> 1394.46, 1366.42, 1498.58, 1452.43, 1420.60, 1454.60, 1430.83, 1…

Chart

brush <- alt$selection(type = "interval", encodings = list("x"))

upper <- 
  alt$Chart(vega_data$sp500$url)$
  mark_area()$
  encode(
    alt$X("date:T", scale = list("domain" = brush$ref())),
    y = "price:Q"
  )$
  properties(
    width = 600,
    height = 200
  )

lower <- 
  upper$
  properties(
    selection = brush,
    height=60
  )

chart <- alt$vconcat(upper, lower)

chart

Layered Area 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-0…
#> $ source         <chr> "Fossil Fuels", "Fossil Fuels", "Fossil Fuels", "Fossil…
#> $ net_generation <dbl> 35361, 35991, 36234, 36205, 36883, 37014, 41389, 42734,…

Chart

chart <- 
  alt$Chart(vega_data$iowa_electricity())$
  mark_area(opacity = 0.3)$
  encode(
    x = "year:T",
    y= alt$Y(
      "net_generation:Q", 
      stack = NULL
    ),
    color = "source:N"
  )

chart

Normalized Stacked Area 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-0…
#> $ source         <chr> "Fossil Fuels", "Fossil Fuels", "Fossil Fuels", "Fossil…
#> $ net_generation <dbl> 35361, 35991, 36234, 36205, 36883, 37014, 41389, 42734,…

Chart

chart <- 
  alt$Chart(vega_data$iowa_electricity())$
  mark_area()$
  encode(
    x = "year:T",
    y= alt$Y(
      "net_generation:Q", 
      stack = "normalize"
    ),
    color = "source:N"
  )

chart

Streamgraph

Altair example

Data

glimpse(vega_data$unemployment_across_industries())
#> Rows: 1,708
#> Columns: 6
#> $ series <chr> "Government", "Government", "Government", "Government", "Govern…
#> $ year   <dbl> 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 200…
#> $ month  <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, …
#> $ count  <dbl> 430, 409, 311, 269, 370, 603, 545, 583, 408, 391, 384, 365, 463…
#> $ rate   <dbl> 2.1, 2.0, 1.5, 1.3, 1.9, 3.1, 2.9, 3.1, 2.1, 2.0, 1.9, 1.8, 2.3…
#> $ date   <dttm> 2000-01-01 08:00:00, 2000-02-01 08:00:00, 2000-03-01 08:00:00,…

Chart

chart <- 
  alt$Chart(vega_data$unemployment_across_industries$url)$
  mark_area()$
  encode(
    x = alt$X(
      "date:T",
      timeUnit = "yearmonth",
      axis = alt$Axis(format = "%Y", domain = FALSE, tickSize = 0)
    ),
    y = alt$Y("sum(count):Q", stack = "center", axis = NULL),
    color = alt$Color("series:N", scale = alt$Scale(scheme = "category20b"))
  )$
  interactive()

chart

Trellis Area Chart

Altair example

Data

source <- vega_data$iowa_electricity()

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

Chart

chart <- 
  alt$Chart(source)$
  mark_area()$
  encode(
    x = "year:T",
    y = "net_generation:Q",
    color = "source:N",
    row = "source:N"
  )$
  properties(
    height = 100
  )

chart

Trellis Area Sort Chart

Altair example

Data

source <- vega_data$stocks()

glimpse(source)
#> Rows: 560
#> Columns: 3
#> $ symbol <chr> "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT", "MSFT",…
#> $ date   <dttm> 2000-01-01, 2000-02-01, 2000-03-01, 2000-04-01, 2000-05-01, 20…
#> $ price  <dbl> 39.81, 36.35, 43.22, 28.37, 25.45, 32.54, 28.40, 28.40, 24.53, …

Chart

chart <- 
  alt$Chart(source)$
  transform_filter("datum.symbol != 'GOOG'")$
  mark_area()$
  encode(
    x = "date:T", 
    y = "price:Q",
    color = "symbol:N",
    row = alt$Row("symbol:N", sort = c('MSFT', 'AAPL', 'IBM', 'AMZN')
    )
  )$
  properties(
    width = 400,
    height = 50
  )

chart