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

Our first step is to set up our environment:

Filled step chart

Altair example

Data

glimpse(vega_data$stocks())
#> 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(vega_data$stocks())$
  mark_area(
    color = "lightblue",
    interpolate = "step-after",
    line = TRUE
  )$
  encode(
    x = "date",
    y = "price"
  )$
  transform_filter("datum.symbol == 'GOOG'")

chart

Line chart with Confidence Interval Band

Altair example

This example shows how to make a line chart with a bootstrapped 95% confidence interval band.

Data

glimpse(vega_data$cars())
#> Rows: 406
#> Columns: 9
#> $ Name             <chr> "chevrolet chevelle malibu", "buick skylark 320", "pl…
#> $ Miles_per_Gallon <dbl> 18, 15, 18, 16, 17, 15, 14, 14, 14, 15, NaN, NaN, NaN…
#> $ Cylinders        <dbl> 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 8, 8, 8, 8, 8, 8, 8,…
#> $ Displacement     <dbl> 307, 350, 318, 304, 302, 429, 454, 440, 455, 390, 133…
#> $ Horsepower       <dbl> 130, 165, 150, 150, 140, 198, 220, 215, 225, 190, 115…
#> $ Weight_in_lbs    <dbl> 3504, 3693, 3436, 3433, 3449, 4341, 4354, 4312, 4425,…
#> $ Acceleration     <dbl> 12.0, 11.5, 11.0, 12.0, 10.5, 10.0, 9.0, 8.5, 10.0, 8…
#> $ Year             <dttm> 1970-01-01, 1970-01-01, 1970-01-01, 1970-01-01, 1970…
#> $ Origin           <chr> "USA", "USA", "USA", "USA", "USA", "USA", "USA", "USA…

Chart

cars <- vega_data$cars()

line <- 
  alt$Chart(cars)$
  mark_line()$
  encode(
    x = "Year",
    y = "mean(Miles_per_Gallon)"
  )

confidence_interval <- 
  alt$Chart(cars)$
  mark_area(opacity = 0.3)$
  encode(
    x = "Year",
    y = alt$Y(
      "ci0(Miles_per_Gallon)", 
      axis = alt$Axis(title = "Miles/Gallon")
    ),
    y2 = "ci1(Miles_per_Gallon)"
  )

chart <- (confidence_interval + line)

chart

Line Chart with Layered Aggregates

Altair example

This example shows how to make a multi series line chart of the daily closing stock prices for AAPL, AMZN, GOOG, IBM, and MSFT between 2000 and 2010, along with a layered rule showing the average values.

Data

glimpse(vega_data$stocks())
#> 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

stocks <- vega_data$stocks()

base <- 
  alt$Chart(stocks)$
  properties(
    width = 550,
    title = "Daily closing prices with their aggregate prices"
  )

line <- 
  base$
  mark_line()$
  encode(
    x = "date",
    y = "price",
    color = "symbol"
  )

rule <- 
  base$
  mark_rule()$
  encode(
    y = alt$Y("average(price)"),
    color = "symbol",
    size = alt$value(2)
  )

chart <- (line + rule)

chart

Line Chart with Percent axis

Altair example

Data

jobs <- jsonlite::fromJSON(vega_data$jobs$url)

glimpse(jobs)
#> Rows: 7,650
#> Columns: 5
#> $ job   <chr> "Accountant / Auditor", "Accountant / Auditor", "Accountant / Au…
#> $ sex   <chr> "men", "men", "men", "men", "men", "men", "men", "men", "men", "…
#> $ year  <int> 1850, 1860, 1870, 1880, 1900, 1910, 1920, 1930, 1940, 1950, 1960…
#> $ count <int> 708, 1805, 1310, 2295, 11753, 0, 111209, 181482, 0, 330352, 4250…
#> $ perc  <dbl> 1.309607e-04, 2.140037e-04, 9.977882e-05, 1.253599e-04, 3.958602…

Chart

chart <- 
  alt$Chart(vega_data$jobs$url)$
  mark_line()$
  encode(
    x = "year:O",
    y = alt$Y("perc:Q", axis = alt$Axis(format = "%")),
    color = "sex:N"
  )$
  properties(title = "Percent of work-force working as Welders")$
  transform_filter("datum.job == 'Welder'")

chart

Line Chart with Points

Altair example

Data

source <- tibble(
  x = 1:100,
  `f(x)` = sin(x/5)
)

glimpse(source)
#> Rows: 100
#> Columns: 2
#> $ x      <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, …
#> $ `f(x)` <dbl> 0.19866933, 0.38941834, 0.56464247, 0.71735609, 0.84147098, 0.9…

Chart

chart <- 
  alt$Chart(source)$
  mark_line(point = TRUE)$
  encode(
    x = "x",
    y = "f(x)"
  )

chart

Line Chart with Varying Size

Altair example

Data

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

Chart

chart <- 
  alt$Chart(vega_data$wheat())$
  mark_trail()$
  encode(
    x = "year:T",
    y = "wheat:Q",
    size = "wheat:Q"
  )

chart

Multi Series Line Chart

Altair example

Data

glimpse(vega_data$stocks())
#> 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

A difference from the Python example is that you have to make sure that you declare that the date variable is of type "temporal"

chart <- 
  alt$Chart(vega_data$stocks())$
  mark_line()$
  encode(
    x = "date:T",
    y = "price:Q",
    color = "symbol:N"
  )

chart

Slope Graph

Altair example

Data

The year here is stored by pandas as an integer. When treating columns as dates, it is best to use either a string representation or a datetime representation.

barley <- vega_data$barley()
barley$year <- as.character(barley$year)

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

Chart

chart <- 
  alt$Chart(barley)$
  mark_line()$
  encode(
    x = "year",
    y = "median(yield)",
    color = "site"
  )

chart

Step Chart

Altair example

This example shows Google’s stock price over time. This uses the step-after interpolation scheme. The full list of interpolation options includes linear, linear-closed, step, step-before, step-after, basis, basis-open, basis-closed, cardinal, cardinal-open, cardinal-closed, bundle, and monotone.

Data

glimpse(vega_data$stocks())
#> 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(vega_data$stocks())$
  mark_line(interpolate = "step-after")$
  encode(
    x = "date",
    y = "price"
  )$
  transform_filter(JS("datum.symbol == 'GOOG'"))

chart