vignettes/example-gallery-01-simple.Rmd
example-gallery-01-simple.Rmd
This document is adapted from the Simple Charts section of the Altair Example Gallery.
Our first step is to set up our environment:
library("altair")
library("reticulate")
library("tibble")
library("tidyr")
library("dplyr")
vega_data <- import_vega_data()
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
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, 3…
#> $ z <dbl> 50, 41, 34, 29, 26, 25, 26, 29, 34, 41, 50, 41, 32, 25, 20, 17, 16, …
This example shows how to make a basic histogram, based on the vega-lite docs.
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, 24551…
#> $ `Worldwide Gross` <dbl> 146083, 10876, 203134, 373615, 1087521, 26245…
#> $ `US DVD Sales` <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, N…
#> $ `Production Budget` <int> 8000000, 300000, 250000, 300000, 1000000, 160…
#> $ `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, N…
#> $ Distributor <chr> "Gramercy", "Strand", "Lionsgate", "Fine Line…
#> $ Source <chr> NA, NA, NA, NA, "Original Screenplay", NA, NA…
#> $ `Major Genre` <chr> NA, "Drama", "Comedy", "Comedy", "Drama", NA,…
#> $ `Creative Type` <chr> NA, NA, NA, NA, "Contemporary Fiction", NA, N…
#> $ 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, 8…
#> $ `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, 3275…
glimpse(data)
#> Rows: 101
#> Columns: 2
#> $ x <int> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,…
#> $ sinx <dbl> 0.00000000, 0.19866933, 0.38941834, 0.56464247, 0.71735609, 0.841…
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 <-
alt$Chart(vega_data$cars())$
mark_circle()$
encode(
x = "Horsepower",
y = "Miles_per_Gallon",
color = "Origin",
tooltip = c("Name", "Horsepower", "Miles_per_Gallon", "Origin")
)$
interactive()
chart
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,…
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…