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

Our first step is to set up our environment:

Simple Bar Chart

Altair example

Data

Definition

data <-  
  tibble(
    a= c("A", "B", "C", "D", "E", "F", "G", "H", "I"),
    b =c(28, 55, 43, 91, 81, 53, 19, 87, 52)
  )
glimpse(data)
#> Rows: 9
#> Columns: 2
#> $ a <chr> "A", "B", "C", "D", "E", "F", "G", "H", "I"
#> $ b <dbl> 28, 55, 43, 91, 81, 53, 19, 87, 52

Chart

chart <- 
  alt$Chart(data)$
  mark_bar()$
  encode(
    x = "a",
    y = "b"
  )

chart

Simple Heatmap

Altair example

Data

Definition

# Compute x^2 + y^2 across a 2D grid
data <- 
  crossing(x = seq(-5, 5), y = seq(-5, 5)) %>%
  mutate(z = x^2 + y^2)
glimpse(data)
#> Rows: 121
#> Columns: 3
#> $ x <int> -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -4, -4, -4, -4, -4, -4,…
#> $ y <int> -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, -5, -4, -3, -2, -1, 0, 1, 2, …
#> $ z <dbl> 50, 41, 34, 29, 26, 25, 26, 29, 34, 41, 50, 41, 32, 25, 20, 17, 16,…

Chart

chart <- 
  alt$Chart(data)$
  mark_rect()$
  encode(
    x = "x:O",
    y = "y:O",
    color = "z:Q"
  )

chart

Simple Histogram

Altair example

This example shows how to make a basic histogram, based on the vega-lite docs.

Data

movies <- jsonlite::fromJSON("https://vega.github.io/vega-datasets/data/movies.json")
glimpse(movies)
#> Rows: 3,201
#> Columns: 16
#> $ Title                    <chr> "The Land Girls", "First Love, Last Rites", …
#> $ `US Gross`               <int> 146083, 10876, 203134, 373615, 1009819, 2455…
#> $ `Worldwide Gross`        <dbl> 146083, 10876, 203134, 373615, 1087521, 2624…
#> $ `US DVD Sales`           <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
#> $ `Production Budget`      <int> 8000000, 300000, 250000, 300000, 1000000, 16…
#> $ `Release Date`           <chr> "Jun 12 1998", "Aug 07 1998", "Aug 28 1998",…
#> $ `MPAA Rating`            <chr> "R", "R", NA, NA, "R", NA, "R", "R", "R", NA…
#> $ `Running Time min`       <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, …
#> $ Distributor              <chr> "Gramercy", "Strand", "Lionsgate", "Fine Lin…
#> $ Source                   <chr> NA, NA, NA, NA, "Original Screenplay", NA, N…
#> $ `Major Genre`            <chr> NA, "Drama", "Comedy", "Comedy", "Drama", NA…
#> $ `Creative Type`          <chr> NA, NA, NA, NA, "Contemporary Fiction", NA, …
#> $ Director                 <chr> NA, NA, NA, NA, NA, NA, "Christopher Nolan",…
#> $ `Rotten Tomatoes Rating` <int> NA, NA, NA, 13, 62, NA, NA, NA, 25, 86, 81, …
#> $ `IMDB Rating`            <dbl> 6.1, 6.9, 6.8, NA, 3.4, NA, 7.7, 3.8, 5.8, 7…
#> $ `IMDB Votes`             <int> 1071, 207, 865, NA, 165, NA, 15133, 353, 327…

Chart

chart <- 
  alt$Chart(movies)$
  mark_bar()$
  encode(
    alt$X("IMDB Rating:Q", bin = TRUE), 
    y = "count()"
  )

chart