vignettes/example-gallery-03-line-charts.Rmd
example-gallery-03-line-charts.Rmd
This document is adapted from the Line Charts section of the Altair Example Gallery.
Our first step is to set up our environment:
library("altair")
library("tibble")
library("jsonlite")
library("dplyr")
library("readr")
vega_data <- import_vega_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, …
This example shows how to make a line chart with a bootstrapped 95% confidence interval band.
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…
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
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.
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, …
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
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…
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…
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, …
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…
This example shows Google’s stock price over time. This uses the
step-after
interpolation scheme. The full list of interpolation options includeslinear
,linear-closed
,step
,step-before
,step-after
,basis
,basis-open
,basis-closed
,cardinal
,cardinal-open
,cardinal-closed
,bundle
, andmonotone
.
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 <-
alt$Chart(vega_data$stocks())$
mark_line(interpolate = "step-after")$
encode(
x = "date",
y = "price"
)$
transform_filter(JS("datum.symbol == 'GOOG'"))
chart