## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup, warning = FALSE---------------------------------------------------
library(rsgl)
library(duckdb)

con <- dbConnect(duckdb())
dbWriteTable(con, "cars", cars)
dbWriteTable(con, "trees", trees)

## ----cars_sample_query--------------------------------------------------------
dbGetQuery(con, "
  select *
  from cars
  limit 5
")

## ----trees_sample_query-------------------------------------------------------
dbGetQuery(con, "
  select *
  from trees
  limit 5
")

## ----from_cars----------------------------------------------------------------
dbGetPlot(con, "
  visualize
    horsepower as x,
    miles_per_gallon as y
  from cars
  using points
")

## ----from_subquery------------------------------------------------------------
dbGetPlot(con, "
  visualize
    horsepower as x,
    miles_per_gallon as y
  from (
    select *
    from cars
    where origin = 'Japan'
  )
  using points
")

## ----visualize----------------------------------------------------------------
dbGetPlot(con, "
  visualize
    horsepower as x,
    miles_per_gallon as y,
    origin as color
  from cars
  using points
")

## ----histogram----------------------------------------------------------------
dbGetPlot(con, "
  visualize
    bin(miles_per_gallon) as x,
    count(*) as y
  from cars
  group by
    bin(miles_per_gallon)
  using bars
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    bin(miles_per_gallon) as x,
    count(*) as y
  from cars
  group by
    bin(miles_per_gallon)
  using bars
  scale by
    log(x)
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    year as x,
    avg(miles_per_gallon) as y
  from cars
  group by
    year
  using points
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    year as x,
    avg(miles_per_gallon) as y
  from cars
  group by
    year
  using line
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    age as x,
    circumference as y
  from trees
  using line
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    age as x,
    circumference as y
  from trees
  collect by
    tree_id
  using lines
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    age as x,
    circumference as y
  from trees
  using regression line
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    origin as x,
    miles_per_gallon as y
  from cars
  using points
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    origin as x,
    miles_per_gallon as y
  from cars
  using jittered points
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    horsepower as x,
    miles_per_gallon as y
  from cars
  using points

  layer

  visualize
    horsepower as x,
    miles_per_gallon as y
  from cars
  using regression line
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    horsepower as x,
    miles_per_gallon as y
  from cars
  using (
    points
    layer
    regression line
  )
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    horsepower as x,
    miles_per_gallon as y
  from cars
  using (
    points
    layer
    regression line
  )
  scale by
    log(x), log(y)
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    log_hp as x,
    log_mpg as y
  from (
    select
      log(horsepower) as log_hp,
      log(miles_per_gallon) as log_mpg
    from cars
  )
  using (
    points
    layer
    regression line
  )
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    count(*) as y,
    origin as color
  from cars
  group by
    origin
  using bars
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    count(*) as theta,
    origin as color
  from cars
  group by
    origin
  using bars
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    horsepower as x,
    miles_per_gallon as y
  from cars
  using points
  facet by
    origin
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    horsepower as x,
    miles_per_gallon as y
  from cars
  using points
  facet by
    origin vertically
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    horsepower as x,
    miles_per_gallon as y
  from (
    select
      *,
      case
        when year < 1977
        then '< 1977'
        else '>= 1977'
      end as 'era'
    from cars
  )
  using points
  facet by
    era,
    origin
")

## -----------------------------------------------------------------------------
dbGetPlot(con, "
  visualize
    horsepower as x,
    miles_per_gallon as y
  from cars
  using points
  title
    x as 'Horsepower',
    y as 'Miles Per Gallon'
")

