{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}

{-|
Module      : Graphics.Vega.Tutorials.VegaLite
Copyright   : (c) Douglas Burke, 2019-2021
License     : BSD3

Maintainer  : dburke.gw@gmail.com
Stability   : unstable
Portability : CPP, OverloadedStrings

This tutorial is inspired by - in that it starts off as a close copy of - the
<https://github.com/gicentre/elm-vegalite/tree/master/docs/walkthrough Elm Vega-Lite walkthrough>
created by Jo Wood, and
converted as necessary for the differences between @hvega@ and
<https://package.elm-lang.org/packages/gicentre/elm-vegalite/latest elm-vegalite>.
The Elm tutorial is based on the talk given by
<https://youtu.be/9uaHRWj04D4 Wongsuphasawat et al at the 2017 Open Vis Conf>.

The tutorial targets version 4 of the Vega-Lite specification and
the functionality provided in version @0.12.0.0@ of hvega (although
a number of examples could be simplified by removing the
now-optional type information as of Vega-Lite 4.14).

-}

module Graphics.Vega.Tutorials.VegaLite (

  -- * A Grammar of Graphics
  --
  -- $intro

  -- ** How many Haskell extensions do you need?
  --
  -- $intro-extensions

  -- ** A note on type safety
  --
  -- $intro-type-safety

  -- ** Comparing hvega to Elm Vega-Lite
  --
  -- $compare-to-elm

  -- * What data are we using?
  --
  -- $datasource

  -- * Creating the Vega-Lite visualization
  --
  -- $output

  -- * A Strip Plot
  --
  -- $singleview-stripplot

  -- ** Our first hvega plot
  --
  stripPlot

  -- ** Backgrounds
  --
  -- $background-note

  , stripPlotWithBackground

  -- ** Challenging the primacy of the x axis
  --
  , stripPlotY

  -- ** Data sources
  --

  , gaiaData

  -- ** Adding color as an encoding

  , stripPlotWithColor
  , stripPlotWithColor2

  -- ** Comparing Ordinal with Nominal data types
  --
  -- $stripplot-mmtype

  , stripPlotWithColorOrdinal

  -- * A Pie Chart
  --
  -- $pie-chart

  , pieChart
  , pieChartWithCounting

  -- * Adding an axis
  --
  -- $add-axis

  , parallaxBreakdown

  -- ** Creating a value to plot: aggregating data
  --
  -- $histogram

  , simpleHistogram
  , parallaxHistogram
  , gmagHistogram

  -- ** Changing the scale of an axis
  --
  -- $ylog-histogram

  , ylogHistogram

  -- ** Stacked Histogram

  , gmagHistogramWithColor
  , gmagLineWithColor

  -- ** You don't have to just count
  --
  -- $histogramChoice

  , yHistogram

  , starCount
  , starCount2

  , densityParallax
  , densityParallaxGrouped

  -- ** Plotting with points
  --
  -- $intro-points

  , pointPlot

  -- * Making a map
  --
  -- $intro-map

  , posPlot

  -- ** Using a projection

  , skyPlot

  -- ** Choropleth with joined data

  -- $intro-choropleth

  , choroplethLookupToGeo

  -- * Layered and Multi-View Compositions
  --
  -- $intro-layered

  , smallMultiples
  , smallMultiples2

  , densityMultiples

  -- ** One plot, two plot, red plot, blue plot
  --
  -- $intro-multiplot

  , basePlot

  -- ** Composing layers

  , layeredPlot
  , layeredDiversion
  , layeredCount
  , skyPlotWithGraticules

  -- ** Concatenating views

  , concatenatedPlot
  , concatenatedPlot2
  , concatenatedSkyPlot

  -- ** Repeated views
  --
  -- $intro-repeat

  -- *** Varying fields field

  , repeatPlot

  -- *** Repeating Choropleths

  , choroplethLookupFromGeo

  -- *** Rows and Columns
  , splomPlot

  -- * Interactivity
  --
  -- $intro-interactivity

  , selectionProperties
  , singleSelection
  , nearestSelection
  , multiSelection
  , eventSelection
  , intervalSelection
  , intervalSelectionY

  -- ** Selection Transformations
  --
  -- $intro-selection-transforms

  , transformSelection

  -- *** Selection and bindings
  --
  -- $intro-selection-binding

  , legendSelection
  , widgetSelection
  , bindScales

  -- *** Multiple Coordinated Views
  --
  -- $intro-coordinated-views

  , coordinatedViews
  , coordinatedViews2

  , contextAndFocus

  -- *** Cross-filtering
  --
  -- $intro-crossfilter

  , crossFilter

  -- * Smoothing and Regressing
  --
  -- $intro-smoothing

  , loessExample
  , regressionExample

  -- * Errors: lines, bars, bands, and boxes
  --
  -- $intro-error

  , errorManual
  , errorAuto

  , errorBars
  , errorBand
  , errorBox

  , comparingErrors

  -- * Dashboard-esque
  --
  -- $intro-dashboard

  , combinedPlot

  -- * The end
  --
  -- $otherstuff

  , duplicateAxis
  , compareCounts
  , parallaxView

  -- ** Aitoff projections
  --
  -- $aitoff

  , skyPlotAitoff
  , clusterCenters

  ) where

import qualified Data.Text as T

import Prelude hiding (filter, lookup, repeat)

#if !(MIN_VERSION_base(4, 12, 0))
import Data.Monoid ((<>))
#endif

import Graphics.Vega.VegaLite

-- $intro
-- hvega is a wrapper for the [Vega-Lite visualization
-- grammar](https://vega.github.io/) which itself is based on Leland
-- Wilkinson's [Grammar of
-- Graphics](http://www.springer.com/gb/book/9780387245447).  The
-- grammar provides an expressive way to define how data are
-- represented graphically. The seven key elements of the grammar as
-- represented in hvega and Vega-Lite are:
--
-- [@Data@]: The input to visualize. Example functions: 'dataFromUrl', 'dataFromColumns', and 'dataFromRows'.
--
-- [@Transform@]: Functions to change the data before they are visualized. Example functions: 'filter', 'calculateAs', 'binAs', 'pivot', 'density', and
-- 'regression'. These functions are combined with 'transform'.
--
-- [@Projection@]: The mapping of 3d global geospatial locations onto a 2d plane . Example function: 'projection'.
--
-- [@Mark@]: The visual symbol, or symbols, that represent the data. Example types, used with 'mark': 'Line', 'Circle', 'Bar', 'Text', and 'Geoshape'. There are also ways to specify the shape to use for the 'Point' type, using the 'MShape' setting and the 'Symbol' type.
--
-- [@Encoding@]: The specification of which data elements are mapped to which mark characteristics (commonly known as channels). Example functions: 'position', 'shape', 'size', and 'color'. These encodings are combined with 'encoding'.
--
-- [@Scale@]: Descriptions of the way encoded marks represent the data. Example settings: 'SDomain', 'SPadding', and 'SInterpolate'.
--
-- [@Guides@]: Supplementary visual elements that support interpreting the visualization. Example setings: 'AxDomain' (for position encodings) and 'LeTitleColor' (for legend color, size, and shape encodings).
--
-- In common with other languages that build upon a grammar of graphics
-- such as D3 and Vega, this grammar allows fine grain control of
-- visualization design. Unlike those languages, Vega-Lite - and
-- hvega in turn - provide practical default specifications for most of the
-- grammar, allowing for a much more compact high-level form of
-- expression.
--
-- The [Vega-Lite Example Gallery](https://vega.github.io/vega-lite/examples/)
-- provides a large-number of example visualizations that show off
-- the capabilities of Vega-Lite. Hopefully, by the end of this
-- tutorial, you will be able to create most of them.

-- $intro-extensions
-- The 'Graphics.Vega.VegaLite' module exports a large number of symbols,
-- but does not use any complex type machinery, and so it can be loaded
-- without any extensions, although the extensive use of the 'Data.Text.Text'
-- type means that using the @OverloadedStrings@ extension is __strongly__
-- advised.
--
-- The module does export several types that conflict with the Prelude,
-- so one suggestion is to use
--
--     > import Prelude hiding (filter, lookup, repeat)

-- $intro-type-safety
-- The interface provided by @hvega@ provides __limited__ type safety. Various
-- fields such as 'PmType' are limited by the type of the argument (in this
-- case 'Measurement'), but there's no support to check that the type makes
-- sense for the particular column (as @hvega@ itself does not inspect the
-- data source). Similarly, @hvega@ does not stop you from defining
-- properties that are not valid for a given situation - for instance
-- you can say @'toVegaLite' []@ even though the output is not a
-- valid Vega-Lite specification (i.e. it does not validate against
-- the [Vega-Lite schema](https://github.com/vega/schema)).
--
-- Version @0.5.0.0@ did add some type safety for a number of functions -
-- primarily 'encoding' and 'transform' - as the types they accept
-- have been restricted (to @['EncodingSpec']@ and @['TransformSpec']@
-- respectively), so that they can not be accidentally combined.

-- $compare-to-elm
-- @hvega@ started out as a direct copy of
-- [elm-vegalite](https://package.elm-lang.org/packages/gicentre/elm-vegalite/latest),
-- and has been updated to try and match the functionality of that package.
-- However, @hvega@ has not (yet?) followed @elm-vegalite@ into using
-- functions rather than data structures to define the options: for
-- example, @elm-vegalite@ provides @pQuant n@ which in @hvega@ is the
-- combination of @'PName' n@ and @'PmType' 'Quantitative'@ in @hvega@.
-- The top-level functions - such as 'dataFromUrl', 'encoding', and
-- 'filter' - are generally the same. As the VegaLite schema has expanded
-- over time the differences between the two approaches has also grown.
--
-- Version @0.5.0.0@ does introduce more-significant changes, in that
-- there are now separate types for a number of functions - such as
-- 'encoding', 'transform', and 'select' - to help reduce the
-- chance of creating invalid visualizations.

-- $datasource
-- Rather than use the Seattle weather dataset, used in the Elm walkthrough
-- (if you go through the [Vega-Lite Example Gallery](https://vega.github.io/vega-lite/examples/)
-- you may also want to look at different data ;-), I am going to use a
-- small datset from the [Gaia satellite](http://sci.esa.int/gaia/),
-- which has - and still is, as of early 2020 - radically-improved our knowledge
-- of our Galaxy. The data itself is from the paper
-- \"Gaia Data Release 2: Observational Hertzsprung-Russell diagrams\"
-- [(preprint on arXiV)](https://arxiv.org/abs/1804.09378)
-- [(NASA ADS link)](https://ui.adsabs.harvard.edu/#abs/arXiv:1804.09378).
-- We are going to use Table 1a, which was downloaded from the
-- [VizieR archive](http://vizier.u-strasbg.fr/viz-bin/VizieR-3?-source=J/A%2bA/616/A10/tablea1a)
-- as a tab-separated file (aka 'TSV' format).
--
-- The file contains basic measurements for a number of stars in
-- nine open clusters that all lie within 250 parsecs of the Earth
-- (please note, a parsec is a measure of distance, not time, no matter
-- what [some ruggedly-handsome ex-carpenter](https://en.wikipedia.org/wiki/Han_Solo)
-- might claim). The downloaded file is called
-- @gaia-aa-616-a10-table1a.no-header.tsv@, although I have
-- manually edited it to a \"more standard\" TSV form (we Astronomers like
-- our metadata, and tend to stick it in inappropriate places, such as the
-- start of comma- and tab-separated files, which really mucks up
-- other-people's parsing code). The first few rows in the file are:
--
--
-- +-------------------+---------+-----------+-----------+--------+--------+-------+
-- | Source            | Cluster | RA_ICRS   | DE_ICRS   | Gmag   | plx    | e_plx |
-- +===================+=========+===========+===========+========+========+=======+
-- | 49520255665123328 | Hyades  | 064.87461 | +21.75372 | 12.861 | 20.866 | 0.033 |
-- +-------------------+---------+-----------+-----------+--------+--------+-------+
-- | 49729231594420096 | Hyades  | 060.20378 | +18.19388 | 5.790  | 21.789 | 0.045 |
-- +-------------------+---------+-----------+-----------+--------+--------+-------+
-- | 51383893515451392 | Hyades  | 059.80696 | +20.42805 | 12.570 | 22.737 | 0.006 |
-- +-------------------+---------+-----------+-----------+--------+--------+-------+
-- | ...               | ...     | ...       | ...       | ...    | ...    | ...   |
-- +-------------------+---------+-----------+-----------+--------+--------+-------+
--
-- The @Source@ column is a numeric identifier for the star in the Gaia database,
-- in this particular case the [\"DR2\" release](https://www.cosmos.esa.int/web/gaia/dr2),
-- the @Cluster@ column tells us which [Star Cluster](https://en.wikipedia.org/wiki/Star_cluster)
-- the star belongs to, @RA_ICRS@ and @DE_ICRS@
-- [locate the star on the sky](https://en.wikipedia.org/wiki/Celestial_coordinate_system)
-- and use the [Equatorial coordinate system](https://en.wikipedia.org/wiki/Equatorial_coordinate_system)
-- (the @ICRS@ term has a meaning too, but it isn't important for our
-- purposes),
-- @Gmag@ measues the "brightness" of the star (as in most-things Astronomical,
-- this is not as obvious as you might think, as I'll go into below),
-- and the @plx@ and @e_plx@ columns give the measured
-- [parallax of the star](https://en.wikipedia.org/wiki/Equatorial_coordinate_system)
-- and its error value, in units of
-- milli [arcseconds](https://en.wikipedia.org/wiki/Minute_and_second_of_arc).
-- And yes, I do realise after complaining about popular-culture references
-- confusing distances and time, I am now measuring distances with angles.
-- I think I've already mentioned that Astronomy is confusing...

-- $output
-- The function 'toVegaLite' takes a list of grammar specifications,
-- as will be shown in the examples below, and creates a single JSON object
-- that encodes the entire design. As of @hvega-0.5.0.0@ this targets
-- version 4 of the Vega-Lite schema, but this can be over-ridden with
-- 'toVegaLiteSchema' if needed (although note that this just changes the
-- version number in the schema field, it does not change the output to
-- match a given version).
--
-- There is no concept of ordering to these specification lists, in that
-- @[ dataFromUrl ..., encoding ..., mark ...]@;
-- @[ encoding ..., dataFromUrl ..., mark ... ]@;
-- and
-- @[ encoding ..., mark ..., dataFromUrl ... ]@
-- would all result in the same visualization.
--
-- The output of 'toVegaLite' can be sent to the Vega-Lite runtime to
-- generate the Canvas or SVG output. @hvega@ contains the helper
-- routines:
--
--  * 'fromVL', which is used to extract the JSON contents from 'VegaLite'
--    and create an Aeson 'Data.Aeson.Value';
--
--  * 'toHtml', which creates a HTML page which uses the
--    <https://github.com/vega/vega-embed Vega Embed> Javascript
--    library to display the Vega-Lite visualization;
--
--  * and 'toHtmlFile', which is like 'toHtml' but writes the output
--    to a file.

-- $singleview-stripplot
-- In this section we shall concentrate on creating a single
-- plot. Later on we shall try combining plots, after branching
-- out to explore some of the different ways to visualize
-- multi-dimensional data sets.
--
-- In the examples I link to symbols that have not been used in
-- previous visualizations, to make it easier to see the use
-- of new functionality.

{-|

We could encode one of the numeric data fields as a strip plot where
the horizontal position of a tick mark is determined by the value
of the data item. In this case I am going to pick the "@plx@" column:

<<images/vl/stripplot.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4IgtghgTg1iBcIAuBLAxnANCAJhJECoArlADYIgAWSSADgM7wD0zUEA7gHQDmKSVYgCNiDAKZQ0AewB2SMXK7SwzACJTiPAEKkYY5lQBuYnhGaQG8qAeOnmeAs1MoIAWghuAbAEZP77wAMrgRCZGLeEFwyUq5UYhA4ElxIDIYg2ABmUlCQSETIAJ50YpQpaQC+5dgAJAxocZCUNPRMrLaRfALCXChSzHUNZu2uZPz6hgAsXABWDLLpIArSOCgyPPkAHvkZKGJkOJR0ZFvYSEUliACOxBBy-PgoxiCV5UA Open this visualization in the Vega Editor>

@
'toVegaLite'
    [ 'dataFromUrl' "https:\/\/raw.githubusercontent.com\/DougBurke\/hvega\/master\/hvega\/data\/gaia-aa-616-a10-table1a.no-header.tsv" ['TSV']
    , 'mark' 'Tick' []
    , 'encoding' ('position' 'X' [ 'PName' "plx", 'PmType' 'Quantitative' ] [])
    ]
@

Notice how there is __no explicit definition__ of the axis details,
color choice or size. These can be customised, as shown in examples
below, but the default values are designed to follow good practice in
visualization design.

Three grammar elements are represented by the three functions
'dataFromUrl', 'mark', and 'encoding'.

The 'encoding' function takes as a single parameter, a list of
specifications that are themselves generated by other functions. In
this case we use the function 'position' to provide an encoding of the
@\"plx\"@ field as the x-position in our plot. The precise way in which
the data value (parallax) is mapped to the x-position will depend on the type of
data we are encoding. We can provide a hint by declaring the
measurement type of the data field, here 'Quantitative' indicating a
numeric measurement type. The final parameter of position is a list of
any additional encodings in our specification. Here, with only one
encoding, we provide an empty list.

As we build up more complex visualizations we will use many more
encodings. To keep the coding clear, the idiomatic way to do this with
hvega is to chain encoding functions using point-free
style. The example above coded in this way would be

@
let enc = encoding
            . position X [ PName "plx", PmType Quantitative ]

in toVegaLite
    [ dataFromUrl "https:\/\/raw.githubusercontent.com\/DougBurke\/hvega\/master\/hvega\/data\/gaia-aa-616-a10-table1a.no-header.tsv" [TSV]
    , mark Tick []
    , enc []
    ]
@

-}

stripPlot :: VegaLite
stripPlot :: VegaLite
stripPlot = [PropertySpec] -> VegaLite
toVegaLite
    [ Text -> [Format] -> PropertySpec
dataFromUrl Text
"https://raw.githubusercontent.com/DougBurke/hvega/master/hvega/data/gaia-aa-616-a10-table1a.no-header.tsv" [Format
TSV]
    , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Tick []
    , [EncodingSpec] -> PropertySpec
encoding (Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"plx", Measurement -> PositionChannel
PmType Measurement
Quantitative ] [])
    ]

-- $background-note
-- The default background color for the visualization, at least in the
-- Vega-Embed PNG and SVG output, is white (in Vega-Lite version 4;
-- prior to this it was transparent). In many cases this is
-- perfectly fine, but an explicit color can be specified using the
-- 'BackgroundStyle' configuration option, as shown here, or with the
-- 'background' function, which is used in the choropleth examples
-- below ('choroplethLookupToGeo').

{-|

The 'configure' function allows a large number of configuration
options to be configured, each one introduced by the
'configuration' function. Here I set the color to be a light gray
(actually a very-transparent black; the 'Color' type describes the
various supported color specifications, but it is generally safe to assume
that if you can use it in HTML then you can use it here).

<<images/vl/stripplotwithbackground.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4Igxg9gdgZglgcxALlAIwIZgNYIE4QCuUAJiiHgpgBQAMANAAQPNO0B0AjAJQgC+9EAFsMebOQAucHCEEkMEjClCE8AG3IALCRIAOAZ2QB6I3gwB3dgjgTNhNIX0BTPJCgSn79pCFGAIkQIAEKq2E5GmgBuTggYRiL6HngR0bFG8opGsXAYALQYeQBsnIX5nLS5imhqTpwY7FAQuZpOGCQu7BL6kbIgMBB4IhLKIBIAnrpOkt38AiAAJPpgLSJaOgbGRqn11rb27HAQRksrcdu5ajbhkQAs7ABW+tC9npAkcFBIqCAAHiPwTjUZGQIF0aj+gnGk3IAEdCBh3DYFHBorM+EA Open this visualization in the Vega Editor>

@
let enc = encoding
            . position X [ PName \"plx\", PmType Quantitative ]

    conf = 'configure'
             . 'configuration' ('BackgroundStyle' "rgba(0, 0, 0, 0.1)")

in toVegaLite
    [ dataFromUrl \"https:\/\/raw.githubusercontent.com\/DougBurke\/hvega\/master\/hvega\/data\/gaia-aa-616-a10-table1a.no-header.tsv\" [TSV]
    , mark Tick []
    , enc []
    , conf []
    ]
@

If you want a transparent background (as was the default with Vega-Lite 3
and earlier), you would use

@
'configuration' ('BackgroundStyle' "rgba(0, 0, 0, 0)")
@

-}

stripPlotWithBackground :: VegaLite
stripPlotWithBackground :: VegaLite
stripPlotWithBackground = [PropertySpec] -> VegaLite
toVegaLite
    [ Text -> [Format] -> PropertySpec
dataFromUrl Text
"https://raw.githubusercontent.com/DougBurke/hvega/master/hvega/data/gaia-aa-616-a10-table1a.no-header.tsv" [Format
TSV]
    , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Tick []
    , [EncodingSpec] -> PropertySpec
encoding (Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"plx", Measurement -> PositionChannel
PmType Measurement
Quantitative ] [])
    , [ConfigureSpec] -> PropertySpec
configure (ConfigurationProperty -> BuildConfigureSpecs
configuration (Text -> ConfigurationProperty
BackgroundStyle Text
"rgba(0, 0, 0, 0.1)") [])
    ]


{-|
There is nothing that forces us to use the x axis, so let's
try a vertical strip plot. To do so requires changing only
__one__ character in the specifiction, that is the first argument to
'position' is now 'Y' rather than 'X':

<<images/vl/stripploty.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4IgtghgTg1iBcIAuBLAxnANCAJhJECoArlADYIgAWSSADgM7wD0zUEA7gHQDmKSVYgCNiDAKZQ0AewB2SMXK7SwzACJTiPAEKkYY5lQBuYnhGaQG8qAeOnmeAs1MoIAWghuAbAEZP77wAMrgRCZGLeEFwyUq5UYhA4ElxIDIYg2ABmUlCQSETIAJ50YpQpaQC+5dgAJAxocZCUNPRMrLaRfALCXChSzHUNZu2uZPz6hgAsXABWDLLpIArSOCgyPPkF+RkoYmQ4lHRkAB4LSEUliACOxBBy-PgoxiCV5UA Open this visualization in the Vega Editor>

@
let enc = encoding
            . position 'Y' [ PName "plx", PmType Quantitative ]

in toVegaLite
    [ dataFromUrl \"https:\/\/raw.githubusercontent.com\/DougBurke\/hvega\/master\/hvega\/data\/gaia-aa-616-a10-table1a.no-header.tsv\" [TSV]
    , mark Tick []
    , enc []
    ]
@
-}
stripPlotY :: VegaLite
stripPlotY :: VegaLite
stripPlotY = [PropertySpec] -> VegaLite
toVegaLite
    [ Text -> [Format] -> PropertySpec
dataFromUrl Text
"https://raw.githubusercontent.com/DougBurke/hvega/master/hvega/data/gaia-aa-616-a10-table1a.no-header.tsv" [Format
TSV]
    , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Tick []
    , [EncodingSpec] -> PropertySpec
encoding (Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"plx", Measurement -> PositionChannel
PmType Measurement
Quantitative ] [])
    ]


{-|
Since we are going to be using the same data source, let's define it here:

@
gaiaData =
  let addFormat n = (n, 'FoNumber')
      cols = [ \"RA_ICRS\", \"DE_ICRS\", \"Gmag\", \"plx\", \"e_plx\" ]
      opts = [ 'Parse' (map addFormat cols) ]

  in dataFromUrl "https:\/\/raw.githubusercontent.com\/DougBurke\/hvega\/master\/hvega\/data\/gaia-aa-616-a10-table1a.no-header.tsv" opts
@

The list argument to 'dataFromUrl' allows for some customisation of
the input data.  Previously I used @['TSV']@ to specify the data is in
tab-separated format, but it isn't actually needed here (since the
file name ends in \".tsv\"). However, I have now explicitly defined how
to parse the numeric columns using 'Parse': this is because the columns
are read in as strings for this file by default, which actually doesn't
cause any problems in most cases, but did cause me significant problems
at one point during the development of the tutorial! There is limited
to no feedback from the visualizer for cases like this (perhaps I should
have used the Javascript console), and I only realised the problem thanks
to the @Data Viewer@ tab in the
<https://vega.github.io/editor/#/ Vega Editor>
(after a
<https://twitter.com/pkgw/status/1167127390880968707 suggestion from a colleague>).

Data can also be defined algorithmically - using 'dataSequence' and
'dataSequenceAs' - or inline - with 'dataFromColumns' or
'dataFromRows' - or directly from JSON (as a 'Data.Aeson.Value') using
'dataFromJson'.

Examples showing 'dataFromColumns' are the 'pieChart' and 'skyPlotWithGraticules' plots,
but let's not peak ahead!

-}

gaiaData :: Data
gaiaData :: PropertySpec
gaiaData =
  let addFormat :: a -> (a, DataType)
addFormat a
n = (a
n, DataType
FoNumber)
      cols :: [Text]
cols = [ Text
"RA_ICRS", Text
"DE_ICRS", Text
"Gmag", Text
"plx", Text
"e_plx" ]
      opts :: [Format]
opts = [ [(Text, DataType)] -> Format
Parse ((Text -> (Text, DataType)) -> [Text] -> [(Text, DataType)]
forall a b. (a -> b) -> [a] -> [b]
map Text -> (Text, DataType)
forall a. a -> (a, DataType)
addFormat [Text]
cols) ]

  in Text -> [Format] -> PropertySpec
dataFromUrl Text
"https://raw.githubusercontent.com/DougBurke/hvega/master/hvega/data/gaia-aa-616-a10-table1a.no-header.tsv" [Format]
opts


{-|

One question would be how the parallaxes vary by cluster: as parallax is measuring distance,
then are the clusters similar distances away from us, or is there a range of values? A
first look is to use another \"channel\" to represent (i.e. encode) the cluster:

<<images/vl/stripplotwithcolor.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4IgtghgTg1iBcIAuBLAxnANCAJhJECoArlADYIgAWSSADgM7wD0zUEA7gHQDmKSVYgCNiDAKZQ0AewB2SMXK7SwzACJTiPAEKkYY5lQBuYnhGaQG8qAeOnmeAs1MoIAWghuAbAEZP77wAMrgRCZGLeEFwyUq5UYhA4ElxIDIYg2ABmUlCQSEQgdNDi+QDikDyUMsRgQhLpIABKAIIA+gCSAMINAMqV1bVQ9XRkAB59NXXYqgCi7V29iFUTg9hiLcNji-11AL572AAkDGhxkJQ09EystpF8AsJcKFLMx6dmN65k-PqGACxcACsGLJ6gppDgUDIKvBQNIyNl8hkUGIyDhKB0yKIrPUkABPOhiSpSMCQiAUHbYTagJEotGIDY4-GExAAR2IEDk-HwKGM9QgIxQDHyqCQYUoAAVoGSyPyAAQACgsAEoQHs9kA Open this visualization in the Vega Editor>

@
let enc = encoding
            . position X [ PName \"plx\", PmType Quantitative, 'PAxis' [ 'AxTitle' \"Parallax (mas)\" ] ]
            . 'color' [ 'MName' \"Cluster\", MmType 'Nominal' ]

in toVegaLite
    [ gaiaData
    , mark Tick []
    , enc []
    ]
@

Now each tick mark is colored by the cluster, and a legend is automatically
added to indicate this mapping. Fortunately the number of clusters in the
sample is small enough to make this readable! The 'color' function has
added this mapping, just by giving the column to use (with 'MName') and
its type ('MmType'). The constructors generally begin with @P@ for
position and @M@ for mark, and as we'll see there are other property
types such as facet and text.

Vega-Lite supports several data types, represented
by the 'Measurement' type. We have already seen 'Quantitative', which
is used for numeric data, and here we use 'Nominal' for the clusters,
since they have no obvious ordering.

The labelling for the X axis has been tweaked using 'PAxis', in this
case the default value for the label (the column name) has been
over-ridden by an explicit value.

-}

stripPlotWithColor :: VegaLite
stripPlotWithColor :: VegaLite
stripPlotWithColor =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
            ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"plx", Measurement -> PositionChannel
PmType Measurement
Quantitative, [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"Parallax (mas)" ] ]
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal ]

  in [PropertySpec] -> VegaLite
toVegaLite
     [ PropertySpec
gaiaData
     , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Tick []
     , [EncodingSpec] -> PropertySpec
enc []
     ]

{-|

As of Vega-Lite version 4.14 we can now drop the type information when
it can be inferred. I am a little hazy of the rules, so I am going to
include the information (as it also means I don't have to change
the existing code!). However, as an example, we don't need to
add the @MmType Nominal@ setting to the 'color' channel, since the
following creates the same visualization as 'stripPlotWithColor':

<https://vega.github.io/editor/#/url/vega-lite/N4IgtghgTg1iBcIAuBLAxnANCAJhJECoArlADYIgAWSSADgM7wD0zUEA7gHQDmKSVYgCNiDAKZQ0AewB2SMXK7SwzACJTiPAEKkYY5lQBuYnhGaQG8qAeOnmeAs1MoIAWghuAbAEZP77wAMrgRCZGLeEFwyUq5UYhA4ElxIDIYg2ABmUlCQSEQgdNDi+QDikDyUMsRgQhLpIABKAIIA+gCSAMINAMqV1bVQ9XRkAB59NXXYqgCi7V29iFUTg9hiLcNji-11AL572AAkDGhxkJQ09EystpF8AsJcKFLMx6dmN65k-PqGACxcACsGLJ6gppDgUDIKvBQNIyNl8hkUGIyDhKB0yKIrCAdthNqAkSi0YgNvVUEgwpQAArQCBkMgQEYAAgAFBYAJRkgCedDElAAjsQIHJ+PgUMYcXsgA Open this visualization in the Vega Editor>

@
let enc = encoding
            . position X [ PName \"plx\", PmType Quantitative, 'PTitle' \"Parallax (mas)\" ]
            . 'color' [ 'MName' \"Cluster\" ]

in toVegaLite
    [ gaiaData
    , mark Tick []
    , enc []
    ]
@

Note that as well as removing @MmType Nominal@ from the 'color' encoding, I have
switched to the 'PTitle' option (which is the same as @PAxis [AxTitle ...]@.

-}

stripPlotWithColor2 :: VegaLite
stripPlotWithColor2 :: VegaLite
stripPlotWithColor2 =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
            ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"plx", Measurement -> PositionChannel
PmType Measurement
Quantitative, Text -> PositionChannel
PTitle Text
"Parallax (mas)" ]
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster" ]

  in [PropertySpec] -> VegaLite
toVegaLite
     [ PropertySpec
gaiaData
     , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Tick []
     , [EncodingSpec] -> PropertySpec
enc []
     ]


-- $stripplot-mmtype
-- It is instructive to see what happens if we change the mark type for
-- the color encoding from 'Nominal' to 'Ordinal'.

{-|

<<images/vl/stripplotwithcolorordinal.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4IgtghgTg1iBcIAuBLAxnANCAJhJECoArlADYIgAWSSADgM7wD0zUEA7gHQDmKSVYgCNiDAKZQ0AewB2SMXK7SwzACJTiPAEKkYY5lQBuYnhGaQG8qAeOnmeAs1MoIAWghuAbAEZP77wAMrgRCZGLeEFwyUq5UYhA4ElxIDIYg2ABmUlCQSEQgdNDi+QDikDyUMsRgQhLpIABKAIIA+gCSAMINAMqV1bVQ9XRkAB59NXXYqgCi7V29iFUTg9hiLcNji-11AL572AAkDGhxkJQ09EystpF8AsJcKFLMx6dmN65k-PqGACxcACsGLJ6gppDgUDIKvBQNIyNl8hkUGIyDhKB0yKIrPUkABPOhiSjZCEyCAUHbYTagJEotGIDY4-GExAAR2IEDk-HwKGM9QgIxQDHyqCQYUoAAVoGSyPyAAQACgsAEoQHs9kA  Open this visualization in the Vega Editor>

@
let enc = encoding
            . position X [ PName \"plx\", PmType Quantitative, PAxis [ AxTitle \"Parallax (mas)\" ] ]
            . color [ MName \"Cluster\", MmType 'Ordinal' ]

in toVegaLite
    [ gaiaData
    , mark Tick []
    , enc []
    ]
@

As can be seen, the choice of color scale has changed to one more
appropriate for an ordered set of values.
-}
stripPlotWithColorOrdinal :: VegaLite
stripPlotWithColorOrdinal :: VegaLite
stripPlotWithColorOrdinal =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
            ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"plx", Measurement -> PositionChannel
PmType Measurement
Quantitative, [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"Parallax (mas)" ] ]
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Ordinal ]

  in [PropertySpec] -> VegaLite
toVegaLite
     [ PropertySpec
gaiaData
     , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Tick []
     , [EncodingSpec] -> PropertySpec
enc []
     ]


-- $pie-chart
-- Before adding a second axis, let's temporarily look at another
-- \"one dimensiona" chart, namely the humble pie chart.
-- The 'Arc' mark type allows you to create pie charts, as well as more
-- complex visualizations which we won't discuss further in this
-- tutorial.


{-|

In this example we embed the data for the pie chart - namely the number
of stars per cluster - in the vsualization itself (using
'dataFromColumns' to create column data labelled \"cluster\" and
\"count\"). The 'position' encoding is set to 'Theta', which is
given the star counts, and the 'color' is set to the
Cluster name.

<<images/vl/piechart.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4IgtghgTg1iBcJoGMQBoQBMIBcINADcIAbAVwFMBnBAbVGXKpwqgSRIAcALCAAgAKrdCGQB7MgDscCAOwAWAAwBfNAyYs2iAEIkIk8XwCMI8VJnx5ADgCcq9WWbDEAYTGQ+24RjPSERgFYAZntRDWcQAAkATwhMalMJP3gAwNDGR012AEkXPgAmIJsTHySLIPyA9PCtEFyCgDZFfMTzBHkbfOrMiIA5AHE8-PlU1uSlFTUwntqBEgoASziE0rb4IwqG7qdZqAhqCk4KMYsbIKtlAF1VEAASKmRuCkh2bhwcTip4AHpvwgoAOYQAB0AIWOG4ZAARsCFmJvg8npA-oCIABaEjgih-eTAgBWVDEkhEFAMYkwC0kAIIIAhFDwNIAZgsKCRMOxfDIMDhokd2ABHMj6HDg3ALf4gG7iEhiLSgZms9mIDI7EQ8vmISTuSmkSXKZRAA Open this visualization in the Vega Editor>

@
let manualData = 'dataFromColumns' []
                 . 'dataColumn' "cluster" ('Strings' clusters)
                 . dataColumn "count" ('Numbers' counts)
                 $ []

    clusters = [ \"alpha Per\", \"Blanco 1\", \"Coma Ber\", \"Hyades\", \"IC 2391\"
               , \"IC 2602\", \"NGC 2451\", \"Pleiades\", \"Praesepe\"]
    counts = [ 740, 489, 153, 515, 325, 492, 400, 1326, 938]

    enc = encoding
          . position 'Theta' [PName "count", PmType Quantitative]
          . color [MName "cluster", MmType Nominal]

in toVegaLite
   [ manualData
   , mark 'Arc' []
   , enc []
   ]
@

-}

pieChart :: VegaLite
pieChart :: VegaLite
pieChart =
  let manualData :: PropertySpec
manualData = [Format] -> [DataColumn] -> PropertySpec
dataFromColumns []
                   ([DataColumn] -> PropertySpec)
-> ([DataColumn] -> [DataColumn]) -> [DataColumn] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> DataValues -> [DataColumn] -> [DataColumn]
dataColumn Text
"cluster" ([Text] -> DataValues
Strings [Text]
clusters)
                   ([DataColumn] -> [DataColumn])
-> ([DataColumn] -> [DataColumn]) -> [DataColumn] -> [DataColumn]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> DataValues -> [DataColumn] -> [DataColumn]
dataColumn Text
"count" ([Double] -> DataValues
Numbers [Double]
counts)
                   ([DataColumn] -> PropertySpec) -> [DataColumn] -> PropertySpec
forall a b. (a -> b) -> a -> b
$ []

      clusters :: [Text]
clusters = [ Text
"alpha Per", Text
"Blanco 1", Text
"Coma Ber", Text
"Hyades", Text
"IC 2391"
                 , Text
"IC 2602", Text
"NGC 2451", Text
"Pleiades", Text
"Praesepe"]
      counts :: [Double]
counts = [ Double
740, Double
489, Double
153, Double
515, Double
325, Double
492, Double
400, Double
1326, Double
938]

      enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
            ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Theta [Text -> PositionChannel
PName Text
"count", Measurement -> PositionChannel
PmType Measurement
Quantitative]
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [Text -> MarkChannel
MName Text
"cluster", Measurement -> MarkChannel
MmType Measurement
Nominal]

  in [PropertySpec] -> VegaLite
toVegaLite
     [ PropertySpec
manualData
     , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Arc []
     , [EncodingSpec] -> PropertySpec
enc []
     ]


{-|

There are three main changes to 'pieChart':

 - 'MInnerRadius' is used to impose a minimum radius on the pie slices
   (so leaving a hole in the center);

 - the 'ViewStyle' configuration is used to turn off the plot edge;

 - and the count value is calculated automatically by the 'PAggregate'
   method (summing over the \"Cluster\" column), rather than having a
   hand-generated table of values encoded in the visualization.

<<images/vl/piechartwithcounting.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4Igxg9gdgZglgcxALlANzgUwO4tAZwBcAnCAa0xSgFcAbWgXwYBoQBbAQ2LLxEIE8ADpWQguYEKzhQomYgCUOAEzjV8KAEwAGFiCUdCHXtWK0UIABaFCg-MgD094h2wA6BHEIXqAIzVzIKEJMINdINnsAEQhqBAAhEwp7CzRMBA57TiI5ZNT0+31De3S4DgBaDnKANgBGKoqarTLDH1pMGo5XKAgyi0xlOVdCfDRJEBgIYk5CXkEufBFQAHFOJFEaNh85MfkAQQB9AEkAYXkAZXMNreIxwVoAD0vqTe3WSIBRI9OL9efrscw+zuj1+LxuTF0ABJ8GA+pxzFYbHZHHlOh4vL5XHAIPYYXCMqiyrRPJh7GgACyuABW+GgAKgkBUUDWoC8mEMvA4CAQxDSBhE4BiQTGAmE5gAjtQOEFPAY4KkQLpILRJrx4JhaEpzMdaGpgjdWKKBd02NIOGYIUA Open this visualization in the Vega Editor> 

@
let enc = encoding
          . position Theta ['PAggregate' 'Count', PmType Quantitative]
          . color [MName "Cluster", MmType Nominal]

in toVegaLite
   [ gaiaData
   , mark Arc ['MInnerRadius' 20]
   , enc []
   , configure (configuration ('ViewStyle' ['ViewNoStroke']) [])
   ]
@

-}


pieChartWithCounting :: VegaLite
pieChartWithCounting :: VegaLite
pieChartWithCounting =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
            ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Theta [Operation -> PositionChannel
PAggregate Operation
Count, Measurement -> PositionChannel
PmType Measurement
Quantitative]
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal]

  in [PropertySpec] -> VegaLite
toVegaLite
     [ PropertySpec
gaiaData
     , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Arc [Double -> MarkProperty
MInnerRadius Double
20]
     , [EncodingSpec] -> PropertySpec
enc []
     , [ConfigureSpec] -> PropertySpec
configure (ConfigurationProperty -> BuildConfigureSpecs
configuration ([ViewConfig] -> ConfigurationProperty
ViewStyle [ViewConfig
ViewNoStroke]) [])
     ]


-- $add-axis
-- While the strip plot shows the range of parallaxes, it is hard to
-- make out the distribution of values, since the ticks overlap. Even
-- changing the opacity of the ticks - by adding an encoding channel
-- like @'opacity' [ 'MNumber' 0.6 ]@, or by setting the 'MOpacity'
-- property of the 'mark' - only helps so much. Adding a second
-- axis is easy to do, so let's see how the parallax distribution
-- varies with cluster membership.

{-|

The 'stripPlotWithColor' visualization can be changed to show two
variables just by adding a second 'position' declaration, which
shows that the 7 milli-arcsecond range is rather crowded:

<<images/vl/parallaxbreakdown.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEC2CGBOBrSAuKAXAlgY2QGnCgBNZ1ZUxQIJIBXeAGwsgAt10AHAZxQHpf4sAO4A6AOaZ0LWgCNaXAKbxsAewB26BRpGrovACIraYgEL1EC3iwBuCsbF5wum+Fdv3eJMr3uZYAWlgAgDYARmDA0IAGfzIZBgVQ2BE1FX8WBVgiJRF0LmtIAmooADMVeDh0CipiqA4ERWrCWqgAcTgxZjVaaBklQubayAAlAEEAfQBJAGFhgGUunr74AZaaDgYAD0Xe-qK1yH0AUSnZhbRIbt2V-ZbIBXGN7Yur5chBiABfQe+v-cgACRcbAZODMNicHj8dzJCRSWQiTAqXjA0EOGH+BiSSzWAAsIgAVlx1KsoFpVERMGpOmgajRVAxytVICVMAoGERmNMGPIXIUMABPDgKLoqaBU2BMT63KDPSgfUpsjnMJ6k4qQdBCkUXACOtFgGkkpEwtjV1EgsE2mC4zKw6ASzAACghJQxLWAABROACUkF+1Glg0gAuZrPZnIu3N5e0FwtF4rUkr9hED9PUrJplG+nyAA Open this visualization in the Vega Editor>

@
let enc = encoding
            . position X [ PName \"plx\", PmType Quantitative, PAxis [ AxTitle \"Parallax (mas)\" ] ]
            . position Y [ PName \"Cluster\", PmType Nominal ]
            . color [ MName \"Cluster\", MmType Nominal ]

in toVegaLite
    [ gaiaData
    , mark Tick []
    , enc []
    ]
@

I have left the color-encoding in, as it makes it easier to compare to
'stripPlotWithColor', even though it replicates the information provided
by the position of the mark on the Y axis. The 'yHistogram' example
below shows how the legend can be removed from a visualization.

-}

parallaxBreakdown :: VegaLite
parallaxBreakdown :: VegaLite
parallaxBreakdown =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
            ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"plx", Measurement -> PositionChannel
PmType Measurement
Quantitative, [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"Parallax (mas)" ] ]
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"Cluster", Measurement -> PositionChannel
PmType Measurement
Nominal ]
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal ]

  in [PropertySpec] -> VegaLite
toVegaLite
     [ PropertySpec
gaiaData
     , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Tick []
     , [EncodingSpec] -> PropertySpec
enc []
     ]


-- $histogram
-- We can also \"create\" data to be plotted, by aggregating data. In this
-- case we can create a histogram showing the number of stars with the
-- same parallax value (well, a range of parallaxes).

{-|

Since sensible (hopefully) defaults are provided for unspecified settings, it
is relatively easy to write generic representations of a particular
visualization. The following function expands upon the previous
specifications by:

 * taking a field name, rather than hard coding it;

 * the use of @'PBin' []@ to ask for the x-axis values to be binned;

 * the addition of a second axis ('Y') which is used for the
   aggregated value ('Count', which means that no column has
   to be specified with @PName@);

 * and the change from 'Tick' to 'Bar' for the 'mark'.

Note that we did not have to specify how we wanted the histogram
calculation to proceed - e.g. the number of bins, the bin widths,
or edges - although we could have added this, by using a non-empty
list of 'BinProperty' values with 'PBin', if the defaults are not
sufficient.

@
simpleHistogram :: T.Text -> VegaLite
simpleHistogram field =
  let enc = encoding
              . position X [ PName field, PmType Quantitative, 'PBin' [] ]
              . position Y [ PAggregate Count, PmType Quantitative ]

  in toVegaLite
       [ gaiaData
       , mark 'Bar' []
       , enc []
       ]
@

-}

simpleHistogram :: T.Text -> VegaLite
simpleHistogram :: Text -> VegaLite
simpleHistogram Text
field =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
              ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
field, Measurement -> PositionChannel
PmType Measurement
Quantitative, [BinProperty] -> PositionChannel
PBin [] ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Operation -> PositionChannel
PAggregate Operation
Count, Measurement -> PositionChannel
PmType Measurement
Quantitative ]

  in [PropertySpec] -> VegaLite
toVegaLite
     [ PropertySpec
gaiaData
     , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar []
     , [EncodingSpec] -> PropertySpec
enc []
     ]

{-|

With 'simpleHistogram' it becomes easy to get a histogram of the parallax
values:

<<images/vl/parallaxhistogram.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEC2CGBOBrSAuKAjBkA04oBNYAXWVMUCCSAV3gBszIALIogBwGcUB6b+WAO4A6AOYBLIk2rpqHAKbwAxgHsAdkTnqhK6NwAiy6iIBCtRHO5MAbnJGxucDhviWbd7oRLc7Y2AFpYfwA2AEYggJCABj8SdDo5ENghVWU-JjlYfAUhIg4rHDwqADNleDgiMgpKKjYEeUrC6qgAcTgRRlVqaHQFAqbKSAAlAEEAfQBJAGFBgGUOrp74Pv6oNjoAD3nu3twVqD0AUQnpubRITu2l3ZXIOVG1zbOLxchGiABfRs+P68gAEg4inScEYLHYXF4biS4kk0iEYmU3EBwPsUL8dAkFisABYhAArDhqZa3VQqfBiVTtNBVKiPciQIpiOR0fCMB44DAUshEeDUOTYKBEACebDkjAAjtRYOoJMQxDZIO9rlQhZVILARCJ4LZiGKzipqOoOZBhaKJVKZSQiPKxd8wEq8JAVKpGVTyJ93kA Open this visualization in the Vega Editor>

@parallaxHistogram = 'simpleHistogram' \"plx\"@

We can see that although parallaxes around 20 to 25 milli-arcseconds
dominated the earlier visualizations, such as 'stripPlotWithColor',
__most__ of the stars have a much-smalled parallax, with values
in the range 5 to 10.

-}

-- TODO XXX explain parallax

parallaxHistogram :: VegaLite
parallaxHistogram :: VegaLite
parallaxHistogram = Text -> VegaLite
simpleHistogram Text
"plx"

{-|

A different column (or field) of the input data can be viewed,
just by changing the name in the specification:

<<images/vl/gmaghistogram.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEC2CGBOBrSAuKAjBkA04oBNYAXWVMUCCSAV3gBszIALIogBwGcUB6b+WAO4A6AOYBLIk2rpqHAKbwAxgHsAdkTnqhK6NwAiy6iIBCtRHO5MAbnJGxucDhviWbd7oRLc7Y2AFpYfwA2AEYggJCABj8SdDo5ENghVWU-JjlYfAUhIg4rHDwqADNleDgiMgpKKjYEeUrC6qgAcTgRRlVqaHQFAqbKSAAlAEEAfQBJAGFBgGUOrp74Pv6oNjoAD3nu3twVqD0AUQnpubRITu2l3ZXIOVG1zbOLxchGiABfRs+P68gAEg4inScEYLHYXF4biS4kk0iEYmU3EBwPsUL8dAkFisABYhAArDhqZa3VQqfBiVTtNBVKiPciQIpiOR0fCMVqwdrYDAUshEeDUORcyBEACebDkjAAjtRYOoJMQxDZIO9rlQRZVIByRPBbMQJWcVNR1DgoKLxVKZXKSERFRLvmAVXhICpVIyqeRPu8gA Open this visualization in the Vega Editor>

@gmagHistogram = simpleHistogram \"Gmag\"@

Here we can see that the number of stars with a given magnitude
rises up until a value of around 18, and then drops off.

-}

-- TODO XXX explain magnitudes

gmagHistogram :: VegaLite
gmagHistogram :: VegaLite
gmagHistogram = Text -> VegaLite
simpleHistogram Text
"Gmag"



-- $ylog-histogram
-- In the case of 'parallaxHistogram', the data is __dominated__ by
-- stars with small parallaxes. Changing the scale of the
-- Y axis to use a logarithmic, rather than linear, scale /might/
-- provide more information:

{-|

<<images/vl/yloghistogram.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAWCmCWBzaAXSAuMBmADDgNOFALYCGATgNYZjCQDOK5A9pbDZIuaQJ6T5QAZvAA2Ijs24A7ROwGQUPAA7tMkAEYVIAX0IRIAE1IpSNUBH0BXcuLWoUS+ugD0z7gHcAdIngpol9Ut6WHIAY2YpFFhIz3DiZwARZktEACFrNmdoADdYRFJnMkYQrNz85yMTZ3z4UgBaUnqANgBGJoaWnDqTdRFYFtJPKWY6uFIDEM8Uemz+In1BSTI0THMLfSUKYLN59agAcTJEDilLYnUQub2LSAAlAEEAfQBJAGFbgGUTs4vyK+uoEoRAAPb7nS56AGQBIAURe7y+alO4L+kOukFgjyBoKRP0uuwg2l2RMJkMgABJ6KE4GQOPZHC5nGVBj4-AFPPBmM4qTSCsy6iJfLAmQAWTwAK3oEX+Cl8fQ4+1ItTAAAUKKQxKRgbB6DLouEDPAZDt1pAcbQCUJ4LARAYONj-qb1EaaExLLA0TdFCoOABHSykSK+YzwXKOm5a+C61aylDytRq7ia4FgAAURQAlDpiZ7IHxVpaGKENapaAplKXICJmMddIXSIguHljJXwpZIuH9N7K-7Ayhg-2w8SiHX9OEpMJjqsidogA Open this visualization in the Vega Editor>

@
let enc = encoding
            . position X [ PName "plx", PmType Quantitative, PBin [], PAxis [ AxTitle "Parallax (mas)" ] ]
            . position Y [ PAggregate Count, PmType Quantitative, 'PScale' [ 'SType' 'ScLog' ] ]

in toVegaLite
   [ gaiaData
   , mark Bar [ 'MFill' "orange", 'MStroke' "gray" ]
   , enc []
   , 'height' 300
   , 'title' "Gaia Parallaxes" []
   ]
@

There are four new changes to the visualization created by 'simpleHistogram' (since 'PAxis'
has been used above):

 1. an explicit choice of scaling for the Y channel (using 'PScale');

 2. the fill ('MFill') and edge ('MStroke') colors of the histogram bars are different;

 3. the height of the overall visualization has been increased;

 4. and a title has been added.

If you view this in the Vega Editor you will see the following warning:

@
A log scale is used to encode bar's y. This can be misleading as the height of the bar can be arbitrary based on the scale domain. You may want to use point mark instead.
@

-}
ylogHistogram :: VegaLite
ylogHistogram :: VegaLite
ylogHistogram =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
              ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"plx", Measurement -> PositionChannel
PmType Measurement
Quantitative, [BinProperty] -> PositionChannel
PBin [], [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"Parallax (mas)" ] ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Operation -> PositionChannel
PAggregate Operation
Count, Measurement -> PositionChannel
PmType Measurement
Quantitative, [ScaleProperty] -> PositionChannel
PScale [ Scale -> ScaleProperty
SType Scale
ScLog ] ]

  in [PropertySpec] -> VegaLite
toVegaLite
     [ PropertySpec
gaiaData
     , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar [ Text -> MarkProperty
MFill Text
"orange", Text -> MarkProperty
MStroke Text
"gray" ]
     , [EncodingSpec] -> PropertySpec
enc []
     , Double -> PropertySpec
height Double
300
     , Text -> [TitleConfig] -> PropertySpec
title Text
"Gaia Parallaxes" []
     ]


{-|

A color encoding can also be added. When used with the 'Tick' mark -
'stripPlotWithColor' - the result was that each tick mark was colored
by the \"Cluster\" field, but for the 'Bar' mark the result is that
the bars are stacked together. I have also taken the opportunity to
widen the plot ('width'); define the binning scheme used, with @'Step'
1@; and configure the location of the x axis tick marks, using
'AxValues'.

<<images/vl/gmaghistogramwithcolor.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAWCmCWBzaAXSAuMBmADDgNOFALYCGATgNYZQBGFkhEkAJqSqTaBMwK7kAbGjBQoADgGd0AemnlSAdwB0ieCmi9avCbHIBjAPYA7FLBNLDxaQBEDvRACF+lWNOgA3WIlLSyE0+Runt7SbBzS3vCkALSkMQBsAIzxsYk40Ry0ArCJpEpGBtFwpCy6SigS7oxEzABmBuRkaJjcPMxiFDpcNW1QAOJkiMJGvMS0utW9PJAASgCCAPoAkgDCMwDKw6Pj5JNTUGICAB5bYxNM+5DWAKLLa5uYkCNnuxdTkLALhyePzzuQPQgAF8eiDgRdIAp4Cx1DQACx4CEAEgkejgZGEqHEUlkwTyqnUmiU8AM0lR6J8eOiAjUrnccKUACsJMY9h8jIYWPAjEMWj1IIYBA0uJBavBYAIWMIVgJtAFGFAUABPMSwYYGYjc0hCIFvKA-MCtNqi8WS4QDUhDPXMWjckX+WBiGiJXWAxUqtWPACOvFIJjU7Hgnj2xtIR3gEhF7m1vFgkcwAG0CGAAKz4MBpdOJNNgABMOAAumCeK7jUqRZbEOQvOxPVBDLwTArIMrVcIfX6UAGu8Hi6X68YxbzDSCgUA Open this visualization in the Vega Editor>

@
let enc = encoding
            . position X [ PName \"Gmag\", PmType Quantitative, binning, axis ]
            . position Y [ PAggregate Count, PmType Quantitative ]
            . color [ MName \"Cluster\", MmType Nominal ]

    binning = PBin [ 'Step' 1 ]
    axis = PAxis [ 'AxValues' (Numbers [ 0, 5 .. 20 ]) ]

in toVegaLite
   [ gaiaData
   , mark Bar []
   , enc []
   , height 300
   , 'width' 400
   ]
@

Note that @hvega@ will allow you to combine a 'color' encoding with a 'ScLog'
scale, even though a Vega-Lite viewer will not display the
resulting Vega-Lite specification, saying

@Cannot stack non-linear scale (log)@

-}
gmagHistogramWithColor :: VegaLite
gmagHistogramWithColor :: VegaLite
gmagHistogramWithColor =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
              ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Gmag", Measurement -> PositionChannel
PmType Measurement
Quantitative, PositionChannel
binning, PositionChannel
axis ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Operation -> PositionChannel
PAggregate Operation
Count, Measurement -> PositionChannel
PmType Measurement
Quantitative ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal ]

      binning :: PositionChannel
binning = [BinProperty] -> PositionChannel
PBin [ Double -> BinProperty
Step Double
1 ]
      axis :: PositionChannel
axis = [AxisProperty] -> PositionChannel
PAxis [ DataValues -> AxisProperty
AxValues ([Double] -> DataValues
Numbers [ Double
0, Double
5 .. Double
20 ]) ]

  in [PropertySpec] -> VegaLite
toVegaLite
     [ PropertySpec
gaiaData
     , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar []
     , [EncodingSpec] -> PropertySpec
enc []
     , Double -> PropertySpec
height Double
300
     , Double -> PropertySpec
width Double
400
     ]

{-|

Notice how we never needed to state explicitly that we wished our bars
to be stacked. This was reasoned directly by Vega-Lite based on the
combination of bar marks and color channel encoding. If we were to
change just the mark function from 'Bar' to 'Line', Vega-Lite produces an
unstacked series of lines, which makes sense because unlike bars,
lines do not occlude one another to the same extent.

<<images/vl/gmaglinewithcolor.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAWCmCWBzaAXSAuMBmADDgNOFALYCGATgNYZQA28AdrJIRJACakqk2gRsBXcrRowUKAA4BndAHpZ5UgHcAdIngpoAgEYCpscgGMA9gxSwzKk8VkARYwMQAhIZVizoAN1iJSsslLm5B7evrKc3LK+8KQAtKRxAGwAjInxyTix3Nq0sMmkKgzGsXCk7AYqKFKeLERsAGbG5GRomHz8bBIU+rx1HVAA4mSIogwCxNoGtf38kABKAIIA+gCSAMJzAMqj45Pk0zNQErQAHjsTU6yHkLYAoqsb25iQYxf7VzOQsEvHZ8+ve0gfQgAF8+mDQVdIEp4OxNDQACx4KEAEikhjgZFEqEkMnkoQK6k0OhU8GMsnRmL8BNi9HMsk8CJUACspKYDl8GCZ2IwRm0+pATLQmrxIPV4LBaOxRGtaHogiwoCgAJ4SZj-YzERikEQgj5QP5gdodMUSqWiIakEb6tjaRiiwKwCQ0ZJ64FK1XqqAARwEpDMGi48G8BxNpBO8Ckos8OoEsCjmAA2gQwABWfBgDIZ5LpsAAJhwAF0Ifw3SblaKrYhyD4uF7BQ4zIrICq1aJff6UIGuyGS2WoCYGOK+UawSCgA Open this visualization in the Vega Editor>

@
let enc = encoding
            . position X [ PName \"Gmag\", PmType Quantitative, binning, axis ]
            . position Y [ PAggregate Count, PmType Quantitative ]
            . color [ MName \"Cluster\", MmType Nominal ]

    binning = PBin [ Step 1 ]
    axis = PAxis [ AxValues (Numbers [ 0, 5 .. 20 ]) ]

in toVegaLite
   [ gaiaData
   , mark 'Line' []
   , enc []
   , height 300
   , width 400
   ]
@

-}

gmagLineWithColor :: VegaLite
gmagLineWithColor :: VegaLite
gmagLineWithColor =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
              ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Gmag", Measurement -> PositionChannel
PmType Measurement
Quantitative, PositionChannel
binning, PositionChannel
axis ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Operation -> PositionChannel
PAggregate Operation
Count, Measurement -> PositionChannel
PmType Measurement
Quantitative ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal ]

      binning :: PositionChannel
binning = [BinProperty] -> PositionChannel
PBin [ Double -> BinProperty
Step Double
1 ]
      axis :: PositionChannel
axis = [AxisProperty] -> PositionChannel
PAxis [ DataValues -> AxisProperty
AxValues ([Double] -> DataValues
Numbers [ Double
0, Double
5 .. Double
20 ]) ]

  in [PropertySpec] -> VegaLite
toVegaLite
     [ PropertySpec
gaiaData
     , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Line []
     , [EncodingSpec] -> PropertySpec
enc []
     , Double -> PropertySpec
height Double
300
     , Double -> PropertySpec
width Double
400
     ]


-- $histogramChoice
-- The previous histogram visualizations have taken advantage of Vega-Lite's
-- ability to bin up ('Count') a field, but there are a number of aggregation
-- properties (as defined by the 'Operation' type). For example, there
-- are a number of measures of the \"spread\" of a population, such as
-- the sample standard deviation ('Stdev').
--
-- You can also synthesize new data based on existing data, with the
-- 'transform' operation. Unlike the 'encoding' function, the order
-- of the arguments to 'transform' do matter, as they control the
-- data flow (e.g. you can not filter a data set if you have not
-- created the field to be filtered).

{-|
The aim for this visualization is to show the spread in the @Gmag@ field
for each cluster, so we now swap the axis on which the aggregate is
being applied (so that the cluster names appear on the y axis),
and hide the legend that is applied (using @'MLegend' []@) since
we can read off the color mapping from the y axis.

<<images/vl/yhistogram.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEC2CGBOBrSAuMxIHsAOsDGAlgC4CeqYADAHQBsANFKdgKbmQBGCkAvneFABNYRWOVAQIkAK7wANmwAWRItgDOKAPQb4sAO5UA5sQVT2U1c3h5MAOyLM7Va9A0ARTFIMAhGYmYaFADdmA1gNOFV7eADg0I0hEQ1QglgAWlg0mgBGGnSsilSRdllmLNgqG0xUhWZYAUsqIlVAyD4JKAAzTHg4IjF+dshceAt+9sGAcTgDNhspaHZLVoHxyAAlAEEAfQBJAGE1gGVZ+cX4ZfGJIdkADxOFpbbLqFcAUV2D47RIOYfzp8ukGYW2wt3uZ0gKwg3BWMOhT0gABJVHganBFMo1JoNLFykYiCZ2FQCJgNCi0WFcalZMR-IEACxUABWqlsFygDmsAgINhmaHEV2ssm6YkgHQIzFkAjYe1k5iirUYJBYs0w0B5sHkDEgJQMDmlaDmslkvBWkDu-LFEqlbCmsBm2vtBngIWErG+kXqLW1THdUAAjlJYHZiMICMEeACoGRLeLJQaoLL5Y8lSrvpV1TZNTx+KbJNYbOK+egYdwgA Open this visualization in the Vega Editor>

@
let enc = encoding
            . position X [ PName \"Gmag\", PmType Quantitative, PAggregate 'Stdev' ]
            . position Y [ PName \"Cluster\", PmType Nominal ]
            . color [ MName \"Cluster\", MmType Nominal, 'MLegend' [] ]

in toVegaLite
   [ gaiaData
   , mark Bar [ 'MOpacity' 0.6 ]
   , enc []
   ]
@

The bar opacity is reduced slightly with 'MOpacity 0.6' so that the
x-axis grid lines are visible. An alternative would be to change the
'AxZIndex' value for the 'X' encoding so that it is drawn on top of
the bars.

-}

yHistogram :: VegaLite
yHistogram :: VegaLite
yHistogram =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
              ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Gmag", Measurement -> PositionChannel
PmType Measurement
Quantitative, Operation -> PositionChannel
PAggregate Operation
Stdev ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"Cluster", Measurement -> PositionChannel
PmType Measurement
Nominal ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal, [LegendProperty] -> MarkChannel
MLegend [] ]

  in [PropertySpec] -> VegaLite
toVegaLite
     [ PropertySpec
gaiaData
     , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar [ Double -> MarkProperty
MOpacity Double
0.6 ]
     , [EncodingSpec] -> PropertySpec
enc []
     ]



{-|

Aggregation can happen in the position channel - as we've seen with
the 'PAggregate' option - or as a 'transform', where we create
new data to replace or augment the existing data. In the following
example I use the 'aggregate' transform to calculate the number of
rows in the original dataset per cluster with the
'Count' operation. This effectively replaces
the data, and creates a new one with the fields @\"Cluster\"@ and
@\"count\"@.

The other two major new items in this visualization are that the
X axis has been ordered to match the Y axis (using 'ByChannel' and
'PSort' in the 'position' encoding), and I have specified my own SVG
definition for the symbols with 'SymPath' and 'MShape'.

<<images/vl/starcount.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1wIxhIBzWdAVwAcAjATz0MgGEAba5aAU1kgBdADRR45Sr3Lw+LAmXS0WkAMY1E0SKMjxkytdQ2QAvoOPERxSFniwA1i1AkoPKnd4B1AJYATaAAsWAEZhYghIZC8AL15ggAY40OcIuHR3ZXoOeBUHJJJIVC8ODmVGXmL0AHctMJd-eFpY-EgAWTBEgFogsAAZdoA6ACYAZmEOuKHh3rAQ8cnpieGADmEJwe6+iYA2QdX+gE4AFgW9w-3puZ29o4vFlbWNsC6xtam+uZGX+c2x7qia5LQRiNZS0dBeIzEYx5SA+GTwRy1SDUWAlZr+aDQWjIXAAelxCEq-XIXgC1Ho3H4ag0vA0-TUWFxABEaOQAEIo9y4-wANyk8FxNh4-G5fOkuLh0AF0i88A68DlWyCW3lQTiHSlmV4QXg-UQ6A6-l48B8-H60GQPIB+Qw2BkiOc4VotmQTVItWSAHEbORlIhqFh6PxrY6oAAlACCAH0AJJsMMAZT9AaDAjyocgtA4AA9k4Hg+nHZAmQBRWPxpPNf35tMe-K8KNZ3NVlPBuvmZwdsDQqwAEmQKiNNmUGKxOPxYt1JLJ9H6XnQuIHQ4Fk46HFJvFxPMO-QAVsh0IhrZBaWofBDffgnPlm6QCl5yj5lJxuHw0y5MJoryfEGeL6UTG0IEQSrdAsAheASh7ZJmCvOt70ffR1E0QtwmAt1IAAR2oJBoFJGQvD5EN8ngbMvD0b88OgDgMIAOVbWAwHQVAwB4F0TFqLtoKgalCkvUhzGMIA Open this visualization in the Vega Editor>

@
let trans = 'transform'
            . 'aggregate' [ 'opAs' 'Count' \"\" \"count\" ]
                        [ \"Cluster\" ]

    enc = encoding
          . position X [ PName \"Cluster\"
                       , PmType Nominal
                       , 'PSort' [ 'ByChannel' 'ChY' ]
                       ]
          . position Y [ PName \"count\"
                       , PmType Quantitative
                       , PAxis [ AxTitle \"Number of stars\" ]
                       ]

    star = 'SymPath' \"M 0,-1 L 0.23,-0.23 L 1,-0.23 L 0.38,0.21 L 0.62,0.94 L 0,0.49 L -0.62,0.94 L -0.38,0.21 L -1,-0.23 L -0.23,-0.23 L 0,-1 z\"

in toVegaLite [ gaiaData
              , trans []
              , enc []
              , mark 'Point' [ 'MShape' star
                           , MStroke \"black\"
                           , 'MStrokeWidth' 1
                           , MFill \"yellow\"
                           , 'MSize' 100
                           ]
              ]
@

Notes:

- the star design is based on a
<https://upload.wikimedia.org/wikipedia/commons/a/a5/Star_with_eyes.svg Wikipedia design>,
after some hacking and downsizing (such as losing the cute eyes);

- when using 'Count' with 'opAs', the first 'FieldName' argument is ignored,
so I set it to the empty string @\"\"@ (it's be great if the API were such
we didn't have to write dummy arguments, but at present @hvega@
doesn't provide this level of safety);

- although the order of operations of 'transform' is important, here
  I only have one (the 'aggregate' call);

- and the order of the arguments to 'toVegaLite' does not matter (so you
  can have the 'transform' appear before 'encoding' or after it).

-}

starCount :: VegaLite
starCount :: VegaLite
starCount =
  let trans :: [TransformSpec] -> PropertySpec
trans = [TransformSpec] -> PropertySpec
transform
              ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [VLSpec] -> [Text] -> [TransformSpec] -> [TransformSpec]
aggregate [ Operation -> Text -> Text -> VLSpec
opAs Operation
Count Text
"" Text
"count" ]
                          [ Text
"Cluster" ]

      enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
            ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Cluster"
                         , Measurement -> PositionChannel
PmType Measurement
Nominal
                         , [SortProperty] -> PositionChannel
PSort [ Channel -> SortProperty
ByChannel Channel
ChY ]
                         ]
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"count"
                         , Measurement -> PositionChannel
PmType Measurement
Quantitative
                         , [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"Number of stars" ]
                         ]

      -- based on https://upload.wikimedia.org/wikipedia/commons/a/a5/Star_with_eyes.svg
      star :: Symbol
star = Text -> Symbol
SymPath Text
"M 0,-1 L 0.23,-0.23 L 1,-0.23 L 0.38,0.21 L 0.62,0.94 L 0,0.49 L -0.62,0.94 L -0.38,0.21 L -1,-0.23 L -0.23,-0.23 L 0,-1 z"

  in [PropertySpec] -> VegaLite
toVegaLite [ PropertySpec
gaiaData
                , [TransformSpec] -> PropertySpec
trans []
                , [EncodingSpec] -> PropertySpec
enc []
                , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Point [ Symbol -> MarkProperty
MShape Symbol
star
                             , Text -> MarkProperty
MStroke Text
"black"
                             , Double -> MarkProperty
MStrokeWidth Double
1
                             , Text -> MarkProperty
MFill Text
"yellow"
                             , Double -> MarkProperty
MSize Double
100
                             ]
                ]


{-|

I've shown that the number of stars per cluster increases when
ordered by increasing count of the number of stars per cluster,
which is perhaps not the most informative visualization. How about
if I ask if there's a correlation between number of stars and
distance to the cluster (under the assumption that objects further
away can be harder to detect, so there /might/ be some form of
correlation)?

To do this, I tweak 'starCount' so that we also calculate the
parallax to each cluster in the 'transform' - in this case taking
the median value of the distribution thanks to the 'Median' operation - and
then using this new field to order the X axis with 'ByFieldOp'. Since
parallax is inversely correlated with distance we use the
'Descending' option to ensure the clusters are drawn from near to
far. We can see that there is no obvious relation with distance.

<<images/vl/starcount2.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1wIxQSTIBzWdAVwAcAjATz0MgGEAbW5aAU1iQAugBpi5SPErV+leALZFy5YJHT02kAMZ1E0SKKjxkWxLSyNBkAL7iVJNRq1Z+AEwCWSQ8dP5I9FwAHgD6rm4+kKge-FwR-oFBthIkwik2xGLEkFjwsADWbGQUfDT5-ADqHm7QABZsAIz2JR4AXvyNAAydzRCQpejlWoxc8NqFvVDRXFxazLFc6ADuhin9tfD0Hf4AsmA9ALQNYAAy+wB0AEwAzKIHnVfXp2BN94-PD9cAHKIPl8dnB4ANkuv3OAE4ACwfMGQ8HPN4gsFQhGfH5-AFgI53P5PM5vG4496Au7HVqrSTQZhbLT0dAefSQYh2bJuBTwIprWiwWb+WrQaD0ZC4AD0IoQS3OlA8dVojF4gl0+n4+nOuiwIoAInRKAAhbnlEW1ABucngItyfEERtN8hFbOg5vkXgO8HgByBDSBroanQOjpG-Aa8HOiHQB1q-HgbkE52gyGNFIoGGwCk5DgCeWQ21IKUkAHFcpQzBYrEJJpIAEoAQRCAEkOJWAMolyzWCsURKtstJjOagCi9cbLf85jb5bzFH4IS7o9L1knGXIS7ALL6ABJkNpI7ktPzBcKxbaQ9LZYxzh50CKtzvzceDlwZfwRcbIecAFbIdCIJOQFW6TxEGLfBij6JIQMnKIYjiLRuF4AQJwzL9YAMED1E0fxciSIwoNieIoESMJ3EiTAYyEfwYy3FVAOLNcVBgakc0gMMsAZeBZnSSZIFYCCM2iPDu3bSCqRpfwAEdaCQaAZQUDxTV7Ch4CCDw-FIGAZS4JiADl51gMB0FQMA+CzZJl2ZZodG-aJgNIDIbCAA Open this visualization in the Vega Editor>

@
let trans = transform
            . aggregate [ opAs Count \"\" \"count\"
                        , opAs 'Median' \"plx\" \"plx_med\"
                        ]
                        [ \"Cluster\" ]

    enc = encoding
          . position X [ PName \"Cluster\"
                       , PSort [ 'ByFieldOp' \"plx_med\" 'Max'
                               , 'Descending'
                               ]
                       ]
          . position Y [ PName \"count\"
                       , PmType Quantitative
                       , PAxis [ AxTitle \"Number of stars\" ]
                       ]

    star = SymPath \"M 0,-1 L 0.23,-0.23 L 1,-0.23 L 0.38,0.21 L 0.62,0.94 L 0,0.49 L -0.62,0.94 L -0.38,0.21 L -1,-0.23 L -0.23,-0.23 L 0,-1 z\"

in toVegaLite [ gaiaData
              , trans []
              , enc []
              , mark Point [ MShape star
                           , MStroke \"black\"
                           , MStrokeWidth 1
                           , MFill \"yellow\"
                           , MSize 100
                           ]
              ]
@

Notes:

- I find the \"Data Viewer\" section of the
<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1wIxQSTIBzWdAVwAcAjATz0MgGEAbW5aAU1iQAugBpi5SPErV+leALZFy5YJHT02kAMZ1E0SKKjxkWxLSyNBkAL7iVJNRq1Z+AEwCWSQ8dP5I9FwAHgD6rm4+kKge-FwR-oFBthIkwik2xGLEkFjwsADWbGQUfDT5-ADqHm7QABZsAIz2JR4AXvyNAAydzRCQpejlWoxc8NqFvVDRXFxazLFc6ADuhin9tfD0Hf4AsmA9ALQNYAAy+wB0AEwAzKIHnVfXp2BN94-PD9cAHKIPl8dnB4ANkuv3OAE4ACwfMGQ8HPN4gsFQhGfH5-AFgI53P5PM5vG4496Au7HVqrSTQZhbLT0dAefSQYh2bJuBTwIprWiwWb+WrQaD0ZC4AD0IoQS3OlA8dVojF4gl0+n4+nOuiwIoAInRKAAhbnlEW1ABucngItyfEERtN8hFbOg5vkXgO8HgByBDSBroanQOjpG-Aa8HOiHQB1q-HgbkE52gyGNFIoGGwCk5DgCeWQ21IKUkAHFcpQzBYrEJJpIAEoAQRCAEkOJWAMolyzWCsURKtstJjOagCi9cbLf85jb5bzFH4IS7o9L1knGXIS7ALL6ABJkNpI7ktPzBcKxbaQ9LZYxzh50CKtzvzceDlwZfwRcbIecAFbIdCIJOQFW6TxEGLfBij6JIQMnKIYjiLRuF4AQJwzL9YAMED1E0fxciSIwoNieIoESMJ3EiTAYyEfwYy3FVAOLNcVBgakc0gMMsAZeBZnSSZIFYCCM2iPDu3bSCqRpfwAEdaCQaAZQUDxTV7Ch4CCDw-FIGAZS4JiADl51gMB0FQMA+CzZJl2ZZodG-aJgNIDIbCAA Vega Editor>
rather useful when creating new data columns or structures,
as you can actually see what has been created (I find Firefox works much
better than Chrome here);

- the use of 'ByFieldOp' here is a bit un-settling, as you need to
give it an aggregation-style operation to apply to the data field,
but in this case we have already done this with 'opAs' (so I pick
'Max' as we just need something that copies the value over).

We revisit this data in 'layeredCount'.

-}


starCount2 :: VegaLite
starCount2 :: VegaLite
starCount2 =
  let trans :: [TransformSpec] -> PropertySpec
trans = [TransformSpec] -> PropertySpec
transform
              ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [VLSpec] -> [Text] -> [TransformSpec] -> [TransformSpec]
aggregate [ Operation -> Text -> Text -> VLSpec
opAs Operation
Count Text
"" Text
"number"
                          , Operation -> Text -> Text -> VLSpec
opAs Operation
Median Text
"plx" Text
"plx_med"
                          ]
                          [ Text
"Cluster" ]

      enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
            ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Cluster"
                         , Measurement -> PositionChannel
PmType Measurement
Nominal
                         -- I think I want a ByField back
                         , [SortProperty] -> PositionChannel
PSort [ Text -> Operation -> SortProperty
ByFieldOp Text
"plx_med" Operation
Max
                                 , SortProperty
Descending
                                 ]
                         ]
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"number"
                         , Measurement -> PositionChannel
PmType Measurement
Quantitative
                         , [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"Number of stars" ]
                         ]

      star :: Symbol
star = Text -> Symbol
SymPath Text
"M 0,-1 L 0.23,-0.23 L 1,-0.23 L 0.38,0.21 L 0.62,0.94 L 0,0.49 L -0.62,0.94 L -0.38,0.21 L -1,-0.23 L -0.23,-0.23 L 0,-1 z"

  in [PropertySpec] -> VegaLite
toVegaLite [ PropertySpec
gaiaData
                , [TransformSpec] -> PropertySpec
trans []
                , [EncodingSpec] -> PropertySpec
enc []
                , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Point [ Symbol -> MarkProperty
MShape Symbol
star
                             , Text -> MarkProperty
MStroke Text
"black"
                             , Double -> MarkProperty
MStrokeWidth Double
1
                             , Text -> MarkProperty
MFill Text
"yellow"
                             , Double -> MarkProperty
MSize Double
100
                             ]
                ]



{-|

Vega-Lite supports a number of data transformations, including
several \"pre-canned\" transformations, such as a
kernel-density estimator, which I will use here to
look for structure in the parallax distribution. The earlier
use of a fixed-bin histogram - 'parallaxHistogram' and 'ylogHistogram' -
showed a peak around 5 to 10 milli-arcseconds, and a secondary
one around 20 to 25 milli-arcseconds, but can we infer anything more
from the data?

I have already shown that the 'transform'
function works in a similar manner to 'encoding', in that
it is applied to one or more transformations. In this
example I use the 'density' transform - which is __new to Vega Lite 4__ -
to \"smooth\" the data without having to pre-judge the data
(although there are options to configure the density estimation).
The transform creates new fields - called \"value\" and \"density\"
by default - which can then be displayed as any other field. In this
case I switch from 'Bar' or 'Line' to use the 'Area' encoding, which
fills in the area from the value down to the axis.

<<images/vl/densityparallax.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1hIATAUxQEtoBPPKABwBsAPSAXwF0AacKLeLADW9UBAiR0DeAGNqdfAAYAdAHZe4qMjjohZepABGTWSI3jI22LrIAReMgAW9AgCZuYACw8+E2g318SEEyeEg+dnNSeGgw-DELAFdYJgNHaGgGZFwAehyEAHdlAHNqR0TDROQyWBl0RGgKaGU6rBzbdETigCFkvRzHADcyYvgcgW0ageHRnJIYsdHKeABaeFWANgBGDbWtxRXY4zIt+GVEdBXHUPJYZWhkQchzCQxsGNFfC2lYas-NTSQADiAmKBkQiSwhhqzy+gIASgBBAD6AEkAMLwgDK4Mh0NgsIB31YuKhMJeAMgtgAomjMTighCyQSKYCyMjmGxGXiYXCwOwvgKIJE+JAACTIGTXARpDJZXI5GZnUrQcqGZSUdA5SXSsZKlZMahkRWeZQAK2Q9UJUAodRIlEQYPiX0gXLACUBqEoZCYJAMg3gTES+lZUH8gSgAEdEkhoNQYpRhtbAfAWJRkKIYNQmBHID9AyY2ELxCLAQp3ZAvT6-UFyFRaM8wzQAgZo7H43Gk8XS1A6ogvU73QL2EA Open this visualization in the Vega Editor>

@
let trans = transform
            . 'density' \"plx\" []

    enc = encoding
          . position X [ PName \"value\"
                       , PmType Quantitative
                       , PAxis [ AxTitle \"parallax\" ]
                       ]
          . position Y [ PName \"density\", PmType Quantitative ]

in toVegaLite
     [ gaiaData
     , mark 'Area' [ MOpacity 0.7
                 , MStroke \"black\"
                 , 'MStrokeDash' [ 2, 4 ]
                 , ]
     , trans []
     , enc []
     ]
@

The parallax distribution shows multiple peaks within the
5 to 10 milli-arcsecond range, and separate peaks at 12
and 22 milli-arcseconds.

The properties of the area mark are set here to add a black,
dashed line around the edge of the area. The 'DashStyle' configures
the pattern by giving the lengths, in pixels,
of the \"on\" and \"off" segments, so here the gaps are twice the
length of the line segments. This was done more to show that it
can be done, rather than because it aids this particular visualization!

-}

densityParallax :: VegaLite
densityParallax :: VegaLite
densityParallax =
  let trans :: [TransformSpec] -> PropertySpec
trans = [TransformSpec] -> PropertySpec
transform
              ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [DensityProperty] -> [TransformSpec] -> [TransformSpec]
density Text
"plx" []

      enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
            ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"value"
                         , Measurement -> PositionChannel
PmType Measurement
Quantitative
                         , [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"parallax" ]
                         ]
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"density", Measurement -> PositionChannel
PmType Measurement
Quantitative ]

  in [PropertySpec] -> VegaLite
toVegaLite
       [ PropertySpec
gaiaData
       , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Area [ Double -> MarkProperty
MOpacity Double
0.7
                   , Text -> MarkProperty
MStroke Text
"black"
                   , [Double] -> MarkProperty
MStrokeDash [ Double
2, Double
4 ]
                   ]
       , [TransformSpec] -> PropertySpec
trans []
       , [EncodingSpec] -> PropertySpec
enc []
       ]


{-|

The density estimation can be configured using 'DensityProperty'.
Here we explicitly label the new fields to create (rather than
use the defaults), and ensure the calculation is done per cluster.
This means that the data range for each cluster is used to
perform the KDE, which in this case is useful (as it ensures the
highest fidelity), but there are times when you may wish to ensure
a consistent scale for the evaluation (in which case you'd use
the 'DnExtent' option, as well as possibly 'DnSteps', to define
the grid). The final change is to switch from density estimation
to counts for the dependent axis.

<<images/vl/densityparallaxgrouped.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1wIxQSTIBzWdAVwAcAjATz0MgGEAbW5aAU1iQAugBpi5SPGRsCkAB4BrACb9IoqMxVqxEigGM6iaDPxxa-ceQiRVKAJbRW+SPS7zIegL7FdNrPCwimzAkOj08PqOzmAADAB0AOwaMMz0ai6B-PCQXlZQyvDQOfhkFLSwXGyQABbQ0PTIuAD0zQgA7vGUjjW0jLyChsb8xvGGWM0AInSUAEIVivzNNQBu-JTwzQF8gstrG82Fxc0b9vAAtPAXAGwAjNeXt7HnxYxc-Lfw8Yjo5zXZqlg8RMK3UekgGGwRRCehsEVgyAypFhFAA4gFKNVELQsIxBGDrBQAEoAQQA+gBJDhEgDKWJxeKE+UJrnc9Nx+OZ1kgkwAopTqXSXNiOUyUTZ+GS3B5hQz8SifORFWA8sRIAASZD6f4Bap1BpNVr7L7daC9RjxezoZpanWbY3nLiOJYrAAs8QAVsh0IgCVARoZlPZEJjSuDDFxMCEIfZ+FxlNVuLwBEyoE50lj0Fhg-AqqrJDLkSzULH49UlKo-ZJ00jIABHWhIaCOIr2NZVijweT2UykGCOd7VAAKgVzXC7uW8XMgMVCJbjCZcWkrKRr1QbTZbzfbKS7Pejzegg5cHCMJlyyvzUCGJdDpB8XiAA Open this visualization in the Vega Editor>

@
let trans = transform
            . density "plx" [ 'DnAs' \"xkde\" \"ykde\"
                            , 'DnGroupBy' [ \"Cluster\" ]
                            , 'DnCounts' True
                            ]

    enc = encoding
          . position X [ PName \"xkde\"
                       , PmType Quantitative
                       , PAxis [ AxTitle \"Parallax\" ]
                       ]
          . position Y [ PName \"ykde\"
                       , PmType Quantitative
                       , PAxis [ AxTitle \"Counts\" ]
                       ]
          . color [ MName \"Cluster\"
                  , MmType Nominal
                  ]

in toVegaLite
     [ gaiaData
     , mark Area [ MOpacity 0.7 ]
     , trans []
     , enc []
     ]
@

Note how the clusters separate out in pretty cleanly, but - as
also shown in the 'pointPlot' visualization below - it is pretty
busy around 7 milli arcseconds.

The counts here (the Y axis) are __significantly larger__ than
seen than the actual count of stars, shown in 'starCount'. It
appears that the @'DnCounts' True@ option is interpreted as
<https://vega.github.io/vega-lite/docs/density.html#example-stacked-density-estimates multiplying the density values by the number of values in a group>,
which means that there is a bin-width effect. This is explored
further in the 'compareCounts' plot below.

-}

densityParallaxGrouped :: VegaLite
densityParallaxGrouped :: VegaLite
densityParallaxGrouped =
  let trans :: [TransformSpec] -> PropertySpec
trans = [TransformSpec] -> PropertySpec
transform
              ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [DensityProperty] -> [TransformSpec] -> [TransformSpec]
density Text
"plx" [ Text -> Text -> DensityProperty
DnAs Text
"xkde" Text
"ykde"
                              , [Text] -> DensityProperty
DnGroupBy [ Text
"Cluster" ]
                              , Bool -> DensityProperty
DnCounts Bool
True
                              ]

      enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
            ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"xkde"
                         , Measurement -> PositionChannel
PmType Measurement
Quantitative
                         , [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"Parallax" ]
                         ]
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"ykde"
                         , Measurement -> PositionChannel
PmType Measurement
Quantitative
                         , [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"Counts" ]
                         ]
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster"
                    , Measurement -> MarkChannel
MmType Measurement
Nominal
                    ]

  in [PropertySpec] -> VegaLite
toVegaLite
       [ PropertySpec
gaiaData
       , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Area [ Double -> MarkProperty
MOpacity Double
0.7 ]
       , [TransformSpec] -> PropertySpec
trans []
       , [EncodingSpec] -> PropertySpec
enc []
       ]


-- $intro-points
-- At this point we make a signifiant detour from the Elm Vega-Lite
-- walkthtough, and look a bit more at the 'Point' mark, rather than creating
-- small-multiple plots. Don't worry, we'll get to them later.
--
-- I apologize for the alliterative use of point here.

{-|

Here I use the 'Point' mark to display the individual
@Gmag, plx@ pairs, encoding by __both__ 'color' and 'shape.
Since the encoding uses the same field of the data (the @Cluster@
name), Vega-Lite is smart enough to only display one legend,
which contains the point shape and color used for each cluster.

Since the parallax values are bunched together at low values,
a logarithmic scale ('ScLog') is used for the y axis, along with
commands to define the actual axis domain - by turning off the
'IsNice' support and listing the minimum and maximum values
for the axis with 'SDomain'.

<<images/vl/pointplot.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAWCmCWBzaAXSAuMAWADDgNOFALYCGATgNYZQAOA9vAHZqESQAmpKpNoE7AK7kANjRgoUtAM7oA9HPKkA7gDpE8FNEEAjQdNjkAxvRawWqk8TkAReoMQAhYZVhzoAN1iJScstJRDdy8fOS4eOR94UgBaUliANgBGBLiknBieHRFYJNJVJnoYuFIOQ1UUaQ9INgFIADN6cjI0TH4BdloKAz4iDvYAcTJEcSZBYh1DGr7+yAAlAEEAfQBJAGE5gGVR8cnyaf662hEADx2JqdrDyBsAUVWN7cxIMYv9q9nYJeOz59e9yAzCAAXxmoJBtUgyngHC0NFwBCIkAAJNIjHAyOJUFJZAoQvkNFpdKp4PQ5GiMb58TERJo3B4sKoAFbSUwHKDmEwcZgjNozSAmERNPgNeCwEQccRrET6QLvKAoACetFgo3oxGYpDEwI+kGk0FIKpF9TFEqlMoClwVytVf3Vmu1ut+YGAovFkueQ1II3w1qNzwAjoJSCxNNx4F5IDr+YretcTe7xD92R09UYtbaXZx1aRmDQANoAZlUAFZfYWAEwAXV9kCV-qgQp9UCY8CMmfqWoM0eu9czkCDIZQYeHkbBRB7UBMTBNvJdoOBQA Open this visualization in the Vega Editor>

@
let enc = encoding
                    . position X [ PName \"Gmag\", PmType Quantitative ]
                    . position Y [ PName \"plx\", PmType Quantitative, PScale scaleOpts ]
                    . color cluster
                    . shape cluster

    scaleOpts = [ SType ScLog, 'SDomain' ('DNumbers' [3.5, 32]), 'SNice' ('IsNice' False) ]
    cluster = [ MName \"Cluster\", MmType Nominal ]
, 
in toVegaLite [ gaiaData
              , mark Point []
              , enc []
              , width 400
              , height 400
              ]
@

We can see that each cluster appears to have a separate parallax
value (something we have seen in earlier plots, such as 'parallaxBreakdown'),
and that it doesn't really vary with Gmag. What this is telling
us is that for these star clusters, the distance to each member star
is similar, and that they are generally at different distances
from us. However, it's a bit hard to tell exactly what is going
on around 5 to 6 milli arcseconds, as the clusters overlap here.

This line of thinking leads us nicely to map making, but before we
try some cartography, I wanted to briefly provide some context for
these plots. The
<https://lco.global/spacebook/distance/parallax-and-distance-measurement/ parallax of a star>
is a measure of its distance from us, but it is an inverse relationship,
so that nearer stars have a larger parallax than those further from us.
The @Gmag@ column measures the apparent brightness of the star, with the
@G@ part indicating what
<https://www.cosmos.esa.int/web/gaia/iow_20180316 part of the spectrum>
is used (for Gaia, the @G@ band is pretty broad, covering much of
the visible spectrum), and the @mag@ part is because optical Astronomy
tends to use

 * <https://www.skyandtelescope.com/astronomy-resources/the-stellar-magnitude-system/ the logarithm of the measured flux>

 * and then subtract this from a constant

so that larger values mean fainter sources. These are also
<https://en.wikipedia.org/wiki/Apparent_magnitude apparent magnitues>,
so that they measure the flux of the star as measured at Earth,
rather than its intrinsic luminosity (often defined as an object's
<https://en.wikipedia.org/wiki/Absolute_magnitude absolute magnitude>).

We can see that the further the cluster is from us - that is, as we
move down this graph to smaller parallax values - then the
smallest stellar magnitude we can see in a cluster tends to increase,
but that there are stars up to the maximum value (20) in each cluster.
This information can be used to look at the
distribution of absolute magnitudes of stars in a cluster, which
tells us about its evolutionary state - such as is it newly formed or old -
amongst other things. However, this is straying far from the
aim of this tutorial, so lets get back to plotting things.

-}

pointPlot :: VegaLite
pointPlot :: VegaLite
pointPlot =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
              ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Gmag", Measurement -> PositionChannel
PmType Measurement
Quantitative ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"plx", Measurement -> PositionChannel
PmType Measurement
Quantitative, [ScaleProperty] -> PositionChannel
PScale [ScaleProperty]
scaleOpts ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
shape [MarkChannel]
cluster
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [MarkChannel]
cluster

      scaleOpts :: [ScaleProperty]
scaleOpts = [ Scale -> ScaleProperty
SType Scale
ScLog, DomainLimits -> ScaleProperty
SDomain ([Double] -> DomainLimits
DNumbers [Double
3.5, Double
32]), ScaleNice -> ScaleProperty
SNice (Bool -> ScaleNice
IsNice Bool
False) ]
      cluster :: [MarkChannel]
cluster = [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal ]

  in [PropertySpec] -> VegaLite
toVegaLite [ PropertySpec
gaiaData
                , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Point []
                , [EncodingSpec] -> PropertySpec
enc []
                , Double -> PropertySpec
width Double
400
                , Double -> PropertySpec
height Double
400
                ]

-- $intro-map
-- We have some hint that the different clusters are distinct objects
-- in space, in that they appear to be different distances from us,
-- but where does the \"cluster\" in the name \"Stellar Cluster\"
-- come from? Well, we can try plotting up the position of each star
-- on the sky - using the @RA_ICRS@ and @DE_ICRS@ fields - to find out.

{-|

The following specification should only contain one new feature - other
than sneakily switching from 'Point' to 'Circle' type for the mark - and
that is displaying the x axis (namely Right Ascension) in reverse (using
@'PSort' [ 'Descending' ]@. This is needed because Right Ascension
is measured from right to left. I like to explain it by talking about
oranges, and how we are at the center of an orange looking out at its
skin, and so have the direction reversed to if you were outside, looking
in. This may be why I don't get invited to too many parties.
You can see that we also have one cluster that straddles the
0 and 360 degrees Right Ascension meridian, which will lead to some
fun later ('clusterCenters').

<<images/vl/posplot.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAWCmCWBzaAXSAuMBmADDgNOFALYCGATgNYZQDG85tANrJIRJACakqk2gQOAV3JMaMFCgAOAZ3QB6eeVIB3AHSJ4KaEIBGQmbEYB7AHYpY5tbWPF5AEWNDEAIRGVY86ADdYiUvJkMhbkXr7+8ty88v7wpAC0pAkAbACMyYmpOPG8uiyppGqmxvFwpJxGaigy3mxEHABmxuRkaJgCghxSFIb89Z1QAOJkiOKmQsS6RnUDgpAASgCCAPoAkgDC8wDKYxNT5DOzUFJMAB67k9PsR5D2AKJrmzuYkOOXB9ezkLDLJ+cvb32kH6EAAvv1wWDrpAVPBONoaAAWPDQgAkMlocDI4lQ0jkinChU02j0angxnkGKxAUJ8SYWk83kRagAVjIzIdvqYbJx4KZRu1+pAbExmvxIA14LAmJxxOsmAYQmwoCgAJ5SVgA2x80hiUGfKD-MAdToSqUy8RLR7bQ6mjG6zXGri2Uh8mgAbQI2GSOAAuvgoKZ4LRHQ1dYZ9SCoOzyG0oBUMZZefzbXM1RrxABHISkcxaHjwXypjikU7wGTilBaFiWpCoMCLROmGTk0xgAAUFUQAEpIJDBJHTaq+l9JdLZS97tadgaOPaa+1nWQ3Zh3fEAJxezf+wPB0Ph2CDgaQdOOyDZ3NV3hVouzqCl8uV6tn+ywZg6qtmDtd3v9sFEI9hTMSUBWNcFQSAA Open this visualization in the Vega Editor>

@
let enc = encoding
            . position X (axOpts \"RA_ICRS\" \"Right Ascension (deg)\" ++ [ raScale, 'PSort' [ 'Descending' ] ])
            . position Y (axOpts \"DE_ICRS\" \"Declination (deg)\" ++ [ decScale ])
            . color [ MName \"Cluster\", MmType Nominal ]

    axOpts field lbl = [ PName field, PmType Quantitative, PAxis [ AxTitle lbl ]]

    scaleOpts minVal maxVal = [ SDomain (DNumbers [ minVal, maxVal ]), SNice (IsNice False) ]
    raScale = PScale (scaleOpts 0 360)
    decScale = PScale (scaleOpts (-90) 90)

in toVegaLite [ gaiaData
              , mark 'Circle' []
              , enc []
              , width 400
              , height 300
              ]
@

We can see that these clusters are indeed localised on the sky,
with <https://en.wikipedia.org/wiki/Hyades_(star_cluster) Hyades>
looking like it covers the largest area. However, we should be
careful and __not forget__ either
<https://www.youtube.com/watch?v=E9IuXEwpU7U Grover's hard work>
or
<https://www.youtube.com/watch?v=MMiKyfd6hA0 Father Ted's explanation to Father Dougal>,
since these clusters are different distances from us, which
makes size a tricky thing to measure from this plot.

There is also the fact that I have used possibly the worst
way of displaying the Right Ascension and Declination data. Although
the night sky is not the same as the Earth's surface, the issues
when trying to display the Globe on a flat surface also apply to
displaying up the sky. For this plot the distortions near the
pole are huge, although fortunately we don't have any clusters
too close to either pole.

-}

posPlot :: VegaLite
posPlot :: VegaLite
posPlot =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
              ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X (Text -> Text -> [PositionChannel]
axOpts Text
"RA_ICRS" Text
"Right Ascension (deg)" [PositionChannel] -> [PositionChannel] -> [PositionChannel]
forall a. [a] -> [a] -> [a]
++ [ PositionChannel
raScale, [SortProperty] -> PositionChannel
PSort [ SortProperty
Descending ] ])
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y (Text -> Text -> [PositionChannel]
axOpts Text
"DE_ICRS" Text
"Declination (deg)" [PositionChannel] -> [PositionChannel] -> [PositionChannel]
forall a. [a] -> [a] -> [a]
++ [ PositionChannel
decScale ])
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal ]

      axOpts :: Text -> Text -> [PositionChannel]
axOpts Text
field Text
lbl = [ Text -> PositionChannel
PName Text
field, Measurement -> PositionChannel
PmType Measurement
Quantitative, [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
lbl ]]

      scaleOpts :: Double -> Double -> [ScaleProperty]
scaleOpts Double
minVal Double
maxVal = [ DomainLimits -> ScaleProperty
SDomain ([Double] -> DomainLimits
DNumbers [ Double
minVal, Double
maxVal ]), ScaleNice -> ScaleProperty
SNice (Bool -> ScaleNice
IsNice Bool
False) ]
      raScale :: PositionChannel
raScale = [ScaleProperty] -> PositionChannel
PScale (Double -> Double -> [ScaleProperty]
scaleOpts Double
0 Double
360)
      decScale :: PositionChannel
decScale = [ScaleProperty] -> PositionChannel
PScale (Double -> Double -> [ScaleProperty]
scaleOpts (-Double
90) Double
90)

  in [PropertySpec] -> VegaLite
toVegaLite [ PropertySpec
gaiaData
                , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Circle []
                , [EncodingSpec] -> PropertySpec
enc []
                , Double -> PropertySpec
width Double
400
                , Double -> PropertySpec
height Double
300
                ]


{-|
Vega-Lite supports a large number of projections - via the
'Projection' type - which we use below to create
a similar visualization to 'posPlot'. Here I use the
'Longitude' and 'Latitude' channels, along with a
'Mercator' 'projection', to display the data.

The trick in this case is that longitude runs from -180 to 180
degrees, but the data has Right Ascension going from 0
to 360 degrees. Here we take advantage of Vega Lite's
__data transformation__ capabilities and create a new
column - which I call @longitude@ - and is defined as
\"Right Ascension - 360\" when the Right Ascension is
greater than 180, otherwise it is just set to the
Right Ascension value. The "expression" support
is essentially a sub-set of Javascript, and the @datum@
object refers to the current row. The new data column
can then be used with the 'Longitude' channel.
Thankfully the 'Latitude' channel can use the Declination values
without any conversion.

As can be seen, this flips the orientation compared to
'posPlot', and makes the center of the plot have a
longiture (or Right Ascension), of 0 degrees.

<<images/vl/skyplot.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1wIxQSTJ5k8oAbdRAcwEtoBXAEwFNIAaY8pADG8WkLa140HvkgcpbLADoASgEEA+gEkAwioDKYAHxgAjAA4ADGAD8Yee2XrtewwFowAZgBs1-A8VVTV0DSAEwAF9iAF1+CEgACy5mRgToGgAWS0s4qCx4WABrGmFmWCFaHly5KXgaMgo2WFoStOgAB2RcAHpuhAB3JRZoBLYAIzZkLnKGaURoJSF0LG6AEXQ2RgAhJsKuboSANy5GeG785GlYA+PT7ocz0+Z4N3gX71NvV9NLN2h4MaVUzwJSIdBuJLwbiwJTQZCHPjhSAYbBSerheLtApTdHkQQAcXyjBKiEUY2miLxFGcIX0JLJFNyVMg7VoAA96VhybBKVSoKsAKIuUKyUlcxkYihcDSsjmihk8yVRcjKyLVfrMDgjGieACsOWIkAAJMghEl8q1oB0ur1biDhqMxkpmOhuqbzWc7W5aKx9ocMkoAFbIBi8lmwdCBrhCaAuxD1GAAT3aMjy0xE0EwkAi1S4iCWHGYTFxFCW9B5+AaglQzC4tA4JVlvMEptEqeAkHdXCwqcghzKmuY1F4UGgyd79GJOclSZTJQAjmwkLH-rHjs2KJVGHmG5WYKxKo2CqJJBzVRBp4JJCvOO3kbX6yVBcK6SPZ73F8vWFJmOvLxRM3QWhY3aBMazrXcoB0WhJiuPhR3HElliLURsyZOgGGGW8wIfSDIHoJhWGwt8xznWRP3mb81x4VV-2EBga2JSsogiIA Open this visualization in the Vega Editor>

@
let trans = transform
                . 'calculateAs'
                  "datum.RA_ICRS > 180 ? datum.RA_ICRS - 360 : datum.RA_ICRS"
                  "longitude"

    axOpts field = [ PName field, PmType Quantitative ]

    enc = encoding
            . position 'Longitude' (axOpts "longitude")
            . position 'Latitude' (axOpts \"DE_ICRS\")
            . color [ MName \"plx\"
                    , MmType Quantitative
                    , 'MScale' [ SType ScLog
                             , 'SScheme' \"viridis\" []
                             ]
                    , MLegend [ 'LTitle' "parallax" ]
                    ]
            . 'tooltip' [ 'TName' \"Cluster\", 'TmType' Nominal ]

in toVegaLite [ width 400
              , height 350
              , 'projection' [ 'PrType' 'Mercator' ]
              , gaiaData
              , trans []
              , enc []
              , mark Circle []
              ]
@

The other major change made to 'posPlot' is that the stars are now
color-encoded by the log of their parallax value
rather than cluster membership,
and the color scheme has been changed to use the \"viridis\" color
scale.
The 'LTitle' option is set for the legend (on the
'color' channel) rather than use the default (which in
this case would be @\"plx\"@).

Since parallax is a numeric value, with ordering (i.e. 'Quantitative'),
the legend has changed from a list of symbols to a gradient bar.
To account for this lost of information, I have added a 'tooltip'
encoding so that when the pointer is moved over a star its cluster
name will be displayed. This is, unfortunately,
/only/ visible in the interactive version of the visualization.

__Note that__ the tooltip behavior changed in Vega Lite 4 (or in the
code used to display the visualizations around this time), since
prior to this tooltips were on by default. Now tooltips have to be
explicitly enabled (with 'tooltip' or 'tooltips').

From this visualization we can see that the apparent size of the cluster
(if we approximate each cluster as a circle, then we can think of the radius
of the circle as a measure of size) depends on parallax, with larger
sizes having larger parallaxes. This is because the distance to a star
is inversely-dependent on its parallax, so larger parallaxes mean the
star is closer to us. However, there is no reason that the intrinsic
size - that is its actual radius - of each cluster is the same.
We can see that although the Hyades and Pleiades clusters overlap
on the sky, they have significantly-different parallaxes (as can
be seen in 'pointPlot' for example), with Hyades being the closer
of the two.

It is possible to add graticules - with the aptly-named
'graticule' function - but this requires the use of layers,
which we haven't covered yet. If you are impatient you can jump
right to 'skyPlotWithGraticules'!

If you want to see how to "create your own projection", see
'skyPlotAitoff', which uses the
<https://en.wikipedia.org/wiki/Aitoff_projection Aitoff projection>
(which is unfortunately not available to
Vega-Lite directly).

-}

skyPlot :: VegaLite
skyPlot :: VegaLite
skyPlot =
  let trans :: [TransformSpec] -> PropertySpec
trans = [TransformSpec] -> PropertySpec
transform
                ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> [TransformSpec] -> [TransformSpec]
calculateAs
                  Text
"datum.RA_ICRS > 180 ? datum.RA_ICRS - 360 : datum.RA_ICRS"
                  Text
"longitude"

      axOpts :: Text -> [PositionChannel]
axOpts Text
field = [ Text -> PositionChannel
PName Text
field, Measurement -> PositionChannel
PmType Measurement
Quantitative ]

      enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
              ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Longitude (Text -> [PositionChannel]
axOpts Text
"longitude")
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Latitude (Text -> [PositionChannel]
axOpts Text
"DE_ICRS")
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"plx"
                      , Measurement -> MarkChannel
MmType Measurement
Quantitative
                      , [ScaleProperty] -> MarkChannel
MScale [ Scale -> ScaleProperty
SType Scale
ScLog
                               , Text -> [Double] -> ScaleProperty
SScheme Text
"viridis" []
                               ]
                      , [LegendProperty] -> MarkChannel
MLegend [ Text -> LegendProperty
LTitle Text
"parallax" ]
                      ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TextChannel] -> BuildEncodingSpecs
tooltip [ Text -> TextChannel
TName Text
"Cluster", Measurement -> TextChannel
TmType Measurement
Nominal ]
              -- note: opacity doesn't really help here

  in [PropertySpec] -> VegaLite
toVegaLite [ Double -> PropertySpec
width Double
350
                , Double -> PropertySpec
height Double
400
                , [ProjectionProperty] -> PropertySpec
projection [ Projection -> ProjectionProperty
PrType Projection
Mercator ]
                , PropertySpec
gaiaData
                , [TransformSpec] -> PropertySpec
trans []
                , [EncodingSpec] -> PropertySpec
enc []
                , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Circle []
                ]


-- $intro-choropleth
-- There are some things vega-lite can do, don't fit as well into the
-- flow of looking at astronomy data!  But having examples is helpful.
-- So we bring our eyes back to earth, and demonstrate a basic
-- "choropleth", a map - in the sense of pictures of bounded geographical
-- regions - with data for each location indicated by color.
--
-- Don't worry, we'll soon be back staring at the stars!
--
-- The choropleth examples (there's another one later on)
-- use a map of the United States as the data source, which we abstract
-- out into a helper function:
--
-- @
-- usGeoData :: T.Text -> Data
-- usGeoData f = dataFromUrl \"https:\/\/raw.githubusercontent.com\/vega\/vega\/master\/docs\/data\/us-10m.json\" ['TopojsonFeature' f]
-- @
--
-- The argument gives the \"topological\" feature in the input file to
-- display (via 'TopojsonFeature'). You can read more information on this
-- in the <https://vega.github.io/vega-lite/docs/data.html#topojson Vega-Lite documentation>.
--
-- This section was contributed by Adam Conner-Sax. Thanks!

usGeoData :: T.Text -> Data
usGeoData :: Text -> PropertySpec
usGeoData Text
f = Text -> [Format] -> PropertySpec
dataFromUrl Text
"https://raw.githubusercontent.com/vega/vega/master/docs/data/us-10m.json" [Text -> Format
TopojsonFeature Text
f]


{-|

Our first choropleth is based on the
<https://vega.github.io/vega-lite/examples/geo_choropleth.html Choropleth>
example from the Vega-Lite
<https://vega.github.io/vega-lite/examples/ Example Gallery>.

The key elements are:

   * Using the 'TopojsonFeature' feature for the data source (thanks to @usGeoData@).
   * Choosing the correct "feature" name in the geographic data, here @\"counties\"@
     in the argument to our @usGeoData@ helper function.
   * Performing a Vega-Lite lookup to join the data to be plotted (the unemployment rate)
     to the geographic data.  In this case, the column name in the unemployment data - @\"id\"@
     given as the first argument to 'lookup' - is the same as the column name in the
     geographic data, the third argument to 'lookup'. Those can be different.
   * Specifying a projection, that is a mapping from (longitude, latitude) to (x,y)
     coordinates. Since we are looking at data for the main-land United States of
     America we use 'AlbersUsa' (rather than looking at the whole globe, as we did
     in earlier visualizations), which lets us view the continental USA as well as Alaska and Hawaii.
   * Using the 'Geoshape' mark.

<<images/vl/choroplethlookuptogeo.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1wIxQSTJVZ0d8zyKATeaePU4higV1gBt2kABbRoAB2S4A9FIQB3AHQBzAJbQh3AEbdkAU1gBjdImi6TCo1ikA3XUvg27DrPGSnYUxugPJPLB9yIulhifOgAnlhm0ArQyNaQnAwAvgA0SeSQANa64YIqjJDpXBCUKrp8jMjsBJAIppAAuhlgaS2QYehZ3GL5hUnJxI3FUEK6Kkoi7ADMAAyzI5AusFmCSrroyELwYrpFxJDMrOz0pbwC+MKiEtKy8Iqq6lo6+kYm0RY0jvbfzq7unm8viOAWQAFoAIyzLAKABWyGM+0yGGwLBOlF0LF4e0uRkC0HK1VSUGg4V2gmg6DE6HhiMGEDapTkBXU7AArPNFgASZAGMYuQQicSSGS2ezKNQaTQKFToKS8-kOMXwMF8NS6GwAFjhCMQSKgYmosN0BgJiLoMDJOKg8D4mn0yAAqsg2IyoGYjIwVIglCckpAjGFYH6uGUKoVLvU9iNMrzbdbgJAFcFrZAxLxQhsED69m7MqTyZcAI7cJAE1gE2yJcj01qLTTwAxZJTUQIRm0AL2x+oDxlQExOg2SQA Open this visualization in the Vega Editor>

@
let unemploymentData = dataFromUrl \"https:\/\/raw.githubusercontent.com\/vega\/vega\/master\/docs\/data\/unemployment.tsv\" []

in toVegaLite
   [ usGeoData \"counties\"
   , transform
     . 'lookup' \"id\" unemploymentData \"id\" ('LuFields' [\"rate\"])
     $ []
   , projection [PrType 'AlbersUsa']
   , encoding
     . color [ MName \"rate\", MmType Quantitative, MScale [ SScheme "purpleorange" [] ] ]
     $ []
   , mark Geoshape []
   , width 500
   , height 300
   , 'background' "azure"
   ]
@

So, we have seen how to join data between two datasets - thanks to
'lookup' - and display the unemployment rate (from one data source)
on a map (defined from another data source).

I have chosen a
<https://vega.github.io/vega/docs/schemes/index.html#diverging diverging color scheme>
for the rate, mainly just because I can, but also because I wanted to see how
the areas with high rates were clustered. I've also shown how the 'background'
function can be used (it is simpler than the 'configuration' approach
used earlier in 'stripPlotWithBackground').

Our next choropleth - 'choroplethLookupFromGeo' - will show how we can join
multiple fields across data sources, but this requires understanding how
Vega-Lite handles multiple views, which is fortunately next in our
tutorial.

-}

choroplethLookupToGeo :: VegaLite
choroplethLookupToGeo :: VegaLite
choroplethLookupToGeo =
  let unemploymentData :: PropertySpec
unemploymentData = Text -> [Format] -> PropertySpec
dataFromUrl Text
"https://raw.githubusercontent.com/vega/vega/master/docs/data/unemployment.tsv" []

  in [PropertySpec] -> VegaLite
toVegaLite
     [ Text -> PropertySpec
usGeoData Text
"counties"
     , [TransformSpec] -> PropertySpec
transform
       ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> PropertySpec
-> Text
-> LookupFields
-> [TransformSpec]
-> [TransformSpec]
lookup Text
"id" PropertySpec
unemploymentData Text
"id" ([Text] -> LookupFields
LuFields [Text
"rate"])
       ([TransformSpec] -> PropertySpec)
-> [TransformSpec] -> PropertySpec
forall a b. (a -> b) -> a -> b
$ []
     , [ProjectionProperty] -> PropertySpec
projection [Projection -> ProjectionProperty
PrType Projection
AlbersUsa]
     , [EncodingSpec] -> PropertySpec
encoding
       ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"rate", Measurement -> MarkChannel
MmType Measurement
Quantitative, [ScaleProperty] -> MarkChannel
MScale [ Text -> [Double] -> ScaleProperty
SScheme Text
"purpleorange" [] ] ]
       ([EncodingSpec] -> PropertySpec) -> [EncodingSpec] -> PropertySpec
forall a b. (a -> b) -> a -> b
$ []
     , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Geoshape []
     , Double -> PropertySpec
width Double
500
     , Double -> PropertySpec
height Double
300
     , Text -> PropertySpec
background Text
"azure"
     ]


-- $intro-layered
-- The Stacked-Histogram plot - created by 'gmagHistogramWithColor' - showed
-- the distribution of the \"Gmag\" field by cluster, but it was hard to
-- compare them. A common approach in this situation is to split up
-- the data into multiple plots -
-- the <https://en.wikipedia.org/wiki/Small_multiple small multiple>
-- approach (also known as trellis plots) - which we can easily achieve in
-- Vega Lite. It also gets us back on track with the Elm walkthrough.

{-|

Our first attempt is with the 'column' function, which tells
Vega-Lite to create a plot for each @Cluster@ field (and introduces
us to the @F@ family of 'FacetChannel' constructors).

The legend has been turned off with @'MLegend' []@, since it doesn't
add anything to this visulization (as the individual plots, labelled
by the cluster name, provide the same information).

<<images/vl/smallmultiples.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEC2CGBOBrSAuKAjBkA04oBNYAXWVMUCCSAV3gBszIALIogBwGcUB6b+WAO4A6AOYBLIk2rpqHAKbwAxgHsAdkTnqhK6NwAiy6iIBCtRHO5MAbnJGxucDhviWbd7oRLc7Y2AFpYfwA2AEYggJCABj8SdDo5ENghVWU-JjlYfAUhIg4rHDwqADNleDgiMgpKKjYEeUrC6qgAcTgRRlVqaHQFAqbKSAAlAEEAfQBJAGFBgGUOrp74Pv6oNjoAD3nu3twVqD0AUQnpubRITu2l3ZXIOVG1zbOLxchGiABfRs+P68gAEg4inScEYLHYXF4biS4kk0iEYmU3EBwPsUL8dAkFisABYhAArDhqZa3VQqfBiVTtNBVKgqOilSqQIpiOR0fCMSZ0WTOHBQIgATzYcg6ymgFNgDGwUHiIk07LQnTodHe1yoj3ITJZbMYrVg7SlkHQFLIRHg1DkBoFQsYAEdqLB1BJiGIbJAVY1IHSuqpGczWfKoJzuTs+YLhU9ReKGO7qpB+Q1+pA9SJ4LZiOGoCpqOplrGrRnIHaHUQnSXXaqBrB1mIOIyS0R4owAHILBRgZRFMAzEjwWvfSj9mOZtTMqnkT7vIA Open this visualization in the Vega Editor>

@
let enc = encoding
            . position X [ PName \"Gmag\", PmType Quantitative, PBin [] ]
            . position Y yAxis
            . color [ MName \"Cluster\", MmType Nominal, MLegend [] ]
            . 'column' [ 'FName' \"Cluster\", 'FmType' Nominal ]

    yAxis = [ PAggregate Count
            , PmType Quantitative
            , PAxis [ AxTitle \"Number of Stars\" ]
            ]

in toVegaLite
     [ gaiaData
     , mark Bar []
     , enc []
     ]
@

Since we have nine clusters in the sample, the overall visualization is
too wide, unless you have a very-large monitor. Can we do better?

-}

smallMultiples :: VegaLite
smallMultiples :: VegaLite
smallMultiples =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
              ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Gmag", Measurement -> PositionChannel
PmType Measurement
Quantitative, [BinProperty] -> PositionChannel
PBin [] ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [PositionChannel]
yAxis
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal, [LegendProperty] -> MarkChannel
MLegend [] ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FacetChannel] -> BuildEncodingSpecs
column [ Text -> FacetChannel
FName Text
"Cluster", Measurement -> FacetChannel
FmType Measurement
Nominal ]

      yAxis :: [PositionChannel]
yAxis = [ Operation -> PositionChannel
PAggregate Operation
Count
              , Measurement -> PositionChannel
PmType Measurement
Quantitative
              , [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"Number of Stars" ]
              ]

  in [PropertySpec] -> VegaLite
toVegaLite
       [ PropertySpec
gaiaData
       , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar []
       , [EncodingSpec] -> PropertySpec
enc []
       ]


{-|

The number of columns used in small-multiple can be defined using the
'columns' function. However, this requires us to:

 * move the facet definition out from the encoding and into the top-level,
   with the 'facetFlow' function;

 * and define the plot as a separate specification, and apply it
   with 'specification' and 'asSpec'.

The actual syntactic changes to 'smallMultiples' are actually
fairly minor:

@
let enc = encoding
            . position X [ PName \"Gmag\", PmType Quantitative, PBin [] ]
            . position Y yAxis
            . color [ MName \"Cluster\", MmType Nominal, MLegend [] ]

    yAxis = [ PAggregate Count
            , PmType Quantitative
            , PAxis [ AxTitle \"Number of Stars\" ]
            ]

in toVegaLite
     [ gaiaData
     , 'columns' 4
     , 'facetFlow' [ FName \"Cluster\", FmType Nominal ]
     , 'specification' ('asSpec' [ mark Bar [], enc [] ])
     ]
@

<<images/vl/smallmultiples2.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAmCGAutIC4yghSBXATgGxSgAt54AHAZ2QHpqdYB3AOgHMBLeIrAIywoFMcAYwD2AO3j8JTUQFtqAERFYWAIVwBrftSIA3fi1jVZsCpJw79h6nETVDbWAFpYzgGwBGNy48AGJ4jcePwesExiIk5E-LDQgkzwFLqQADTgGJAAZiI4JvCE6BiYZLA4AgXpRZgA4iYshJBiWLLcgqmVVZAASgCCAPoAkgDCXQDKDU0tbWlVGWR4AB4Tza047bOYCgCigyPjqI0r0x1FkPx980sHk6uQJ2AAvh1PEA8zUBRk-EIVpyY4Gga3FK6wyUlE0DYYnqqEKp1EeByBSybH4eGgDSGeD45lSUHgAE8vhMRLIobACCkoMEWFIMagmng8G97pArmgUWj6VBarB6lTINwoYR4DgsPwBYTiQcAI5YWASDgINj6SAs2aQAm-DaQPksHAGBD8BqiLASUEaqXG2XyxWIeAq43vDWwBZsCjIh3wYINAByRxwYBEmTAo0QZTV9xeGGj6qgABIKEJoiYGiRyFRaFYwuxODwmGwRNQkymjNmnHgONpdAAWJgAKwo4lBkARzTEHtQNfeWVgQn4+VhnPRmOxZmm+KJ1qgETJYgpap7ojEmTYMLQTweQA Open this visualization in the Vega Editor>

Note that Vega Lite does support a @\"facet\"@ field in its encodings,
but hvega follows Elm VegaLite and requires you to use this
<https://github.com/gicentre/elm-vegalite/issues/5#issuecomment-514501218 wrapped facet> approach.

I chose 4 columns rather than 3 here to show how "empty" plots
are encoded. You can see how a 3-column version looks in the
next plot, 'densityMultiples'.

-}

smallMultiples2 :: VegaLite
smallMultiples2 :: VegaLite
smallMultiples2 =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
              ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Gmag", Measurement -> PositionChannel
PmType Measurement
Quantitative, [BinProperty] -> PositionChannel
PBin [] ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [PositionChannel]
yAxis
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal, [LegendProperty] -> MarkChannel
MLegend [] ]

      yAxis :: [PositionChannel]
yAxis = [ Operation -> PositionChannel
PAggregate Operation
Count
              , Measurement -> PositionChannel
PmType Measurement
Quantitative
              , [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"Number of Stars" ]
              ]

  in [PropertySpec] -> VegaLite
toVegaLite
       [ PropertySpec
gaiaData
       , Natural -> PropertySpec
columns Natural
4
       , [FacetChannel] -> PropertySpec
facetFlow [ Text -> FacetChannel
FName Text
"Cluster", Measurement -> FacetChannel
FmType Measurement
Nominal ]
       , VLSpec -> PropertySpec
specification ([PropertySpec] -> VLSpec
asSpec [ Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar [], [EncodingSpec] -> PropertySpec
enc [] ])
       ]


{-|

Earlier - in 'densityParallaxGrouped' - I used the Kernel-Density
Estimation support in Vega Lite 4 to show smoothed parallax
distributions, grouped by cluster. We can combine this with
the 'facetFlow' approach to generate a plot per cluster
of the parallax distribution. I have used 'DnExtent' to ensure
that the density estimation is done on the same grid for
each cluster.

The most important thing in this example is that I have
used a sensible number of columns (ending up in a three by three grid)!
The other significant changes to 'smallMultiples2' is that I have
used the 'FHeader' option to control how the facet headers
are displayed: the title (which in this case was @\"Cluster\"@)
has been hidden, and the per-plot labels made larger, but moved
down so that they lie within each plot. I am not 100% convinced
this is an intended use of 'HLabelPadding', but it seems to work!

<<images/vl/densitymultiples.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAmCGAutIC4yghSBXATgGxSgAt54AHAZ2QHpqdYB3AOgHMBLeIrAIywoFMcAYwD2AO3j8JTUQFtqAERFYWAIVwBrftSIA3fi1jVZsCpJw79h6nETVDbWAFpYzgGwBGNy48AGJ4jcePwesExiIk5E-LDQgkzwFLqQADTgGJAAZiI4JvCE6BiYZLA4AgXpRZgA4iYshJBiWLLcgqmVVZAASgCCAPoAkgDCXQDKDU0tbWlVGWR4AB4Tza047bOYCgCigyPjqI0r0x1FkPx980sHk6uQJ2AAvh1PEA8zUBRk-EIVp-D0Ygo2VyhAA2vdCrNICwcMoyNwAJ5gyBDPB8cyQAC67yhpmRCw0cVSUARhP4WJxnX4C0kEjBvhSYAAzL5sfdMHFARwkQdLndZi8MGzTiYcBoGqUYusMlJRNA2GJ6qhIRlRHgcgUsmx+HhoA1UejplB4AivhMRLIFbACIzIMEWFI9agmng8G97pArmh2VBMtrdQ0CUTKX9TeSDgBHLCwCQcBBsfTS3ELNgUTXwDjBBoABVK1rwsCWgqK7qhPO9G19-qdJLJSc6JrNkejscQGcTIYyhdT6cz4agCikFG5kGLGDHgtLUAAJBQhNETA0SOQqLQrGF2JweEw2CJqHOF0Z1048BxtLoACxMABWFHE0sgauagMITPeWVgQn4+WVHS1OprFE0TMY5TmiWI2l-KEC1aPAejEecNQOR160wGCdVzaB5UVQgnAAJgvTs7VgWCADFxHgUY2AAL37LwiIzeAs2dLBXSI9C4NPFgxAaHA2BYEh+XHSlIEbftGgtK0CHSKdH3EP0lTQJ4HiAA Open this visualization in the Vega Editor>

@
let trans = transform
            . density "plx" [ DnAs \"xkde\" \"ykde\"
                            , DnGroupBy [ \"Cluster\" ]
                            , 'DnExtent' 0 30
                            ]

    enc = encoding
          . position X [ PName \"xkde\"
                       , PmType Quantitative
                       , PAxis [ AxTitle \"Parallax\" ]
                       ]
          . position Y [ PName \"ykde\"
                       , PmType Quantitative
                       , PAxis [ AxTitle \"Density\" ]
                       ]
          . color [ MName \"Cluster\"
                  , MmType Nominal
                  , MLegend []
                  ]

    headerOpts = [ 'HLabelFontSize' 16
                 , 'HLabelAlign' 'AlignRight'
                 , 'HLabelAnchor' 'AEnd'
                 , 'HLabelPadding' (-24)
                 , 'HNoTitle'
                 ]

    spec = asSpec [ enc []
                  , trans []
                  , mark Area [ ]
                  ]

in toVegaLite
     [ gaiaData
     , columns 3
     , facetFlow [ FName \"Cluster\"
                 , FmType Nominal
                 , 'FHeader' headerOpts
                 ]
     , specification spec
     ]
@

-}

densityMultiples :: VegaLite
densityMultiples :: VegaLite
densityMultiples =
  let trans :: [TransformSpec] -> PropertySpec
trans = [TransformSpec] -> PropertySpec
transform
              ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [DensityProperty] -> [TransformSpec] -> [TransformSpec]
density Text
"plx" [ Text -> Text -> DensityProperty
DnAs Text
"xkde" Text
"ykde"
                              , [Text] -> DensityProperty
DnGroupBy [ Text
"Cluster" ]
                              , Double -> Double -> DensityProperty
DnExtent Double
0 Double
30
                              ]

      enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
            ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"xkde"
                         , Measurement -> PositionChannel
PmType Measurement
Quantitative
                         , [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"Parallax" ]
                         ]
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"ykde"
                         , Measurement -> PositionChannel
PmType Measurement
Quantitative
                         , [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"Density" ]
                         ]
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster"
                    , Measurement -> MarkChannel
MmType Measurement
Nominal
                    , [LegendProperty] -> MarkChannel
MLegend []
                    ]

      headerOpts :: [HeaderProperty]
headerOpts = [ Double -> HeaderProperty
HLabelFontSize Double
16
                   , HAlign -> HeaderProperty
HLabelAlign HAlign
AlignRight
                   , APosition -> HeaderProperty
HLabelAnchor APosition
AEnd
                   , Double -> HeaderProperty
HLabelPadding (-Double
24)
                   , HeaderProperty
HNoTitle
                   ]

      spec :: VLSpec
spec = [PropertySpec] -> VLSpec
asSpec [ [EncodingSpec] -> PropertySpec
enc []
                    , [TransformSpec] -> PropertySpec
trans []
                    , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Area [ ]
                    ]

  in [PropertySpec] -> VegaLite
toVegaLite
       [ PropertySpec
gaiaData
       , Natural -> PropertySpec
columns Natural
3
       , [FacetChannel] -> PropertySpec
facetFlow [ Text -> FacetChannel
FName Text
"Cluster"
                   , Measurement -> FacetChannel
FmType Measurement
Nominal
                   , [HeaderProperty] -> FacetChannel
FHeader [HeaderProperty]
headerOpts
                   -- do not have access to plx field here
                   -- and xkde,ykde is not useful here as want to
                   -- sort by the xkde value when ykde is max
                   -- , FSort [ ByFieldOp "xkde" Median ]
                   ]
       , VLSpec -> PropertySpec
specification VLSpec
spec
       ]


-- $intro-multiplot
-- There are four ways in which multiple views may be combined:
--
-- * The __facet operator__ takes subsets of a dataset (facets) and
--   separately applies the same view specification to each of
--   those facets (as seen with the 'column' function above).
--   Available functions to create faceted views:
--   'column', 'row', 'facet', 'facetFlow', and 'specification'.
--
-- * The __layer operator__ creates different views of the data but
--   each is layered (superposed) on the same same space; for example
--   a trend line layered on top of a scatterplot.
--   Available functions to create a layered view: 'layer' and 'asSpec'.
--
-- * The __concatenation operator__ allows arbitrary views (potentially
--   with different datasets) to be assembled in rows or columns.
--   This allows \'dashboards\' to be built.
--   Available functions to create concatenated views:
--   'vConcat', 'hConcat', and 'asSpec'.
--
-- * The __repeat operator__ is a concise way of combining multiple views
--   with only small data-driven differences in each view.
--   Available functions for repeated views: 'repeat' and 'specification'.


{-|

We start with a \"basic\" plot for the dataset: the median value
of the parallax of the stars in each cluster.

<<images/vl/baseplot.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEC2CGBOBrSAuKAjBkA04oBNYAXWVMUCCSAV3gBszIALIogBwGcUB6b+WAO4A6AOYBLIk2rpqHAKbwAxgHsAdkTnqhK6NwAiy6iIBCtRHO5MAbnJGxucDhviWbd7oRLc7Y2AFpYfwA2AEYggJCABj8SdDo5ENghVWU-JjlYfAUhIg4rHDwqADNleDgiMgpKKjYEeUrC6qgAcTgRRlVqaHQFAqbKSAAlAEEAfQBJAGFBgGUOrp74Pv6oNjoAD3nu3twVqD0AUQnpubRITu2l3ZXIOVG1zbOLxchGiABfRs+P68gAEg4inScEYLHYXF4biS4kk0iEYmU3EBwPsUL8dAkFisABYhAArDhqZa3VQqfBiVTtNBVKiPciQIpiOR0fCMSZ0WTOHBQIgATzYcg6ymgFNgDHe1yovMqDKZLMYD25kFgIhE8FsxEFZ2gcnJsFUSr5AsYAEdqPqiBJiGIbJBvmAJXhICpVIyqeRPu8gA Open this visualization in the Vega Editor>

@
let plx = position Y [ PName \"plx\", PmType Quantitative, PAggregate Median ]
    cluster = position X [ PName \"Cluster\", PmType Nominal ]
    enc = encoding . cluster . plx

in toVegaLite
      [ gaiaData
      , mark Bar []
      , enc []
      ]
@
-}

basePlot :: VegaLite
basePlot :: VegaLite
basePlot =
  let plx :: BuildEncodingSpecs
plx = Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"plx", Measurement -> PositionChannel
PmType Measurement
Quantitative, Operation -> PositionChannel
PAggregate Operation
Median ]
      cluster :: BuildEncodingSpecs
cluster = Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Cluster", Measurement -> PositionChannel
PmType Measurement
Nominal ]
      enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuildEncodingSpecs
cluster BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuildEncodingSpecs
plx

  in [PropertySpec] -> VegaLite
toVegaLite
        [ PropertySpec
gaiaData
        , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar []
        , [EncodingSpec] -> PropertySpec
enc []
        ]


{-|

We start our exploration by combining two visualizations, layering
one on top of the other. The base plot shows the same data as
'basePlot', and then on top we will show a horizontal line that
indicates the median parallax for all the stars in the sample.

<<images/vl/layeredplot.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAmCGAutIC4yghSBXATgGxSgAt54AHAZ2QHpqdYB3AOgHMBLeIrAIywoFMcAYwD2AO3j8JTUQFtqAERFYWAIVwBrftSIA3fi1jVZsCpJw79h6nETVDbWAFpYzgGwBGNy48AGJ4jcePwesExiIk5E-LDQgkzwFLqQADTgGJAAZiI4JvCE6BiYZLA4AgXpRZgA4iYshJBiWLLcgqmVVZAASgCCAPoAkgDCXQDKDU0tbWlVGWR4AB4Tza047bOYCgCigyPjqI0r0x1FkPx980sHk6uQJ2AAvh1PEA8zUAAkFELRJg0k5CotCsYXYnB4TDYImo31+RhBTjwHG0ugALEwAFYUcTrKB4WAATzaqAA2h1gJATDgNA1uKVUlApKJoGwxPVUBSrmgsmx+HhoA0hng+OYGZB4ASyPwJiJZKzYAQHkr3hAKVSaQccFhgpAXmAALrvM5iZms9loDqQAkFHl8gUHS5i2AsFg4AwIaUHWT8FmwMRiiVShoARywfvgHAQbH0uvSb3SkFEYkybHNwCeDyAA Open this visualization in the Vega Editor>

@
let plx = position Y [ PName \"plx\", PmType Quantitative, PAggregate Median ]
    cluster = position X [ PName \"Cluster\", PmType Nominal ]

    perCluster = [ mark Bar [], encoding (cluster []) ]
    allClusters = [ mark 'Rule' [] ]

in toVegaLite
      [ gaiaData
      , encoding (plx [])
      , 'layer' (map 'asSpec' [perCluster, allClusters])
      ]
@

For this visualization, the specification starts with the data
source and an encoding, but __only__ for the y axis (which means
that all layered plots use the same encoding for the axis). The
'layer' function introduces the different visualizations that
will be combined, each as there own \"specification\" (hence
the need to apply 'asSpec' to both @perCluster@ and @allClusters@).
Note that there is no x-axis encoding for the 'Rule', since the
data applies to all clusters (i.e. it should span the
whole visualization).

-}
layeredPlot :: VegaLite
layeredPlot :: VegaLite
layeredPlot =
  let plx :: BuildEncodingSpecs
plx = Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"plx", Measurement -> PositionChannel
PmType Measurement
Quantitative, Operation -> PositionChannel
PAggregate Operation
Median ]
      cluster :: BuildEncodingSpecs
cluster = Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Cluster", Measurement -> PositionChannel
PmType Measurement
Nominal ]

      perCluster :: [PropertySpec]
perCluster = [ Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar [], [EncodingSpec] -> PropertySpec
encoding (BuildEncodingSpecs
cluster []) ]
      allClusters :: [PropertySpec]
allClusters = [ Mark -> [MarkProperty] -> PropertySpec
mark Mark
Rule [] ]

  in [PropertySpec] -> VegaLite
toVegaLite
        [ PropertySpec
gaiaData
        , [EncodingSpec] -> PropertySpec
encoding (BuildEncodingSpecs
plx [])
        , [VLSpec] -> PropertySpec
layer (([PropertySpec] -> VLSpec) -> [[PropertySpec]] -> [VLSpec]
forall a b. (a -> b) -> [a] -> [b]
map [PropertySpec] -> VLSpec
asSpec [[PropertySpec]
perCluster, [PropertySpec]
allClusters])
        ]


{-|

This example is similar to 'layeredPlot' but includes an x-axis
encoding for the second layer. We use this to show the range of the
data - so the minimum to maximum parallax range of each cluster - with
the 'Rule' type. The difference to the previous plot is that an
extra positional encoding is added ('Y2') to define the end point
of each line ('Y' is used as the start point).

<<images/vl/layereddiversion.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAWCmCWBzaAXSAuMBmADDgNOFACYCGKpGYoEEkArgE4A2VMKKADgM7oD0fRqQDuAOkTwU0egCN63WIwDGAewB2KWBtGqAtnwAiK+ogBCTANaw+0AG6xEpPrtLdNjG-cd8yFPo-hSAFpSYIA2AEYwkIicIIoZZlgI0lE1FSC4UmJFURRuW0hCWigAMxVGFzRMGhKoTlJGBSpauqgAcRdENjV6XRlFIqI2yAAlAEEAfQBJAGFRgGUevoHGIba6TmYAD2X+weKNyAMAURn5pcxIXv21w5HYSa3dq5vVyGHaAF9Pn4gvw6QYTwYhSKi4AhESAAEm4SjgLjYqC4vAEXlSEikslE8BUfDhCKc6KCzEk1lsABZRAArbjqdZQZikACeg0wAG1Pq1aJAXIwLC1INx4AAvWBUABMBCgKGZnHFVyU8GUSUgAM+dC0qmI8DU3RqGp5zMFpXgsGYxDYzyKUFIiEQjAc5AVUF0sB1pDUNsgsvlbAAjvRPShJOR4PY1Ya-t97tRDbzGgKrox6KrY5q1Nrdfq4xsoMyJSazRarTtvXaHU7NGwXLt1XnIMaapBTebLVdrfhbfbHY5q1ddLrvb6XZBA8HQyGI9GSjOwNGALqArUqHV6wUvagt4vtqCzZjydzDuWj9KDtSkVhfetQVRqU054A-L5AA Open this visualization in the Vega Editor>

@
let plx op = position Y [ PName \"plx\", PmType Quantitative, PAggregate op ]
    cluster = position X [ PName \"Cluster\", PmType Nominal ]

    median = [ mark Circle [ 'MSize' 20 ]
             , encoding (plx Median [])
             ]
    range = [ mark Rule [ ]
            , encoding
                . plx 'Min'
                . position 'Y2' [ PName "plx", PAggregate 'Max' ]
                $ []
            ]

in toVegaLite
      [ gaiaData
      , encoding (cluster [])
      , layer (map asSpec [ median, range ])
      , width 300
      , height 300
      ]
@

The 'MSize' option is used to change the size of the circles so that they
do not drown out the lines (the size value indicates the area of the mark,
and so for circles the radius is proportional to the square root of this
size value; in practical terms I adjusted the value until I got something
that looked sensible).

Note that the y axis is automatically labelled with the different
operation types that were applied - median, minimum, and maximum -
although there is no indication of what marks map to these operations.

-}
layeredDiversion :: VegaLite
layeredDiversion :: VegaLite
layeredDiversion =
  let plx :: Operation -> BuildEncodingSpecs
plx Operation
op = Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"plx", Measurement -> PositionChannel
PmType Measurement
Quantitative, Operation -> PositionChannel
PAggregate Operation
op ]
      cluster :: BuildEncodingSpecs
cluster = Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Cluster", Measurement -> PositionChannel
PmType Measurement
Nominal ]

      median :: [PropertySpec]
median = [ Mark -> [MarkProperty] -> PropertySpec
mark Mark
Circle [ Double -> MarkProperty
MSize Double
20 ], [EncodingSpec] -> PropertySpec
encoding ( Operation -> BuildEncodingSpecs
plx Operation
Median []) ]
      range :: [PropertySpec]
range = [ Mark -> [MarkProperty] -> PropertySpec
mark Mark
Rule [ ], [EncodingSpec] -> PropertySpec
encoding ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Operation -> BuildEncodingSpecs
plx Operation
Min BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y2 [ Text -> PositionChannel
PName Text
"plx", Operation -> PositionChannel
PAggregate Operation
Max ] ([EncodingSpec] -> PropertySpec) -> [EncodingSpec] -> PropertySpec
forall a b. (a -> b) -> a -> b
$ [] ]

  in [PropertySpec] -> VegaLite
toVegaLite
        [ PropertySpec
gaiaData
        , [EncodingSpec] -> PropertySpec
encoding (BuildEncodingSpecs
cluster [])
        , [VLSpec] -> PropertySpec
layer (([PropertySpec] -> VLSpec) -> [[PropertySpec]] -> [VLSpec]
forall a b. (a -> b) -> [a] -> [b]
map [PropertySpec] -> VLSpec
asSpec [ [PropertySpec]
median, [PropertySpec]
range ])
        , Double -> PropertySpec
width Double
300
        , Double -> PropertySpec
height Double
300
        ]


{-|

In this example (adapted from an example provided by Jo Wood)
I display the same data as in 'starCount', but
as two layers: the first is a histogram (using the 'Bar' mark),
and the second displays the count value as a label with the
'Text' mark.

<<images/vl/layeredcount.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1wIxhIBzWdAVwAcAjATz0MgGEAba5aAU1kgBdADRR45Sr3Lw+LAmXS0WkAMY1E0SKMjxkytdQ2QAvoOPERxSAAteAS3LXN+AEwBWAAzCraxKgcsZABudrwA7oGQPFQA1rwsiNQcHMbG3hCQACYy8IHEGdSwHMpO0LTIuAD0lQhhAHTkdtDW1PTc-L58GnVqWJUAIjTkAEKFcZXWQVLwlVi6fLATU9KV2dAz0nbwALTwOwBsAIz7u4ce2+v0HLyH8HWI6Nu28Jn8ddDIQVr5UBjYMnkSCRILR4LBkPF8KAgUDIABxObkZSJLD0fjfGHAgBKAEEAPoASTYWIAysjqKj0elMVBaBwAB7kykCamYyD9ACihOJZPwkBRaJZP1hvDxdMZfIF6OFYHMQLlsupkDCdkyzRYAGYPF4rAASZAqWxzErQMoVarLO6NZqtOp2dCVA1GmaW7YcJq8SpBAAsdQAVsh0IgMVBoE1rpE+PTnFAAHIUwVgdCoMA8MHIMC0fhgFRcHhU35B6AkuwAL0hYEOAA40lYOPBGOj8EQgdCYZA5rAYsp6GCQyLEGpMnZEEioTKMmoOJhATSMv5eBxMspONwFv2aVFMDGyLxB+hh6PlMxtJhXgI+a8DXvD0ja3OMtBGFnkegsCP4MVWZvruQb5FQUyW8WDObRonQOI2HQacLygSgGxMCdFSQyAJVIJD51CJcVzzddvzZQNYB3SA9yHEcxygE8oDPJsoCvFQb3Ikx8PbJ8X0lN8Py-DCxHpOw9HwRJkiQ+9N2YKFIAXbC+QMIxtDYitIAAR2oJAw3WMMpi0Xj+ISJIUhlBUIFEiA21hTtuwkzJxLAbZ9nk59FKjTQTOBUiDyY8c5xgXho0iKTlxk9RNAc9ioBUtSmhkOwtNc2E0LMzcApwtcqR4rciMidzgL5KjIBo2Csl4a9EGAuLWMc1930QT8NzZeA+IEsAhI4ESWIyGyyGSoLDBC0NKr5CKNCizT4m0BrdME-SjJIGaFUEEBjCAA Open this visualization in the Vega Editor>


@
let trans = transform
            . aggregate [ opAs Count \"\" \"count\" ]
                        [ \"Cluster\" ]

    chanSort = [ ByChannel ChY, Descending ]

    baseEnc = encoding
              . position X [ PName \"Cluster\"
                           , PmType Nominal
                           , PSort chanSort
                           , PAxis []
                           ]
              . position Y [ PName \"count\"
                           , PmType Quantitative
                           , PAxis []
                           ]

    barEnc = baseEnc
             . color [ MName \"Cluster\"
                     , MmType Nominal
                     , MLegend [ 'LStrokeColor' \"gray\"
                               , 'LPadding' 10
                               ]
                     , MSort chanSort
                     ]

    labelEnc = baseEnc
               . 'text' [ TName \"count\", TmType Quantitative ]

    barSpec = asSpec [ barEnc [], mark Bar [] ]
    labelSpec = asSpec [ labelEnc [], mark 'Text' [ 'MdY' (-6) ] ]

    cfg = configure
          . configuration (ViewStyle [ViewNoStroke])

in toVegaLite [ width 300
              , height 250
              , cfg []
              , gaiaData
              , title \"Number of stars per cluster\" [ TFontSize 18 ]
              , trans []
              , layer [ barSpec, labelSpec ]
              ]
@

Both axes have been dropped from this visualization since
the cluster name can be found from the legend and the
count is included in the plot. The same sort order is
used for the X axis and the color mapping, so that its
easy to compare (the first item in the legend is the
cluster with the most counts). Note that this changes the
color mapping (cluster to color) compared to previous
plots such as 'parallaxBreakdown'.

-}

layeredCount :: VegaLite
layeredCount :: VegaLite
layeredCount =
  let trans :: [TransformSpec] -> PropertySpec
trans = [TransformSpec] -> PropertySpec
transform
              ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [VLSpec] -> [Text] -> [TransformSpec] -> [TransformSpec]
aggregate [ Operation -> Text -> Text -> VLSpec
opAs Operation
Count Text
"" Text
"count" ]
                          [ Text
"Cluster" ]

      chanSort :: [SortProperty]
chanSort = [ Channel -> SortProperty
ByChannel Channel
ChY, SortProperty
Descending ]

      baseEnc :: [EncodingSpec] -> PropertySpec
baseEnc = [EncodingSpec] -> PropertySpec
encoding
                ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Cluster"
                             , Measurement -> PositionChannel
PmType Measurement
Nominal
                             , [SortProperty] -> PositionChannel
PSort [SortProperty]
chanSort
                             , [AxisProperty] -> PositionChannel
PAxis []
                             ]
                BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"count"
                             , Measurement -> PositionChannel
PmType Measurement
Quantitative
                             , [AxisProperty] -> PositionChannel
PAxis []
                             ]

      barEnc :: [EncodingSpec] -> PropertySpec
barEnc = [EncodingSpec] -> PropertySpec
baseEnc
               ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster"
                       , Measurement -> MarkChannel
MmType Measurement
Nominal
                       , [LegendProperty] -> MarkChannel
MLegend [ Text -> LegendProperty
LStrokeColor Text
"gray"
                                 , Double -> LegendProperty
LPadding Double
10
                                 ]
                       , [SortProperty] -> MarkChannel
MSort [SortProperty]
chanSort
                       ]

      labelEnc :: [EncodingSpec] -> PropertySpec
labelEnc = [EncodingSpec] -> PropertySpec
baseEnc
                 ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TextChannel] -> BuildEncodingSpecs
text [ Text -> TextChannel
TName Text
"count", Measurement -> TextChannel
TmType Measurement
Quantitative ]

      barSpec :: VLSpec
barSpec = [PropertySpec] -> VLSpec
asSpec [ [EncodingSpec] -> PropertySpec
barEnc [], Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar [] ]
      labelSpec :: VLSpec
labelSpec = [PropertySpec] -> VLSpec
asSpec [ [EncodingSpec] -> PropertySpec
labelEnc [], Mark -> [MarkProperty] -> PropertySpec
mark Mark
Text [ Double -> MarkProperty
MdY (-Double
6) ] ]

      cfg :: [ConfigureSpec] -> PropertySpec
cfg = [ConfigureSpec] -> PropertySpec
configure
            ([ConfigureSpec] -> PropertySpec)
-> BuildConfigureSpecs -> [ConfigureSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConfigurationProperty -> BuildConfigureSpecs
configuration ([ViewConfig] -> ConfigurationProperty
ViewStyle [ViewConfig
ViewNoStroke])

  in [PropertySpec] -> VegaLite
toVegaLite [ Double -> PropertySpec
width Double
300
                , Double -> PropertySpec
height Double
250
                , [ConfigureSpec] -> PropertySpec
cfg []
                , PropertySpec
gaiaData
                , Text -> [TitleConfig] -> PropertySpec
title Text
"Number of stars per cluster" [ Double -> TitleConfig
TFontSize Double
18 ]
                , [TransformSpec] -> PropertySpec
trans []
                , [VLSpec] -> PropertySpec
layer [ VLSpec
barSpec, VLSpec
labelSpec ]
                ]


-- TODO: can I plot the mean / median of the parallax for each cluster

{-|

As promised earlier (in 'skyPlot'), now that we have layers, we can
add graticules to a projection. In this case I create two graticule layers,
the \"base\" layer (@grats@), which creates the grey lines that cover
the map - using a spacing of 60 degrees (4 hours) for longitude and
15 degrees for latitude - and then an extra layer (@grats0@), which shows blue lines
at longitude seprations of 180 degrees
and latitude spacings of 90 degrees. In this case the central horizontal and
vertical lines represent 0 degrees, and the one at the left shows
-180 degrees. There are no latitude lines for -90 or +90 since the
default is to stop at ±85 degrees (see 'GrExtent' for a way to
change this).

I added the second graticule layer to see if I could get by without
labels for the grid lines, but decided this did not work out too well,
so ended with two layers, one each for the Right Ascension and
Declination values, using 'dataFromColumns' to manually create the
label positions and label content to display with the 'Text' mark.

<<images/vl/skyplotwithgraticules.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAWCmCWBzaAXSAuMAWADDgNOFAO7wAmK0GYAzAKwFGQAkAzgMZwC2AhtTChQAHVugD0YgG6xEPAHSJ4lAK4AjOfAD2Yjtx5SZPALQAbJbClY5AK1aaAdpEIRIQgE6brsdii2PMwJAoAJ5CsPxcsG7sPCiabpAAvs5QJjzBUdQA2kQQoBAFULxuANbU+YUFkKwoHiWwAOrklNQ4cnQplVA1deGYkIhusMFOuZXVtZr1APJCPOxKI5htHWOFQaF9UIiwmqzQPGGQa2DJJ5BksXwBA26x8OzKJluBNbBC2QBsBGAAjHQAXUSiTGZ0KgWKZX6Oz2ByO+CglxQ1zAgUG90ez3KE3e2V+AA4fgBOHBA4GdVHnWo8eysABm8S42ROeRZVR4rH4JgcihQyjI4QpXUgMRMmNiWwusWUXDkACUAIIAfQAkgBhOUAZTAAD4-oSwAB+MBImXy5XqrVgIy0b5gTCm2WK1UazXHLoQEFdAFCoo8Ur8BbRLG+qXI8psyDKNwmfioYSiCR3YgKJTQNTKVhRdgOFCwewoOQ5rhiAAimmUiAAQtH6mJoNJZGJeG83PXG-okfpZPBjDxjJ9fp8jDxfjgjMjVM9fvJ7JojHAeAK3HIUKxJKMPVAGW5eGgAmyXHM3FmI1v1gBxXiIfj2GWqTK+4XOy1u-p3rgPhJP8ZCEwAD1ve9H0PKpSwAURdLUgM-EDzyqWAlT-QD32AhJQK9D1MMKMFxnzHMyHgewbwPLcRU0bkElI89IDpeBYBMMh+GQzcaI4HgsRuXRYEifhJHgNxyHgTkEQ2I5+m5G9cLIkJxKgABHZQaV8ZFfGkViyOeHZ7CYm4VM4qBjw4tJAOwyppPGNIVP5F5aPoxj+AgqC3VE2TJUU5SlHudSLPWOIKN8D4bjohjdKgNUTEzPNvygNzb00LgiI4pIf0gbliKUGzsRChyJJ5TKBScWLNn4DyCy8tTwjMz1QQpCp1khbEyCWMAOigDikH8KB2HzaKirEyU83-NBRNUDkGKIwbNA+XzESuM8PUgSQOOUWBOUwHJ4MCEwp34IcqFElDrV+AAmH5IBanBZvBNLdv6M6DqgI6jG+UTLuugptruqAsEeyAjteqB3p-PJbtjfp8T+o7TvO962QBE5ZsgfDNEI4iFuFIb91ROzQq5XbXJK98EqS2MPrS+4+UK4L7LCi7+ri-oypU7yqtS9LeSymm8f6QDCbkyBmYq+AfMR2qxnqqpGuo4UyGe1YyI6xAusgQTkBGyNGdi2Bho08YxqzMx7ElRKyDILFEdDLsMfGZbIrW5ktrB-gjCwOh+qO2HqFdugPtBnbwZ+93DtaN7qDd6qCgRrokZRtGSMpGSdexwIcrpgOGaJqA50S+xkvJqyCtstP+BGfn3KU8rVJFtnIw5ovstp-g+eKgWher0WY9BIgfSYHN7DohPgBBRIgA Open this visualization in the Vega Editor>

@
let trans = transform
                . calculateAs
                  "datum.RA_ICRS > 180 ? datum.RA_ICRS - 360 : datum.RA_ICRS"
                  "longitude"

    axOpts field = [ PName field, PmType Quantitative ]

    enc = encoding
            . position Longitude (axOpts "longitude")
            . position Latitude (axOpts \"DE_ICRS\")
            . color [ MName \"plx\"
                    , MmType Quantitative
                    , MScale [ SType ScLog
                             , SScheme \"viridis\" []
                             ]
                    , MLegend [ LTitle "parallax" ]
                    ]
            . tooltip [ TName \"Cluster\", TmType Nominal ]

    stars = asSpec [ gaiaData, trans [], enc [], mark Circle [] ]
    grats = asSpec [ 'graticule' [ 'GrStep' (60, 15) ]
                   , mark 'Geoshape' [ MStroke "grey"
                                   , 'MStrokeOpacity' 0.5
                                   , 'MStrokeWidth' 0.5
                                   ]
                   ]
    grats0 = asSpec [ graticule [ GrStep (180, 90)
                                ]
                    , mark Geoshape [ ]
                    ]

    raData = dataFromColumns []
                 . dataColumn "x" (Numbers [ -120, -60, 60, 120 ])
                 . dataColumn "y" (Numbers [ 0, 0, 0, 0 ])
                 . dataColumn "lbl" (Strings [ "16h", "20h", "4h", "8h" ])

    decData = dataFromColumns []
                 . dataColumn "x" (Numbers [ 0, 0 ])
                 . dataColumn "y" (Numbers [ -45, 45 ])
                 . dataColumn "lbl" (Strings [ "-45", "45" ])

    encLabels = encoding
                . position Longitude (axOpts "x")
                . position Latitude (axOpts "y")
                . text [ TName "lbl", TmType Nominal ]

    raLabels = asSpec [ raData []
                      , encLabels []
                      , mark Text [ 'MAlign' 'AlignCenter'
                                  , 'MBaseline' 'AlignTop'
                                  , MdY 5
                                  ]
                      ]
    decLabels = asSpec [ decData []
                       , encLabels []
                       , mark Text [ MAlign AlignRight
                                   , MBaseline 'AlignMiddle'
                                   , 'MdX' (-5)
                                   ]
                      ]

in toVegaLite [ width 400
              , height 350
              , projection [ PrType Mercator ]
              , layer [ grats, grats0, stars, raLabels, decLabels ]
              ]
@

The layers are drawn in the order they are specified, which is why the
grid lines are drawn under the data (and labels).

You can see the distortion in this particular projection (the
<https://en.wikipedia.org/wiki/Mercator_projection Mercator projection>),
as the spacing between the latitude lines increases as you move towards the
bottom and top of the plot. There are a number of other projections you
can chose from, such as the 'Orthographic' projection I use in
'concatenatedSkyPlot'.

-}

skyPlotWithGraticules :: VegaLite
skyPlotWithGraticules :: VegaLite
skyPlotWithGraticules =
  let trans :: [TransformSpec] -> PropertySpec
trans = [TransformSpec] -> PropertySpec
transform
                ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> [TransformSpec] -> [TransformSpec]
calculateAs
                  Text
"datum.RA_ICRS > 180 ? datum.RA_ICRS - 360 : datum.RA_ICRS"
                  Text
"longitude"

      axOpts :: Text -> [PositionChannel]
axOpts Text
field = [ Text -> PositionChannel
PName Text
field, Measurement -> PositionChannel
PmType Measurement
Quantitative ]

      enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
              ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Longitude (Text -> [PositionChannel]
axOpts Text
"longitude")
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Latitude (Text -> [PositionChannel]
axOpts Text
"DE_ICRS")
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"plx"
                      , Measurement -> MarkChannel
MmType Measurement
Quantitative
                      , [ScaleProperty] -> MarkChannel
MScale [ Scale -> ScaleProperty
SType Scale
ScLog
                               , Text -> [Double] -> ScaleProperty
SScheme Text
"viridis" []
                               ]
                      , [LegendProperty] -> MarkChannel
MLegend [ Text -> LegendProperty
LTitle Text
"parallax" ]
                      ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TextChannel] -> BuildEncodingSpecs
tooltip [ Text -> TextChannel
TName Text
"Cluster", Measurement -> TextChannel
TmType Measurement
Nominal ]
              -- note: opacity doesn't really help here

      stars :: VLSpec
stars = [PropertySpec] -> VLSpec
asSpec [ PropertySpec
gaiaData, [TransformSpec] -> PropertySpec
trans [], [EncodingSpec] -> PropertySpec
enc [], Mark -> [MarkProperty] -> PropertySpec
mark Mark
Circle [] ]
      grats :: VLSpec
grats = [PropertySpec] -> VLSpec
asSpec [ [GraticuleProperty] -> PropertySpec
graticule [ (Double, Double) -> GraticuleProperty
GrStep (Double
60, Double
15) ]
                     , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Geoshape [ Text -> MarkProperty
MStroke Text
"grey"
                                     , Double -> MarkProperty
MStrokeOpacity Double
0.5
                                     , Double -> MarkProperty
MStrokeWidth Double
0.5
                                     ]
                     ]
      grats0 :: VLSpec
grats0 = [PropertySpec] -> VLSpec
asSpec [ [GraticuleProperty] -> PropertySpec
graticule [ (Double, Double) -> GraticuleProperty
GrStep (Double
180, Double
90)
                                  ]
                      , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Geoshape [ ]
                      ]

      -- hmmm, tried to use "ᴴ" / \u1D34 but this does not get converted
      -- properly (is there a UTF-8/16 issue going on, or am I
      -- just failing to understand modern text encoding yet again?)
      --
      -- this also affects ° / \u00b0 so not sure what's going on
      raData :: [DataColumn] -> PropertySpec
raData = [Format] -> [DataColumn] -> PropertySpec
dataFromColumns []
                   ([DataColumn] -> PropertySpec)
-> ([DataColumn] -> [DataColumn]) -> [DataColumn] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> DataValues -> [DataColumn] -> [DataColumn]
dataColumn Text
"x"
                     ([Double] -> DataValues
Numbers [ -Double
120, -Double
60, Double
60, Double
120 ])
                   ([DataColumn] -> [DataColumn])
-> ([DataColumn] -> [DataColumn]) -> [DataColumn] -> [DataColumn]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> DataValues -> [DataColumn] -> [DataColumn]
dataColumn Text
"y"
                     ([Double] -> DataValues
Numbers [ Double
0, Double
0, Double
0, Double
0 ])
                   ([DataColumn] -> [DataColumn])
-> ([DataColumn] -> [DataColumn]) -> [DataColumn] -> [DataColumn]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> DataValues -> [DataColumn] -> [DataColumn]
dataColumn Text
"lbl"
                     ([Text] -> DataValues
Strings [ Text
"16h", Text
"20h", Text
"4h", Text
"8h" ])

      decData :: [DataColumn] -> PropertySpec
decData = [Format] -> [DataColumn] -> PropertySpec
dataFromColumns []
                   ([DataColumn] -> PropertySpec)
-> ([DataColumn] -> [DataColumn]) -> [DataColumn] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> DataValues -> [DataColumn] -> [DataColumn]
dataColumn Text
"x"
                     ([Double] -> DataValues
Numbers [ Double
0, Double
0 ])
                   ([DataColumn] -> [DataColumn])
-> ([DataColumn] -> [DataColumn]) -> [DataColumn] -> [DataColumn]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> DataValues -> [DataColumn] -> [DataColumn]
dataColumn Text
"y"
                     ([Double] -> DataValues
Numbers [ -Double
45, Double
45 ])
                   ([DataColumn] -> [DataColumn])
-> ([DataColumn] -> [DataColumn]) -> [DataColumn] -> [DataColumn]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> DataValues -> [DataColumn] -> [DataColumn]
dataColumn Text
"lbl"
                     ([Text] -> DataValues
Strings [ Text
"-45", Text
"45" ])

      encLabels :: [EncodingSpec] -> PropertySpec
encLabels = [EncodingSpec] -> PropertySpec
encoding
                  ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Longitude (Text -> [PositionChannel]
axOpts Text
"x")
                  BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Latitude (Text -> [PositionChannel]
axOpts Text
"y")
                  BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TextChannel] -> BuildEncodingSpecs
text [ Text -> TextChannel
TName Text
"lbl", Measurement -> TextChannel
TmType Measurement
Nominal ]

      raLabels :: VLSpec
raLabels = [PropertySpec] -> VLSpec
asSpec [ [DataColumn] -> PropertySpec
raData []
                        , [EncodingSpec] -> PropertySpec
encLabels []
                        , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Text [ HAlign -> MarkProperty
MAlign HAlign
AlignCenter
                                    , VAlign -> MarkProperty
MBaseline VAlign
AlignTop
                                    , Double -> MarkProperty
MdY Double
5
                                    ]
                        ]
      decLabels :: VLSpec
decLabels = [PropertySpec] -> VLSpec
asSpec [ [DataColumn] -> PropertySpec
decData []
                         , [EncodingSpec] -> PropertySpec
encLabels []
                         , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Text [ HAlign -> MarkProperty
MAlign HAlign
AlignRight
                                     , VAlign -> MarkProperty
MBaseline VAlign
AlignMiddle
                                     , Double -> MarkProperty
MdX (-Double
5)
                                     ]
                        ]

      -- don't know how to change the center

  in [PropertySpec] -> VegaLite
toVegaLite [ Double -> PropertySpec
width Double
350
                , Double -> PropertySpec
height Double
400
                , [ProjectionProperty] -> PropertySpec
projection [ Projection -> ProjectionProperty
PrType Projection
Mercator ]
                , [VLSpec] -> PropertySpec
layer [ VLSpec
grats, VLSpec
grats0, VLSpec
stars, VLSpec
raLabels, VLSpec
decLabels ]
                ]



{-|

Instead of layering one view on top of another (superposition), we can
place them side by side in a row or column (juxtaposition). In
Vega-Lite this is referred to as /concatenation/:

<<images/vl/concatenatedplot.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAmCGAutIC4yghSBXATgGxSgAt54AHAZ2QHpqdYB3AOgHMBLeIrAIywoFMcAYwD2AO3j8JTUQFtqAERFYWAIVwBrftSIA3fi1jVZsCpJw79h6nETVDbWAFpYzgGwBGNy48AGJ4jcePwesExiIk5E-LDQgkzwFLqQADTgGJAAZiI4JvCE6BiYZLA4AgXpRZgA4iYshJBiWLLcgqmVVZAASgCCAPoAkgDCXQDKDU0tbWlVGWR4AB4Tza047bOYCgCigyPjqI0r0x1FkPx980sHk6uQJ2AAvh1PEA8zULqiYkIIhADaHUKpxMOA0DW4pXWnSkomgbDE9VQQM6VzQWTY-Dw0AaQzwfHMqSg8AAnmR+BMRLJ4bACG97phiQV0ZjsQdLoTILAWCwcAYEOSDrJ+HDYGIOSSyQ0AI5YUXwDgINj6SAvKqq17vCDIzAgsEHCFrTUZGEiOEIiobKCo4DMrE4vFmaZE0kCqARKliGkqo2nRlI22sqC1Lkcrk8vmSBpCkVilLOyUHGVyhXy5XqjDp1UAXXekAAJBQhNETA0SOQqLQrGF2JweEw2CJqIXi0Yq048BxtLoACxMABWFHEUMgX0ybERaCeDyAA Open this visualization in the Vega Editor>

@
let enc field = encoding
                 . position X [ PName \"Cluster\", PmType Nominal ]
                 . position Y [ PName field, PmType Quantitative, PAggregate Median ]

    parallaxes = [ mark Bar [], enc \"plx\" [] ]
    magnitudes = [ mark Bar [], enc \"Gmag\" [] ]

    specs = map asSpec [ parallaxes, magnitudes ]

in toVegaLite
      [ gaiaData
      , 'vConcat' specs
      ]
@

The 'hConcat' function would align the two plots horizontally,
rather than vertically (and is used in 'concatenatedSkyPlot').

Note that as the axes are identical apart from the field for the y axis,
the encoding has been moved into a function to enforce this constraint
(this ensures the x axis is the same, which makes it easier to visually
compare the two plots). However, there is no requirement that the
two plots be "compatible" (they could use different data sources).

-}


concatenatedPlot :: VegaLite
concatenatedPlot :: VegaLite
concatenatedPlot =
  let enc :: Text -> [EncodingSpec] -> PropertySpec
enc Text
field = [EncodingSpec] -> PropertySpec
encoding
                   ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Cluster", Measurement -> PositionChannel
PmType Measurement
Nominal ]
                   BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
field, Measurement -> PositionChannel
PmType Measurement
Quantitative, Operation -> PositionChannel
PAggregate Operation
Median ]

      parallaxes :: [PropertySpec]
parallaxes = [ Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar [], Text -> [EncodingSpec] -> PropertySpec
enc Text
"plx" [] ]
      magnitudes :: [PropertySpec]
magnitudes = [ Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar [], Text -> [EncodingSpec] -> PropertySpec
enc Text
"Gmag" [] ]

      specs :: [VLSpec]
specs = ([PropertySpec] -> VLSpec) -> [[PropertySpec]] -> [VLSpec]
forall a b. (a -> b) -> [a] -> [b]
map [PropertySpec] -> VLSpec
asSpec [ [PropertySpec]
parallaxes, [PropertySpec]
magnitudes ]

  in [PropertySpec] -> VegaLite
toVegaLite
        [ PropertySpec
gaiaData
        , [VLSpec] -> PropertySpec
vConcat [VLSpec]
specs
        ]


{-|

The alignment of the plots can be adjusted with 'spacing', which we
use here to remove the vertical gap between the two plots (the
example is written so that we can see the only difference between
the two plot specifications is the addition of @'PAxis' []@ to the
parallax plot).

<<images/vl/concatenatedplot2.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAmCGAutIC4yghSBXATgGxSgAt54AHAZ2QHpqdYB3AOgHMBLeIrAIywoFMcAYwD2AO3j8JTUQFtqAERFYWAIVwBrftSIA3fi1jVZsCpJw79h6nETVDbWAFpYzgGwBGNy48AGJ4jcePwesExiIk5E-LDQgkzwFLqQADTgGJAAZiI4JvCE6BiYZLA4AgXpRZgA4iYshJBiWLLcgqmVVZAASgCCAPoAkgDCXQDKDU0tbWlVGWR4AB4Tza047bOYCgCigyPjqI0r0x1FkPx980sHk6uQJ2AAvh1PEA8zULqiYkIIhADaHUKpxMOA0DW4pXWnSkomgbDE9VQQM6VzQWTY-Dw0AaQzwfHMqSg8AAnmR+BMRLJ4bACCkoLAFmwKIQmng8G97phiQV0ZjsQdLoTILAWCwcAYEOSDrJ+HDYGIhSSyQ0AI5YeXwDgINj6SAvKr617vCDIzAgsEHCFrY0ZGEiOEIiobKCo4C8rE4vFmaZE0lSqARKliGl6m2nblI938qC1EVCkViiWSBoyuUKumQJX+yBqjVazW6w0YIv6gC670gABIKEJoiYGiRyFRaFYwuxODwmGwRNQa3WjK2nHgONpdAAWJgAKwo4ihkAoJSE8MRYF8Fa+mTYK+ATweQA Open this visualization in the Vega Editor>

@
let enc field flag = encoding
                       . position X ([ PName \"Cluster\", PmType Nominal ] ++
                                     if flag then [ PAxis [] ] else [])
                       . position Y [ PName field, PmType Quantitative, PAggregate Median ]

    parallaxes = [ mark Bar [], enc \"plx\" True [] ]
    magnitudes = [ mark Bar [], enc \"Gmag\" False [] ]

    specs = map asSpec [ parallaxes, magnitudes ]

in toVegaLite
      [ gaiaData
      , 'spacing' 0
      , vConcat specs
      ]
@

Even though we set 'spacing' to @0@ there is still a small gap between
the plots: this can be removed by using @'bounds' 'Flush'@, but we'll
leave using that until the grand finale.

-}

concatenatedPlot2 :: VegaLite
concatenatedPlot2 :: VegaLite
concatenatedPlot2 =
  let enc :: Text -> Bool -> [EncodingSpec] -> PropertySpec
enc Text
field Bool
flag = [EncodingSpec] -> PropertySpec
encoding
                         ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X ([ Text -> PositionChannel
PName Text
"Cluster", Measurement -> PositionChannel
PmType Measurement
Nominal ] [PositionChannel] -> [PositionChannel] -> [PositionChannel]
forall a. [a] -> [a] -> [a]
++
                                       if Bool
flag then [ [AxisProperty] -> PositionChannel
PAxis [] ] else [])
                         BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
field, Measurement -> PositionChannel
PmType Measurement
Quantitative, Operation -> PositionChannel
PAggregate Operation
Median ]

      parallaxes :: [PropertySpec]
parallaxes = [ Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar [], Text -> Bool -> [EncodingSpec] -> PropertySpec
enc Text
"plx" Bool
True [] ]
      magnitudes :: [PropertySpec]
magnitudes = [ Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar [], Text -> Bool -> [EncodingSpec] -> PropertySpec
enc Text
"Gmag" Bool
False [] ]

      specs :: [VLSpec]
specs = ([PropertySpec] -> VLSpec) -> [[PropertySpec]] -> [VLSpec]
forall a b. (a -> b) -> [a] -> [b]
map [PropertySpec] -> VLSpec
asSpec [ [PropertySpec]
parallaxes, [PropertySpec]
magnitudes ]

  in [PropertySpec] -> VegaLite
toVegaLite
        [ PropertySpec
gaiaData
        , Double -> PropertySpec
spacing Double
0
        , [VLSpec] -> PropertySpec
vConcat [VLSpec]
specs
        ]


{-|

In 'skyPlotWithGraticules' I used the 'Mercator' projection to display
the stars on the sky, but promised I would also show you data using the
<https://en.wikipedia.org/wiki/Orthographic_projection_in_cartography Orthographic projection>.

The main specification (that is, the argument of 'toVegaLite') starts
with a change to the plot defaults, using 'configure' to ensure
that no border is drawn around the plot (note that in 'combinedPlot'
I do the same thing, but by setting the stroke color to
@Just \"transparent\"@ rather than @Nothing@). The default data
stream is set up, to ensure we have \"longitude\" and
\"DE_ICRS" values to display. It then has three
versions of the same visualization, varying only on rotation angle and
label, stacked horizontally with 'hConcat'.

Each plot - created with the @rSpec@ helper function - defines
a plot size, uses the 'Orthographic' projection with the
given rotation (the @lambda@ term of 'PrRotate') to change the
center of the display, and then the plot itself is formed from
four layers:

1. 'sphere' is used to indicate the area of the plot covered by the sky
   (filled with a blue variant);

2. graticules are drawn at every 30 degrees (longitude, so 2 hours
   in Right Ascension) and 15 degrees (latitude);

3. the stars are drawn using color to encode the parallax of the
   star and the symbol shape the cluster membership (although the density
   of points is such that it can be hard to make the shapes out);

4. and a label is added at the center of the plot to indicate the
   Right Ascension (the label could be determined automatically from
   the rotation angle, but it was easier to just specify it directly).

Since the data values have two different encodings - 'color' and 'shape' -
there are two legends added. I place them in different locations using
'LOrient': the parallax goes to the right of the plots (which is the
default) and the symbol shapes to the bottom. Both use larger-than-default
font sizes for the text (title and label).

<<images/vl/concatenatedskyplot.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1wIxQSTJ5k8oAbdRAcwEtoBXAEwFNIAaY8pADG8WkLa140HvkgcpbLADoASgEEA+gEkAwioDKYAHxgAjAA4ADGAD8Yee2XrtewwFowAZgBs1-A8VVTV0DSAEwAF9iAF1+CEgACyEGEWgaInIycigErmZGBLT8T0tLOOzIAHdmDmgEmhKy8PiAB1h0ACsuIWhmBhpgGABPFpkoTDr0RgQWhOYhPih26CkxgjdTACYysB3LaIjywUkhrlh05pIs7IoseFgAawHIVGZaWhpKWnmuACNaNg8XhQaAjMaQRhcdDIBLwUaQQ6XQQOeDPZCzM5jOCAqI3CCIvGkJG3e5PfDXQlQZBwdAPLgAdRqdRoliUAFYjpTINT2nTPtMuEM+MTBDzaVwAPIteBCVhC-Csjkiiig+GySHQ2Hw5UErko57TKTzCRjQbUrgtACyzEQmHSJWBpjZB1xeN1NwpN0gd0enxa6BtaU5Xq4iGSHBtjAGyviyXo53JMYory4tA4ftoAA9hZSKsgRLRTdyhLksOCAG7MWA1ZjUYHDNV0KYI4Nc1XggCObCQvRWvXLQKT8ULkMQ6cTua9mGYoaKS3yhRzk+O8F+qYAYgxoPpmAAvMamAAsrdzMFYhb991EkmzJ7b564m8Q273B+8Q8iSfdXMkvc4RZTNNPgAEQAURcUJ63bT4ux7VgjQHFsk25LVTQ-F4ZyA2QdABc1zjvL1oNkW0sBtUQl2XSAR1DcciWXQRp1nT5fnQaBoHQHACLxKjVw3Lcd33Ggjy4wiH0+IRcOkfCP3iXtCyfF9BPwUx33oz9l2-QkYHQdBaF6FpnkA2jIBwtg8MWBtwRIsiPk07j6CYVh-0MzDjIclh2G4CyiKgWDn3g-seFdQlguyOyIE9CofTJOjJzkeUwCVSjRHyRBxNnM4KLbMFPmkTMg2Q34qFTG1wXYgydS4uQpFRclIHLURAWofACEGWh-k+cx6nrbNlO2esEssCIDiq0Nw0jaNKLyudBiMz52o+KCcuIjibKQyjfycry6rm2QhSWxtID83sEKCkSm0czyANcz5bxBZbfO7fy+2YRDQpud6SE+6Jwm-SLEjyAo50aVsqiZepilKUG2k6bpen6OqfMgCYEimGY5gWetllWdI9mBfY7J404E0IYlIsEaKXPeT4UqEP4ASBe7Do1GE4TO5VqpWNEMVgLFYBxYlwtirTKYnOKxTpRlagh3Z2XO7kaT5dVef25CJclaVZVBFk5eQpGWdQsJcyF+J9Tqw1enEC86vNK0bTtFqHTMZ0Ik+-E73JklfVkf1AyykMw3QCMmEmuK4wd4W4t2qAWizf2uXzUQi3zUsKyrGs6yZ8F6CjE2KiR46Atewc1KorhR1oz2tMY59PmrIH49PSQ11oBSBIPY8ZJBMSfavd54FvLuz2geT+NfITVPot2wvlzarpc1NjLAiD9G8h6jqek7AvW8XDbFyjo5MyTMvO2T1+sxByNPuhy5o0PS5rudIBYtiOMbn9eNbselLMTu1NknuUAJJmSku-LSclHzfzfB+ae5A86yR0npZgBkdo3WwsffCWdPgX3IvAi6HlnKoMXvNBgBCvIHU7JvIub0dSCw9hzUWkctIcASklOKKVGBpVkHTZ8J89br2mmAigRVkAlUQGVdAFVjZVTNqQeqjUuDNUIG1DqsgtjdSgL1XYA0WTDTzpAMaQcJr7zbFwfKC8sJ0A6hQ7Bq1L62VnkaeeRDLGQH2lg2QhcXpvVnqQra11iGyDupZGCVDvFBVoSFS4P04Gcn+rkBcwMoaXDBtLBoySvQwy6D0PoXC5FIxRmjOEGMLLY2kOkLYeNdgjRSScTKLUyYMNJFTD4shab00BGvZmUJWbwn0bIs0PM+YCxCvQ08jCq4UHVlLZkCpdaUXVvyFWQj4jqylDKOUOs2Gnn1j0w2lUOYDIhAgS2Jo0TSDtraEmBAnZOhdHQxp4zmk+wDLXUagdg5RhMVpcOJNJmCEPrHQepdE7WzkSnLgZZPiVmrBGTOITZA5x3lNdeXjTorIoNRMc996LI2rExWQ9dFzX2HJ-Nu49lJ-3-sPMFkBpQIH7sC6lEDyU-xUjAr8ji-zbTkYfZeIRV42M8WE9FeCUJsxxaeQ+plzLXwRVAHBi10JYsrkPR+zFWLsU4kPZufFnztyElS0uEDxIYIxYIFlUCJ4co0vLdiul9IWOMjK0BQqFV2Nwb4y6hDeVoPwf4rplC4LhIRJEt0YyuQTOQiwmg2yuQcLycIDK0kUWHUEfLERYiJFSMpP0mqzwGoMyUa1KiqioCHg0ZALRGx+pQEGno9540Q7fO4oIlxblrEePdaRexyKP7coCa49x8qN7BtFV6shg7jLBILiK7esD1Jumib9GI5RhAMFeF8uRlYuCVHObyMYiAJC0FdmugZ4RIBsFgK0nIbEWjIFwAAekfQgSoSgPIJDYL8MyZxki8OfEoZIWBH3AXQGwRgAAhK9dJH0JAHIweAj67h4Vg-BxDKJH0IeYPANw8AcPeBUrh0wlg3ArH+FwUw8AlC2jcLkeA3BYBKGgMgcsWUXiYDuDNFJ9LRGSooAAcTuFuyAR6sBrhTdxZwArsGKHE+-OlcdiKyb4VpflrgZNiZU9xLgGggUabk5cUKrpdSQAACQQruJ8Qo0B71PsfWh99rBP2-CUH0R9FnENobcN8aQ9nDxKA6MgfoIAIhAA Open this visualization in the Vega Editor (although the link is long, and may not work with Internet Explorer)>

@
let trans = transform
              . calculateAs
                \"datum.RA_ICRS > 180 ? datum.RA_ICRS - 360 : datum.RA_ICRS\"
                \"longitude\"

    axOpts field = [ PName field, PmType Quantitative ]
    legend ttl o = MLegend [ LTitle ttl
                           , 'LOrient' o
                           , 'LTitleFontSize' 16
                           , 'LLabelFontSize' 14
                           ]
    enc = encoding
            . position Longitude (axOpts \"longitude\")
            . position Latitude (axOpts \"DE_ICRS\")
            . color [ MName \"plx\"
                    , MmType Quantitative
                    , MScale [ SType ScLog
                             , SScheme \"viridis\" []
                             ]
                    , legend \"parallax\" 'LORight'
                    ]
            . shape [ MName \"Cluster\"
                    , MmType Nominal
                    , legend \"cluster\" 'LOBottom'
                    ]
            . tooltip [ TName \"Cluster\", TmType Nominal ]

    stars = asSpec [ enc [], mark Point [] ]
    grats = asSpec [ graticule [ GrStepMinor (30, 15) ]
                   , mark Geoshape [ MStroke \"grey\"
                                   , MStrokeOpacity 0.5
                                   , MStrokeWidth 0.5
                                   ]
                   ]

    lblData r h0 =
      let r0 = -r
          lbl = h0 <> \"h\"
      in dataFromColumns []
         . dataColumn \"x\" (Numbers [ r0 ])
         . dataColumn \"y\" (Numbers [ 0 ])
         . dataColumn \"lbl\" (Strings [ lbl ])

    encLabels = encoding
                . position Longitude (axOpts \"x\")
                . position Latitude (axOpts \"y\")
                . text [ TName \"lbl\", TmType Nominal ]
    labels r h0 = asSpec [ lblData r h0 []
                         , encLabels []
                         , mark Text [ MAlign AlignCenter
                                     , MBaseline AlignTop
                                     , MdY 5
                                     ]
                         ]

    bg = asSpec [ 'sphere', mark Geoshape [ MFill \"aliceblue\" ] ]

    rSpec r h0 = asSpec [ width 300
                        , height 300
                        , projection [ PrType 'Orthographic'
                                     , 'PrRotate' r 0 0
                                     ]
                        , layer [ bg, grats, stars, labels r h0 ]
                        ]

    s1 = rSpec (-120) \"8\"
    s2 = rSpec 0 \"12\"
    s3 = rSpec 120 \"4\"

    setup = configure . configuration (ViewStyle [ ViewNoStroke ])

in toVegaLite [ setup []
              , gaiaData
              , trans []
              , 'hConcat' [ s1, s2, s3 ] ]
@

-}

concatenatedSkyPlot :: VegaLite
concatenatedSkyPlot :: VegaLite
concatenatedSkyPlot =
  let trans :: [TransformSpec] -> PropertySpec
trans = [TransformSpec] -> PropertySpec
transform
                ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> [TransformSpec] -> [TransformSpec]
calculateAs
                  Text
"datum.RA_ICRS > 180 ? datum.RA_ICRS - 360 : datum.RA_ICRS"
                  Text
"longitude"

      axOpts :: Text -> [PositionChannel]
axOpts Text
field = [ Text -> PositionChannel
PName Text
field, Measurement -> PositionChannel
PmType Measurement
Quantitative ]
      legend :: Text -> LegendOrientation -> MarkChannel
legend Text
ttl LegendOrientation
o = [LegendProperty] -> MarkChannel
MLegend [ Text -> LegendProperty
LTitle Text
ttl
                             , LegendOrientation -> LegendProperty
LOrient LegendOrientation
o
                             , Double -> LegendProperty
LTitleFontSize Double
16
                             , Double -> LegendProperty
LLabelFontSize Double
14
                             ]
      enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
              ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Longitude (Text -> [PositionChannel]
axOpts Text
"longitude")
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Latitude (Text -> [PositionChannel]
axOpts Text
"DE_ICRS")
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"plx"
                      , Measurement -> MarkChannel
MmType Measurement
Quantitative
                      , [ScaleProperty] -> MarkChannel
MScale [ Scale -> ScaleProperty
SType Scale
ScLog
                               , Text -> [Double] -> ScaleProperty
SScheme Text
"viridis" []
                               ]
                      , Text -> LegendOrientation -> MarkChannel
legend Text
"parallax" LegendOrientation
LORight
                      ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
shape [ Text -> MarkChannel
MName Text
"Cluster"
                      , Measurement -> MarkChannel
MmType Measurement
Nominal
                      , Text -> LegendOrientation -> MarkChannel
legend Text
"cluster" LegendOrientation
LOBottom
                      ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TextChannel] -> BuildEncodingSpecs
tooltip [ Text -> TextChannel
TName Text
"Cluster", Measurement -> TextChannel
TmType Measurement
Nominal ]

      stars :: VLSpec
stars = [PropertySpec] -> VLSpec
asSpec [ [EncodingSpec] -> PropertySpec
enc [], Mark -> [MarkProperty] -> PropertySpec
mark Mark
Point [] ]
      grats :: VLSpec
grats = [PropertySpec] -> VLSpec
asSpec [ [GraticuleProperty] -> PropertySpec
graticule [ (Double, Double) -> GraticuleProperty
GrStepMinor (Double
30, Double
15) ]
                     , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Geoshape [ Text -> MarkProperty
MStroke Text
"grey"
                                     , Double -> MarkProperty
MStrokeOpacity Double
0.5
                                     , Double -> MarkProperty
MStrokeWidth Double
0.5
                                     ]
                     ]

      -- make the user work out what the new central location is
      lblData :: Double -> Text -> [DataColumn] -> PropertySpec
lblData Double
r Text
h0 =
        let r0 :: Double
r0 = -Double
r
            lbl :: Text
lbl = Text
h0 Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"h"
        in [Format] -> [DataColumn] -> PropertySpec
dataFromColumns []
           ([DataColumn] -> PropertySpec)
-> ([DataColumn] -> [DataColumn]) -> [DataColumn] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> DataValues -> [DataColumn] -> [DataColumn]
dataColumn Text
"x" ([Double] -> DataValues
Numbers [ Double
r0 ])
           ([DataColumn] -> [DataColumn])
-> ([DataColumn] -> [DataColumn]) -> [DataColumn] -> [DataColumn]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> DataValues -> [DataColumn] -> [DataColumn]
dataColumn Text
"y" ([Double] -> DataValues
Numbers [ Double
0 ])
           ([DataColumn] -> [DataColumn])
-> ([DataColumn] -> [DataColumn]) -> [DataColumn] -> [DataColumn]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> DataValues -> [DataColumn] -> [DataColumn]
dataColumn Text
"lbl" ([Text] -> DataValues
Strings [ Text
lbl ])

      encLabels :: [EncodingSpec] -> PropertySpec
encLabels = [EncodingSpec] -> PropertySpec
encoding
                  ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Longitude (Text -> [PositionChannel]
axOpts Text
"x")
                  BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Latitude (Text -> [PositionChannel]
axOpts Text
"y")
                  BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [TextChannel] -> BuildEncodingSpecs
text [ Text -> TextChannel
TName Text
"lbl", Measurement -> TextChannel
TmType Measurement
Nominal ]
      labels :: Double -> Text -> VLSpec
labels Double
r Text
h0 = [PropertySpec] -> VLSpec
asSpec [ Double -> Text -> [DataColumn] -> PropertySpec
lblData Double
r Text
h0 []
                           , [EncodingSpec] -> PropertySpec
encLabels []
                           , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Text [ HAlign -> MarkProperty
MAlign HAlign
AlignCenter
                                       , VAlign -> MarkProperty
MBaseline VAlign
AlignTop
                                       , Double -> MarkProperty
MdY Double
5
                                       ]
                           ]

      bg :: VLSpec
bg = [PropertySpec] -> VLSpec
asSpec [ PropertySpec
sphere, Mark -> [MarkProperty] -> PropertySpec
mark Mark
Geoshape [ Text -> MarkProperty
MFill Text
"aliceblue" ] ]

      rSpec :: Double -> Text -> VLSpec
rSpec Double
r Text
h0 = [PropertySpec] -> VLSpec
asSpec [ Double -> PropertySpec
width Double
300
                          , Double -> PropertySpec
height Double
300
                          , [ProjectionProperty] -> PropertySpec
projection [ Projection -> ProjectionProperty
PrType Projection
Orthographic
                                       , Double -> Double -> Double -> ProjectionProperty
PrRotate Double
r Double
0 Double
0
                                       ]
                          , [VLSpec] -> PropertySpec
layer [ VLSpec
bg, VLSpec
grats, VLSpec
stars, Double -> Text -> VLSpec
labels Double
r Text
h0 ]
                          ]

      s1 :: VLSpec
s1 = Double -> Text -> VLSpec
rSpec (-Double
120) Text
"8"
      s2 :: VLSpec
s2 = Double -> Text -> VLSpec
rSpec Double
0 Text
"12"
      s3 :: VLSpec
s3 = Double -> Text -> VLSpec
rSpec Double
120 Text
"4"

      setup :: [ConfigureSpec] -> PropertySpec
setup = [ConfigureSpec] -> PropertySpec
configure ([ConfigureSpec] -> PropertySpec)
-> BuildConfigureSpecs -> [ConfigureSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConfigurationProperty -> BuildConfigureSpecs
configuration ([ViewConfig] -> ConfigurationProperty
ViewStyle [ ViewConfig
ViewNoStroke ])

  in [PropertySpec] -> VegaLite
toVegaLite [ [ConfigureSpec] -> PropertySpec
setup []
                , PropertySpec
gaiaData
                , [TransformSpec] -> PropertySpec
trans []
                , [VLSpec] -> PropertySpec
hConcat [ VLSpec
s1, VLSpec
s2, VLSpec
s3 ] ]


-- $intro-repeat
-- Creating the same plot but with a different field is common-enough
-- that Vega-Lite provides the 'repeat' operator.


{-|

The 'concatenatedPlot' example can be extended to view the
distribution of several fields - in this case Right Ascension,
Declination, parallax, and magnitude:

<<images/vl/repeatplot.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEBOCmAOsCGAXSAuMwYHsDuGYA2pAEoCCA+gJIDCpAypADRQAiAojfU65PABsAHiygBxALZIA5pAC6AX2bgoAZ0QBjQqAgRIU6AGtCkAEZJoLFXoAmqJNuu7IAV2gCTACxQp4q9AD0AdBIeAB00gCWKJ4upi6qsNAaOAB2KLDpYSkSAWw4LtIAQm6GsAGeAG6w0kgBUqoZ0BXVtQF2KHW1kUgAtEh9AGwAjIP9wwAMvZ2mArDDSGGpOL2eyDZJYSiqlVa6zgBmONBSaJg6+87wFomOl5eQkjImqS4Spkl7984U3Iwvbw+lmU3yuwgB70+INB7C4dH+mEgr0hwKc90gsEoghEiORQMgaN0CkJxP2SicGNSKRskVSsnOhMgOKwkAOkVgAhsJloAgSTVEkBQAE9EC8cBJaUgPOT0UK7t9WezOdoYAhkGcoNB8JAZQqZNI4LUMiYJLAaUhUl90cLRYiAI4uC0oaKoSLVAn3UlElS6yAAElUGjWUi8Pj8gQCrUWURicTCkRwAUDwbqUd6Ami5UqABYwgArVRpL6QFKpNn0rDEhRAA Open this visualization in the Vega Editor>

@
let enc = encoding
           . position X [ PName \"Cluster\", PmType Nominal ]
           . position Y [ 'PRepeat' 'Row', PmType Quantitative, PAggregate Median ]

    spec = asSpec [ gaiaData
                  , mark Bar []
                  , enc [] ]

    rows = [ \"RA_ICRS\", \"DE_ICRS\", \"plx\", \"Gmag\" ]

in toVegaLite
      [ 'repeat' [ 'RowFields' rows ]
      , 'specification' spec
      ]
@


This more compact specification replaces the data field name
(for example @'PName' \"plx\"@) with a reference to the repeating field
('PRepeat') either as a 'Row' or 'Column' depending on the desired
layout. We then compose the specifications by providing a set of
'RowFields' (or 'ColumnFields') containing a list of the fields to which
we wish to apply the specification (identified with the function
'specification' which should follow the repeat function provided to
toVegaLite).

-}

repeatPlot :: VegaLite
repeatPlot :: VegaLite
repeatPlot =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
             ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Cluster", Measurement -> PositionChannel
PmType Measurement
Nominal ]
             BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Arrangement -> PositionChannel
PRepeat Arrangement
Row, Measurement -> PositionChannel
PmType Measurement
Quantitative, Operation -> PositionChannel
PAggregate Operation
Median ]

      spec :: VLSpec
spec = [PropertySpec] -> VLSpec
asSpec [ PropertySpec
gaiaData
                    , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar []
                    , [EncodingSpec] -> PropertySpec
enc [] ]

      rows :: [Text]
rows = [ Text
"RA_ICRS", Text
"DE_ICRS", Text
"plx", Text
"Gmag" ]

  in [PropertySpec] -> VegaLite
toVegaLite
        [ [RepeatFields] -> PropertySpec
repeat [ [Text] -> RepeatFields
RowFields [Text]
rows ]
        , VLSpec -> PropertySpec
specification VLSpec
spec
        ]


{-|

If we want to plot more than one map from the same table of data
we need to do the lookup in the other order, using lookup to add the
geographic data to the data table. Charting this way requires
specifiying a few things differently than in the previous
choropleth example ('choroplethLookupToGeo'):

  * We're using 'LuAs' in 'lookup', rather than 'LuFields', which lets
    us use all the fields (columns) in the source rather than a specified
    subset.
  * We use a different set of geographic features (state rather than county
    outlines) from @usGeoData@.
  * The plot is defined as a 'specification', but does not directly refer
    to the value being displayed. This is set \"externally\" with the
    call to 'repeat'. Since we have just had an example with
    'RowFields', this time we use 'ColumnFields' to stack the maps
    horizontally.
  * Since the different fields have vastly-different ranges (a maximum of
    roughly 0.01 for \"engineers\" whereas the \"population\" field is
    a billion times larger), the color scaling is set to vary per field
    with 'resolve'.

<<images/vl/choroplethlookupfromgeo.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEBOCmDOB7ANgN1pAXGYl4GMBDZDbXfFRaLKASwDsATWAB1ifYBdIBfHgGnBQ4bQtzKQKyAK4BbejQDakFohbTkY2ogX8o7AOYNYsaPEh7IAC2nRotIvQSQAugKF42+GqAgRInNCE9PAAZlSySkJ+ONExUITm2JAGsIgWcTGQodCIkWSZ8ZCMYoQ+hfFQtsg01pycLPCYAPTNQQDuAHRGnDYARtLwphT0nFydFLLN6AaE07CzzbKJY9DNjIj48OulzYMAtACMAAyynQBWSLoVReHQy+I42bBitqRQ8JxizpacAJ5sWqcNSIS46Xg3CDuSpZADWsD+tVojEgN2hMMgyEQiFh0hYSJRhR4cRcgiyy2gsJ8eECOPekD6mnwVMsn1y8IA8ixCPhaP8aMdOgAmX4A+mpRDwKyEQHo-wlL7lSqQaq1Kz1RotNqELo9fqDYY6MajCZ5eaLGZzZafUzrTbbBVzVTqTScbT0AD6hmMpngHpsdgcwQQE3gqFRMTlUHayN6NAAzMdjmS-CpcudYPg3eCJP9AcliH1fQBVeBlKOQdgURgMAxKopSKj15WhWiwZAoiQiF6PSQoOQKKNZPP0gCO0mCbq+bvQGRhUBIqSY1KobdGQLUFigBiCNa4ABlDHHsInjsTKkOPtL809W+3O9u0luAmLahKwYO4uewOjIAASAgrFgZY1Q1JpWktbo+X6TptGaQDgLmS19mQPlYGmAAWC4rjnKARlbOsyGJHggA Open this visualization in the Vega Editor>

@
let popEngHurrData = dataFromUrl \"https:\/\/raw.githubusercontent.com\/vega\/vega\/master\/docs\/data\/population_engineers_hurricanes.csv\" []

    plotWidth = 300

    viz = [ popEngHurrData
          , width plotWidth
          , transform
            . lookup \"id\" (usGeoData \"states\") \"id\" ('LuAs' \"geo\")
            $ []
          , projection [PrType AlbersUsa]
          , encoding
            . shape [MName \"geo\", MmType GeoFeature]
            . color [MRepeat 'Column', MmType Quantitative, MLegend [LOrient 'LOTop', 'LGradientLength' plotWidth]]
            $ []
          , mark Geoshape [MStroke \"black\", MStrokeOpacity 0.2]
          ]

in toVegaLite
   [ specification $ asSpec viz
   , resolve
     . resolution (RScale [(ChColor, Independent)])
     $ []
   , repeat ['ColumnFields' [\"population\", \"engineers\", \"hurricanes\"]]
   ]
@

By moving the legend to the top of each visualization, I have taken
advantage of the fixed with (here 300 pixels) to ensure the
color bar uses the full width (with 'LGradientLength').

-}

choroplethLookupFromGeo :: VegaLite
choroplethLookupFromGeo :: VegaLite
choroplethLookupFromGeo =
  let popEngHurrData :: PropertySpec
popEngHurrData = Text -> [Format] -> PropertySpec
dataFromUrl Text
"https://raw.githubusercontent.com/vega/vega/master/docs/data/population_engineers_hurricanes.csv" []

      plotWidth :: Double
plotWidth = Double
300

      viz :: [PropertySpec]
viz = [ PropertySpec
popEngHurrData
            , Double -> PropertySpec
width Double
plotWidth
            , [TransformSpec] -> PropertySpec
transform
              ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> PropertySpec
-> Text
-> LookupFields
-> [TransformSpec]
-> [TransformSpec]
lookup Text
"id" (Text -> PropertySpec
usGeoData Text
"states") Text
"id" (Text -> LookupFields
LuAs Text
"geo")
              ([TransformSpec] -> PropertySpec)
-> [TransformSpec] -> PropertySpec
forall a b. (a -> b) -> a -> b
$ []
            , [ProjectionProperty] -> PropertySpec
projection [Projection -> ProjectionProperty
PrType Projection
AlbersUsa]
            , [EncodingSpec] -> PropertySpec
encoding
              ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
shape [Text -> MarkChannel
MName Text
"geo", Measurement -> MarkChannel
MmType Measurement
GeoFeature]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [Arrangement -> MarkChannel
MRepeat Arrangement
Column, Measurement -> MarkChannel
MmType Measurement
Quantitative, [LegendProperty] -> MarkChannel
MLegend [LegendOrientation -> LegendProperty
LOrient LegendOrientation
LOTop, Double -> LegendProperty
LGradientLength Double
plotWidth]]
              ([EncodingSpec] -> PropertySpec) -> [EncodingSpec] -> PropertySpec
forall a b. (a -> b) -> a -> b
$ []
            , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Geoshape [Text -> MarkProperty
MStroke Text
"black", Double -> MarkProperty
MStrokeOpacity Double
0.2]
            ]

  in [PropertySpec] -> VegaLite
toVegaLite
     [ VLSpec -> PropertySpec
specification (VLSpec -> PropertySpec) -> VLSpec -> PropertySpec
forall a b. (a -> b) -> a -> b
$ [PropertySpec] -> VLSpec
asSpec [PropertySpec]
viz
     , [ResolveSpec] -> PropertySpec
resolve
       ([ResolveSpec] -> PropertySpec)
-> ([ResolveSpec] -> [ResolveSpec])
-> [ResolveSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Resolve -> [ResolveSpec] -> [ResolveSpec]
resolution ([(Channel, Resolution)] -> Resolve
RScale [(Channel
ChColor, Resolution
Independent)])
       ([ResolveSpec] -> PropertySpec) -> [ResolveSpec] -> PropertySpec
forall a b. (a -> b) -> a -> b
$ []
     , [RepeatFields] -> PropertySpec
repeat [[Text] -> RepeatFields
ColumnFields [Text
"population", Text
"engineers", Text
"hurricanes"]]
     ]


{-|

We can combine repeated rows and columns to create a grid of
views, such as a scatterplot matrix, adding in color
encoding to separate out the clusters:

<<images/vl/splomplot.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEBOCmAOsCGAXSAuMoIRgewHcMwBtSAJQEEB9ASQGFyBlSAGigBEBROxl9yPAA2ADzZQA4gFskAc0gBdVuByQAxniEBXKQDtiZKr2bjI3Y-yjCxA6XMUqAvstwBnRGuLZVM6AGsvSFcASwAvWGIAVgEUAE9EYkE8YN00ZxVcABNUJC8M1S1oIUSACxQUeFd0AHpq6CQCADpZYJQSrQAjLVdYaA1U2FTGjSlqjjwtWQAhQr9YapKAN1hZJGqZVxReheXV6uyUNdXgpABaJDOANgBGS-PrgAZTw46hWGukRt08U5LkTN6jRQrkWbHyuAAZnhoDI0JhvDhVPAkNAenlERjJDJ5JhILodB1emDMYiKDQGCZcfipIToMSSbhrIlqbT6QyzDwKSwqQSiS4GVBYNQmTyaUTwThHBKpYj0qTBhpMikcVgJepNNDAhDgrAhJlEvRtJs+VA4gkqXgpCkkMU5RjIGJ4ZBtbr9U64IhUIkNNo9JBnKb4hFcQBHLRIVKtVDBZb+-mk2JanV6wIe5BwqDQQhxwPmqBhiMoKNF2MyyVOfmQAAkrjUfxkpXKlRq1V2nxabU6jWCeGqtfrazbpyErXmiwALI0AFauPD6Sv9bUq4BSxxAA Open this visualization in the Vega Editor>

@
let enc = encoding
            . position X [ PRepeat Column, PmType Quantitative ]
            . position Y [ PRepeat Row, PmType Quantitative ]
            . color [ MName \"Cluster\", MmType Nominal ]

    spec = asSpec [ gaiaData
                  , mark Point [ MSize 5 ]
                  , enc [] ]

    fields = [ \"RA_ICRS\", \"DE_ICRS\", \"plx\", \"Gmag\" ]

in toVegaLite
      [ repeat [ RowFields fields, ColumnFields fields ]
      , specification spec
      ]
@

To be honest, this is not the best dataset to use here, as
there is no direct correlation between location (the @RA_ICRS@
and @DE_ICRS@ fields) and the other columns, but it's the
dataset I chose, so we are stuck with it.

Once you have sub-plots as a specification, you can combine
them horizontally and vertically to make a dashboard style
visualization. Interested parties should check out the
<https://github.com/gicentre/elm-vegalite/tree/master/docs/walkthrough#building-a-dashboard-1240 Building a Dashboard> section of the
Elm Vega-Lite Walkthrough for more details.

-}

splomPlot :: VegaLite
splomPlot :: VegaLite
splomPlot =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
             ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Arrangement -> PositionChannel
PRepeat Arrangement
Column, Measurement -> PositionChannel
PmType Measurement
Quantitative ]
             BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Arrangement -> PositionChannel
PRepeat Arrangement
Row, Measurement -> PositionChannel
PmType Measurement
Quantitative ]
             BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal ]

      spec :: VLSpec
spec = [PropertySpec] -> VLSpec
asSpec [ PropertySpec
gaiaData
                    , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Point [ Double -> MarkProperty
MSize Double
5 ]
                    , [EncodingSpec] -> PropertySpec
enc [] ]

      fields :: [Text]
fields = [ Text
"RA_ICRS", Text
"DE_ICRS", Text
"plx", Text
"Gmag" ]

  in [PropertySpec] -> VegaLite
toVegaLite
        [ [RepeatFields] -> PropertySpec
repeat [ [Text] -> RepeatFields
RowFields [Text]
fields, [Text] -> RepeatFields
ColumnFields [Text]
fields ]
        , VLSpec -> PropertySpec
specification VLSpec
spec
        ]


-- $intro-interactivity
-- Interaction is enabled by creating /selections/ that may be combined with
-- the kinds of specifications already described. Selections involve three
-- components:
--
--  * __Events__ are those actions that trigger the interaction such as
--    clicking at a location on screen or pressing a key.
--
--  * __Points of interest__ are the elements of the visualization with
--    which the interaction occurs, such as the set of points selected
--    on a scatterplot.
--
--  * __Predicates__ (i.e. Boolean functions) identify whether or not
--    something is included in the selection. These need not be limited
--    to only those parts of the visualization directly selected through
--    interaction.

{-|

The next several plots show different types of selection -
select a single point, a range of plots, or follow the mouse - and
all have the same basic structure. To avoid repetition, and mistakes,
I am going to introduce a helper function which creates the
plot structure but without the selection definition, and then
use that to build up the plots.

The helper function, 'selectionProperties', takes two arguments, which are
the selection name and the plot title. The selection name is used
to identify the selection, as a visualization can support multiple
selections, and the plot title has been added mainly to show some
minor customization (the use of 'TOrient' to move the title to the
bottom).

The definition of this helper function is:

@
selectionProps selName label =
  let posOpts field = [ PName field
                      , PmType Quantitative
                      , PScale [ 'SZero' False ]
                      ]

      enc = encoding
               . position X (posOpts \"Gmag\")
               . position Y (posOpts \"plx\")

               . color [ 'MSelectionCondition' ('SelectionName' selName)
                           [ MName \"Cluster\", MmType Nominal ]
                           [ 'MString' "grey" ]
                       ]

               . 'opacity' [ MSelectionCondition (SelectionName selName)
                            [ 'MNumber' 1.0 ]
                            [ MNumber 0.3 ]
                         ]

               . 'size' [ MSelectionCondition (SelectionName selName)
                            [ MNumber 40 ]
                            [ MNumber 5 ]
                         ]

      trans = transform
                 . 'filter' ('FExpr' \"datum.DE_ICRS < -20\")

  in [ gaiaData
     , trans []
     , mark Point []
     , enc []
     , title label [ TOrient SBottom ]
     ]
@

The three non-selection-related features added here are that
'SZero' is used to tell Vega Lite that we do not need 0 displayed
on either axis, which leads to a \"tight\" bounding box around
the data, a 'filter' is used to select a subset of rows, namely
only those with a declination less than -20 (via 'FExpr'),
and the plot title is moved to the bottom with 'TOrient'.

The main change is that the selection is used in the encoding section,
identified by name, using 'SelectionName' and the supplied
argument. It is used as a filter for the encoding section, where
'MSelectionCondition' defines the properties to use
when the selection occurs (the first list of properties)
and when it does not (the second list). This is used for
three different encodings:

 - 'color', where the selected star is labelled by its
   cluster color, and all the other are grey;

 - 'opacity', so that the selected star is fully opaque
   whereas un-selected stars are partially transparent;

 - and 'size', so that the selected star is much bigger
   than the others.

When no selection has been made - such as when the visualization
is first created - then all points are encoded with the
\"selected\" case (so colorful, fully opaque, and large in this
case).

-}

selectionProperties ::
  T.Text
  -- ^ The selection name
  -> T.Text
  -- ^ The title for the plot
  -> [PropertySpec]
selectionProperties :: Text -> Text -> [PropertySpec]
selectionProperties Text
selName Text
label =
  let posOpts :: Text -> [PositionChannel]
posOpts Text
field = [ Text -> PositionChannel
PName Text
field
                      , Measurement -> PositionChannel
PmType Measurement
Quantitative
                      , [ScaleProperty] -> PositionChannel
PScale [ Bool -> ScaleProperty
SZero Bool
False ]
                      ]

      enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
               ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X (Text -> [PositionChannel]
posOpts Text
"Gmag")
               BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y (Text -> [PositionChannel]
posOpts Text
"plx")

               BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ BooleanOp -> [MarkChannel] -> [MarkChannel] -> MarkChannel
MSelectionCondition (Text -> BooleanOp
SelectionName Text
selName)
                           [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal ]
                           [ Text -> MarkChannel
MString Text
"grey" ]
                       ]

               BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
opacity [ BooleanOp -> [MarkChannel] -> [MarkChannel] -> MarkChannel
MSelectionCondition (Text -> BooleanOp
SelectionName Text
selName)
                            [ Double -> MarkChannel
MNumber Double
1.0 ]
                            [ Double -> MarkChannel
MNumber Double
0.3 ]
                         ]

               BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
size [ BooleanOp -> [MarkChannel] -> [MarkChannel] -> MarkChannel
MSelectionCondition (Text -> BooleanOp
SelectionName Text
selName)
                            [ Double -> MarkChannel
MNumber Double
40 ]
                            [ Double -> MarkChannel
MNumber Double
5 ]
                         ]

      trans :: [TransformSpec] -> PropertySpec
trans = [TransformSpec] -> PropertySpec
transform
                 ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Filter -> [TransformSpec] -> [TransformSpec]
filter (Text -> Filter
FExpr Text
"datum.DE_ICRS < -20")

  in [ PropertySpec
gaiaData
     , [TransformSpec] -> PropertySpec
trans []
     , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Point []
     , [EncodingSpec] -> PropertySpec
enc []
     , Text -> [TitleConfig] -> PropertySpec
title Text
label [ Side -> TitleConfig
TOrient Side
SBottom ]
     ]


{-|

The actual plot just requires the selection information to be
defined and then added to the plot properties:

<<images/vl/singleselection.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1hJUBLAG2gFNY8oATeaAVywDoARAUQH0BJAMIAlAMpgAPGAC0AJgAMkAL4BdADTgoWeLADWdSAAd0pRNEjqIkRtHh1QES81jl9AC2jQDyXAHofCAHc2AHNSaFdmACNmZBoAY3RTKlM2BKwfDnRmYIAhJx0qH1cANypg+B8tZGpYItLyn2sK8tJ4KXg2gDYARk727rkpG0jyKm74NkR0KVcqeHoaNmhkYvMNSwxsJjt1h0NtWJ2HY6gAcS1g-URWSJo1k72hAEF+YRErm7uLB6gDcgAPD5YW60b4PSDcV6iIEg+4-SBUHh-QH4SDXYF3XYQRS7HHY76QAAkyDisy0bg8Xl8PnqE1C4SibFI6B8JLJFVpUnIYUKxQALGwAFbIRJwyCxUZxaDMxB2QykOIFehy6AATwMVH0yBMwVGSkUBOl0D1+GI1H+ZlRIiokugYHgYCMJjMqigmFIyUtUEi6A86BwBo0CMQCXoOqOewS5EwEeOkGK8HIzE1qOCsCoqrhcYSiDD0tFpqxezINuVqIESeqXyLlglVClMv0BgVSqzJxg6pTUCmWBMicgRbxDkDce1AC8u8QE0muwBWV2QHN5xum+OJ5N0PlyBd1hsF34tqjKxQjvboAzwOJhTOr6cb-ByNgAZgXS7CK7AU-XXe6O5t9fzWVUWbRUj31MEoBRT8SA9cgyzOC5zCgElE0nSAJ1gdA6FQRNYgNKA1Q1fQAEdmCQI0mFIUolAgyAb2gks4KbAEkPFOJULlDCsPwHDyDwhdCK7SBSPIsJKOoodT0XRIyEuU0cUUIA Open this visualization in the Vega Editor>

@
let selLabel = "picked"
    sel = 'selection'
            . 'select' selLabel 'Single' []

in toVegaLite (sel [] : 'selectionProperties' selLabel "Select a point")
@

The 'selection' function is used to define the selection, via one or
more applications of the 'select' function. The form of 'select' is
that the selection is named, in this case we use @\"picked\"@, and the
type is given (a 'Single' click), and then options, which in our case
there aren't any, so an empty list is used.

Note that @hvega@ does not track the selection names, and will allow
you to use a name that you have not defined.

-}

singleSelection :: VegaLite
singleSelection :: VegaLite
singleSelection =
  let selLabel :: Text
selLabel = Text
"picked"

      sel :: [SelectSpec] -> PropertySpec
sel = [SelectSpec] -> PropertySpec
selection
              ([SelectSpec] -> PropertySpec)
-> ([SelectSpec] -> [SelectSpec]) -> [SelectSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> Selection -> [SelectionProperty] -> [SelectSpec] -> [SelectSpec]
select Text
selLabel Selection
Single []

  in [PropertySpec] -> VegaLite
toVegaLite ([SelectSpec] -> PropertySpec
sel [] PropertySpec -> [PropertySpec] -> [PropertySpec]
forall a. a -> [a] -> [a]
: Text -> Text -> [PropertySpec]
selectionProperties Text
selLabel Text
"Select a point")


{-|

The only change here is to add a property to the selection - that
is @'Nearest' True@ - which means that the nearest point to the
click will be highlighted.

<<images/vl/nearestselection.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1hJUBLAG2gFNY8oATeaAVywDoARAUQH0BJAMIAlAMpgAPGAC0AJgAMkAL4BdADTgoWeLADWdSAAd0pRNEjqIkRtHh1QES81jl9AC2jQDyXAHofCAHc2AHNSaFdmACNmZBoAY3RTKlM2BKwfDnRmYIAhJx0qH1cANypg+B8tZGpYItLyn2sK8tJ4KXg2gDYARk727rkpG0jyKm74NkR0KVcqeHoaNmhkYvMNSwxsJjt1h0NtWJ2HY6gAcS1g-URWSJo1k72hAEF+YRErm7uLB6gDcgAPD5YW60b4PSDcV6iIEg+4-SBUHh-QH4SDXYF3XYQRS7HHY76QAAkyDisy0bg8Xl8PnqE1C4SibFI6B8JLJFVpUnIYUKxQALGwAFbIRJwyCxUZxaDMxB2QykOIFehyxBzWBUap0ODMKiqKDQACeBio+mQJmCoyUigJ0uglvwxGo-zMqJEVEl0DAqu0Gs9RhMZj1kEwpGSLqgkXQHnQOGtGgRiAS9HNRz2CXImFTx0gxXg5B1+mC6oNcOzCUQyeloodWL2ZHdytRAnz1S+tcsEqoUpl+gMCqVpZOMCNJtRUywJjzkFreIccezZoAXqOwMRc-mVwBWIPlys9h05vMF-B8uRBzvd6u-ftUZWKed7dAGeBxMIlg-r49gORsADMO8SPcrzXI8V26c93S7KtZVRPtFVvK0wSgFFVxIUNyEbM4LnMKASTzFdiGXWB0DoVA81ia19RHfQAEdmCQW0mFIUolCQyB31Q+sMN7AEcPFOJ8LlIiSPwMjyAooNDWNWj6NMMImJY2cH0gcsyEuB0cUUIA Open this visualization in the Vega Editor>

@
let selLabel = "picked"
    sel = selection
            . select selLabel Single [ 'Nearest' True ]

in toVegaLite (sel [] : selectionProperties selLabel "Select nearest point")
@

One consequence of this change is that once a point has been selected you
can not remove this (i.e. un-select the point). This is in contrast to
'singleSelection', where clicking on an area with no stars would remove the
previous selection. The 'Clear' property can be added to the list
to define a way to clear the selection.

-}

nearestSelection :: VegaLite
nearestSelection :: VegaLite
nearestSelection =
  let selLabel :: Text
selLabel = Text
"picked"

      sel :: [SelectSpec] -> PropertySpec
sel = [SelectSpec] -> PropertySpec
selection
              ([SelectSpec] -> PropertySpec)
-> ([SelectSpec] -> [SelectSpec]) -> [SelectSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> Selection -> [SelectionProperty] -> [SelectSpec] -> [SelectSpec]
select Text
selLabel Selection
Single [ Bool -> SelectionProperty
Nearest Bool
True ]

  in [PropertySpec] -> VegaLite
toVegaLite ([SelectSpec] -> PropertySpec
sel [] PropertySpec -> [PropertySpec] -> [PropertySpec]
forall a. a -> [a] -> [a]
: Text -> Text -> [PropertySpec]
selectionProperties Text
selLabel Text
"Select nearest point")


{-|

The selection can easily be changed to allow multiple stars
to be selected, using shift-click, by swapping from 'Single' to 'Multi'.

<<images/vl/multiselection.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1hJUBLAG2gFNY8oATeaAVywDoARAUQH0BJAMIAlAMpgAPGAC0AJgAMkAL4BdADTgoWeLADWdSAAd0pRNEjqIkRtHh1QES81jl9AC2jQDyXAHofCAHc2AHNSaFdmACNmZBoAY3RTKlM2BKwfDnRmYIAhJx0qH1cANypg+B8tZGpYItLyn2sK8tJ4KXg2gDYARk727rkpG0jyKm74NkR0KVcqeHoaNmhkYvMNSwxsJjt1h0NtWJ2HY6gAcS1g-URWSJo1k72hAEF+YRErm7uLB6gDcgAPD5YW60b4PSDcV6iIEg+4-SBUHh-QH4SDXYF3XYQRS7HHY76QAAkyDisy0bg8Xl8PnqE1C4SibFI6B8JLJFVpUnIYUKxQALGwAFbIRJwyCxUZxaDMxB2GCuUjIMCKsCCmLQMDwMDkeC3Fz4YjQACeBio+iwzEopCUigJ0ugozl1H+ZlRIgVqA1cW5cR0YGg6DAEqoUrARhMy3MUEwpGSrqgkXQHnQOFtGgRiAS9BMlwNu0gCXImCOJ0gxXg5GYZtRwVgVCNcOOBcS2elorzPygZCo5Ho+gEleqXyxe2DUpl+nCKpVauqmu1up7jdLxtNVxTJgrkBHeIcaabyFIAC9q2B7KXy5XTwBWMF7BKIVsTg1litVuh8uSqKBjtuy1FTkqM7qvOOp6kouJ3pA6AGPAcRhA2HYXm+p5yGwADMd6WA+T7tmer5XnQ3TfuKPYhn+k4KkBSqzhqWpgUuu74vmKL4d2vb6Oc8CXCRJIVqexAnrA6B0KgFaxLaUCrqekAAI7MEg9pMKQpRKFBiFsbGHGosiUbinE-FykJIn4GJ5ASSR0n6PJilhMpqm7vuUAPmQuZnjiihAA Open this visualization in the Vega Editor>

@
let selLabel = "this is just a label"
    sel = selection
            . select selLabel 'Multi' []

in toVegaLite (sel [] : selectionProperties selLabel "Shift click to select points")
@

-}

multiSelection :: VegaLite
multiSelection :: VegaLite
multiSelection =
  let selLabel :: Text
selLabel = Text
"this is just a label"

      sel :: [SelectSpec] -> PropertySpec
sel = [SelectSpec] -> PropertySpec
selection
              ([SelectSpec] -> PropertySpec)
-> ([SelectSpec] -> [SelectSpec]) -> [SelectSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> Selection -> [SelectionProperty] -> [SelectSpec] -> [SelectSpec]
select Text
selLabel Selection
Multi []

  in [PropertySpec] -> VegaLite
toVegaLite ([SelectSpec] -> PropertySpec
sel [] PropertySpec -> [PropertySpec] -> [PropertySpec]
forall a. a -> [a] -> [a]
: Text -> Text -> [PropertySpec]
selectionProperties Text
selLabel Text
"Shift click to select points")


{-|

We can take advantage of browser event by using 'On' to define which
event to use, such as mouse movement over points:

<<images/vl/eventselection.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1hJUBLAG2gFNY8oATeaAVywDoARAUQH0BJAMIAlAMpgAPGAC0AJgAMkAL4BdADTgoWeLADWdSAAd0pRNEjqIkRtHh1QES81jl9AC2jQDyXAHofCAHc2AHNSaFdmACNmZBoAY3RTKlM2BKwfDnRmYIAhJx0qH1cANypg+B8tZGpYItLyn2sK8tJ4KXg2gDYARk727rkpG0jyKm74NkR0KVcqeHoaNmhkYvMNSwxsJjt1h0NtWJ2HY6gAcS1g-URWSJo1k72hAEF+YRErm7uLB6gDcgAPD5YW60b4PSDcV6iIEg+4-SBUHh-QH4SDXYF3XYQRS7HHY76QAAkyDisy0bg8Xl8PnqE1C4SibFI6B8JLJFVpUnIYUKxQALGwAFbIRJwyCxUZxaDMxB2QykOIFehy6AATwMVH0WGYlFI5igotRWCysXQpVoikUBOl0FGKqo-zMqIAsmaqGBwu6jCYah70GAJVQpWB4GBvaZ9ZBMKRkk6oJF0B50DgrRoEYgEvQTJd8PY9glyJgjidIMV4ORmJrUcFYFRVXDjpAEogs9LDWA8+CyFRyMrUQIK9Uvli9oGpTL9AYFUqGyW1RqrsmTOXICO8Q5U43kKQAF5Vjul8uVugAVlUUGbrYnucPFf3fLk5-FPaDbdlqKniqoystYINBngOIwnrG8yzvOg5DYABmJ9LzCa8DzA498G6J8xzfSdp2-JRNz2FED27Xt9HOeBLjQuJy33Yg91gdA6FQctYitKB533SAAEdmCQG0mFIUolD-SAQIImMiI-AFIxJSi5Rouj8AY8gmKfVj9E47iwl4-j11wptEjIHMOxxRQgA Open this visualization in the Vega Editor>

@
let selLabel = "picked"
    sel = selection
            . select selLabel Multi [ 'On' "mouseover" ]

in toVegaLite (sel [] : selectionProperties selLabel "Move the pointer to select a point")
@

The supported list of events is described in the
<https://vega.github.io/vega/docs/event-streams/#selector Vega Event-Stream Selectors> documentation.

The addition of @Nearest True@ to the list of properties
sent to 'select' would avoid the flickering, as the mouse moves
between the stars.

-}
eventSelection :: VegaLite
eventSelection :: VegaLite
eventSelection =
  let selLabel :: Text
selLabel = Text
"picked"

      sel :: [SelectSpec] -> PropertySpec
sel = [SelectSpec] -> PropertySpec
selection
              ([SelectSpec] -> PropertySpec)
-> ([SelectSpec] -> [SelectSpec]) -> [SelectSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> Selection -> [SelectionProperty] -> [SelectSpec] -> [SelectSpec]
select Text
selLabel Selection
Multi [ Text -> SelectionProperty
On Text
"mouseover" ]

  in [PropertySpec] -> VegaLite
toVegaLite ([SelectSpec] -> PropertySpec
sel [] PropertySpec -> [PropertySpec] -> [PropertySpec]
forall a. a -> [a] -> [a]
: Text -> Text -> [PropertySpec]
selectionProperties Text
selLabel Text
"Move the pointer to select a point")


{-|

The final 'Selection' value is 'Interval',
which lets you drag a rectangle to select the interior points:

<<images/vl/intervalselection.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1hJUBLAG2gFNY8oATeaAVywDoARAUQH0BJAMIAlAMpgAPGAC0AJgAMkAL4BdADTgoWeLADWdSAAd0pRNEjqIkRtHh1QES81jl9AC2jQDyXAHofCAHc2AHNSaFdmACNmZBoAY3RTKlM2BKwfDnRmYIAhJx0qH1cANypg+B8tZGpYItLyn2sK8tJ4KXg2gDYARk727rkpG0jyKm74NkR0KVcqeHoaNmhkYvMNSwxsJjt1h0NtWJ2HY6gAcS1g-URWSJo1k72hAEF+YRErm7uLB6gDcgAPD5YW60b4PSDcV6iIEg+4-SBUHh-QH4SDXYF3XYQRS7HHY76QAAkyDisy0bg8Xl8PnqE1C4SibFI6B8JLJFVpUnIYUKxQALGwAFbIRJwyCxUZxaDMxB2NHwLAmYJgUjIMCubT0OXQACeBio+hMNWK8BcikUBOl0FG2qo-zMqI4CGV8DAsCoUqQwVGYGg6DAEo90DARiNyHMUEwpGSDqgkXQHnQOAtGgRiAS9CVRz2CXImGzx0gJvIzANqOC7p1cMLCUQmelovw9nhZCo5C1qIEJeqXyxe0DUplVwVSpVao1sC1YJOMD1ZagU0ViFNkD7eIcKcLyFIAC9583C8XS3QAKzTyy1+tDptF03H-B8uSqKADhuy1HLpfK1XqzVKXHTpA6AGPAcRhFWTZ9reJbznIbAAMznlAl5hNeYDEEe87dM+4ptkGaHyl+Y6-pO-7HJuewouhJDRu2+jnPAlw4SSpr7pAe6wOgdCoKasQWlAur6voACOzBIFaTCkKUSiARB1GtnRqLIhG4pxKxcocVx+A8eQfE4YJ86QKJ4lhJJ0nrhRkC1mQlxNjiihAA Open this visualization in the Vega Editor>

@
let selLabel = "naming is hard"
    sel = selection
            . select selLabel 'Interval' [ ]

in toVegaLite (sel [] : selectionProperties selLabel "Drag a rectangle to select points")
@

-}

intervalSelection :: VegaLite
intervalSelection :: VegaLite
intervalSelection =
  let selLabel :: Text
selLabel = Text
"naming is hard"

      sel :: [SelectSpec] -> PropertySpec
sel = [SelectSpec] -> PropertySpec
selection
              ([SelectSpec] -> PropertySpec)
-> ([SelectSpec] -> [SelectSpec]) -> [SelectSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> Selection -> [SelectionProperty] -> [SelectSpec] -> [SelectSpec]
select Text
selLabel Selection
Interval [ ]

  in [PropertySpec] -> VegaLite
toVegaLite ([SelectSpec] -> PropertySpec
sel [] PropertySpec -> [PropertySpec] -> [PropertySpec]
forall a. a -> [a] -> [a]
: Text -> Text -> [PropertySpec]
selectionProperties Text
selLabel Text
"Drag a rectangle to select points")


{-|

The default interval option is to select a rectangle, but it can be restricted -
such as to select all items within a range along a given axis
using 'Encodings':

<<images/vl/intervalselectiony.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1hJUBLAG2gFNY8oATeaAVywDoARAUQH0BJAMIAlAMpgAPGAC0AJgAMkAL4BdADTgoWeLADWdSAAd0pRNEjqIkRtHh1QES81jl9AC2jQDyXAHofCAHc2AHNSaFdmACNmZBoAY3RTKlM2BKwfDnRmYIAhJx0qH1cANypg+B8tZGpYItLyn2sK8tJ4KXg2gDYARk727rkpG0jyKm74NkR0KVcqeHoaNmhkYvMNSwxsJjt1h0NtWJ2HY6gAcS1g-URWSJo1k72hAEF+YRErm7uLB6gDcgAPD5YW60b4PSDcV6iIEg+4-SBUHh-QH4SDXYF3XYQRS7HHY76QAAkyDisy0bg8Xl8PnqE1C4SibFI6B8JLJFVpUnIYUKxQALGwAFbIRJwyCxUZxaDMxBHPaIeBYEzBMCkZBgaoUchgVzaeh2BGIBL0ZXIOgESAAT0gaig0EtBio+hMNWK8BceLAigJ0ugowN1H+ZlRHAQKug6A1VEl0DARhd6silrj2nd5HggNUUEwpGSwagkXQHnQOG9GkNxuVcssCXImGrezd5GYTtRwVgVGtYL2CUQJulovw9nhZGj+tRAmb1S+WL2EqoUplV0VytV6s15G1utg+u7xxgDtbUCmSoVLlnnvxu3FpAAXkfh-umy26ABWPdQXv9pdDyDPo98nIWbitGC4DrKqIKqeKpqhq0qbjqepKLi3aQOgBjwHEYTWkOs5-u6L74HIbAAMwfpAX5hD+YDEP+dDdMB86LoOx4rogMHrvBW5IZeXqoSiNEkLm5DjmcFzmFAJLug+kD3rA6B0Kg7qxN6dqHvoACOzBIL6TCkKUSioThgmjiJ+jIhJ4pxNJBpyQp+BKeQKnAfajqadpphhHpBmemWNaJGQlxDjiihAA Open this visualization in the Vega Editor>

@
let selLabel = "naming is still hard"
    sel = selection
            . select selLabel Interval [ 'Encodings' [ 'ChY' ] ]

in toVegaLite (sel [] : selectionProperties selLabel "Drag to select points by parallax")
@

We'll come back to further things to do with interval selections
when we get to interactive plots below (see 'bindScales').

-}


intervalSelectionY :: VegaLite
intervalSelectionY :: VegaLite
intervalSelectionY =
  let selLabel :: Text
selLabel = Text
"naming is still hard"

      sel :: [SelectSpec] -> PropertySpec
sel = [SelectSpec] -> PropertySpec
selection
              ([SelectSpec] -> PropertySpec)
-> ([SelectSpec] -> [SelectSpec]) -> [SelectSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> Selection -> [SelectionProperty] -> [SelectSpec] -> [SelectSpec]
select Text
selLabel Selection
Interval [ [Channel] -> SelectionProperty
Encodings [ Channel
ChY ] ]

  in [PropertySpec] -> VegaLite
toVegaLite ([SelectSpec] -> PropertySpec
sel [] PropertySpec -> [PropertySpec] -> [PropertySpec]
forall a. a -> [a] -> [a]
: Text -> Text -> [PropertySpec]
selectionProperties Text
selLabel Text
"Drag to select points by parallax")


-- $intro-selection-transforms
-- Simple selections as described above create sets of selected data marks
-- based directly on what was interacted with by the user. Selection
-- transformations allow us to /project/ that direct selection onto other
-- parts of our dataset.

{-|

For example, we can adjust the visualization to select all stars in the
same cluster, which is useful in this case since the Blanco1 and
IC2391 clusters occupy the same space in the magnitude-parallax
plane.

This is invoked simply by adding the 'Fields' constructor to the select
parameters naming the fields onto which we wish to project our selection.
Additionally, we have set the default selection with 'Empty' so that if
nothing is selected, the selection is empty
(as we have previously seen, without this the default selection is the
entire encoded dataset).

<<images/vl/transformselection.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1hJUBLAG2gFNY8oATeaAVywDoARAUQH0BJAMIAlAMpgAPGAC0AJgAMkAL4BdADTgoWeLADWdSAAd0pRNEjqIkRtHh1QES81jl9AC2jQDyXAHofCAHc2AHNSaFdmACNmZBoAY3RTKlM2BKwfDnRmYIAhJx0qH1cANypg+B8tZGpYItLyn2sK8tJ4KXg2gDYARk727rkpG0jyKm74NkR0KVcqeHoaNmhkYvMNSwxsJjt1h0NtWJ2HY6gAcS1g-URWSJo1k72hAEF+YRErm7uLB6gDcgAPD5YW60b4PSDcV6iIEg+4-SBUHh-QH4SDXYF3XYQRS7HHY76QAAkyDisy0bg8Xl8PnqE1C4SibFI6B8JLJFVpUnIYUKxQALGwAFbIRJwyCxUZxaDMxBHPYGUhxPT4ewnNFzWBUap0ODMKhgvZULAGaAATyuiSocOOMFNBitqOQJmCo2tezIVHI9GQdAIkAE5BiNUgylxGkUBOl0FdKpgVH+ZlRIk9VClYHgYCMJmgqjAEtT0HTYDigeqXygmFIyUTUEi6A86BwEY0CMQCXozrllgS5EwXb2xXggYdUGCmvNBu7iQ70tFsY9Xv0AaD5fFKalMv0CqV5igZvtFqwJiHSlxBvFpAAXiPiIPh3QAKy5yAJRAzzexu96uh8uTP-MbnOvyKnoijNja6AGPAcRhOan5Dt++ByGwADMz6vu+QG3ghI7dP+66zrKqLbqB4F7CiYDEAu9D6Oc8CXP+cRDjekDXrA6B0KgQ6xBGe52iOkAAI7MEgUZMKQpRKOecGUSQVaLsRAK7uKTExrJbEcfgXHkDxz77gJwmiWE4mSXiYBkS+iRkJcKo4ooQA Open this visualization in the Vega Editor>

@
let sel = selection
            . select "pick" Single [ 'Fields' [ \"Cluster\" ]
                                   , 'Empty'
                                   , Nearest True
                                   ]

in toVegaLite (sel [] : selectionProperties "pick" "Select a point, select a cluster")
@

-}

-- TODO: why did I call this transformSelection?

transformSelection :: VegaLite
transformSelection :: VegaLite
transformSelection =
  let sel :: [SelectSpec] -> PropertySpec
sel = [SelectSpec] -> PropertySpec
selection
              ([SelectSpec] -> PropertySpec)
-> ([SelectSpec] -> [SelectSpec]) -> [SelectSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> Selection -> [SelectionProperty] -> [SelectSpec] -> [SelectSpec]
select Text
"pick" Selection
Single [ [Text] -> SelectionProperty
Fields [ Text
"Cluster" ]
                                     , SelectionProperty
Empty
                                     , Bool -> SelectionProperty
Nearest Bool
True
                                     ]

  in [PropertySpec] -> VegaLite
toVegaLite ([SelectSpec] -> PropertySpec
sel [] PropertySpec -> [PropertySpec] -> [PropertySpec]
forall a. a -> [a] -> [a]
: Text -> Text -> [PropertySpec]
selectionProperties Text
"pick" Text
"Select a point, select a cluster")


-- TODO: can I think of anything to do with lookupSelection


-- $intro-selection-binding
-- Selection need not be limited to direct interaction with the
-- visualization marks. We can also /bind/ the selection to other
-- user-interface components.

{-|

New in Vega Lite 4 is the ability to interact with the legend via
the 'BindLegend' option. In this case
selecting on a cluster in the legend will highlight that cluster in
the visualization (but not vice versa). Notice how the legend now
also follows the 'MSelectionCondition' rules (that is, the unselected
items in the image below are also drawn in grey and are partially
transparent).

<<images/vl/legendselection.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1hJUBLAG2gFNY8oATeaAVywDoARAUQH0BJAMIAlAMpgAPGAC0AJgAMkAL4BdADTgoWeLADWdSAAd0pRNEjqIkRtHh1QES81jl9AC2jQDyXAHofCAHc2AHNSaFdmACNmZBoAY3RTKlM2BKwfDnRmYIAhJx0qH1cANypg+B8tZGpYItLyn2sK8tJ4KXg2gDYARk727rkpG0jyKm74NkR0KVcqeHoaNmhkYvMNSwxsJjt1h0NtWJ2HY6gAcS1g-URWSJo1k72hAEF+YRErm7uLB6gDcgAPD5YW60b4PSDcV6iIEg+4-SBUHh-QH4SDXYF3XYQRS7HHY76QAAkyDisy0bg8Xl8PnqE1C4SibFI6B8JLJFVpUnIYUKxQALGwAFbIRJwyCxUZxaDMxBHPajYLJeh2SCREzK1EKpXmKDQACeBio+mQJmCox1JFIVHI9GQdAIkAE5BiNUgyjxYEUBOl0HN+GI1H+ZlRImtVClYHgYC1iHoYB5OFUUEwVtM+ki6A86BwXo0CMQCXoprllgS5EwJb2xXgzqNqOCsCoerhx0gCVjYRlldbZGtGqgTpdXyxewl4elos1ZW1YJOMANdagUywJhrkBHHvxu3FpAAXoviNXa3QAKxJtuJIsT2X+yBH5iLvlyc9jqVdqeK2NKXOt9AGeBxGEza3vei5yGwADM57tle75gIeNYPnQ3QvmGb6TlAMbKooP57Ci8GWn2+jnPAlwvnENYHpA+6wOgdCoDWsRerqC76AAjswSA+kwpClEos6QMBBG9ja+jIhaJKUSqNF0fgDHkEx576oa7GcaYYQ8XxHq4ReiBkJc-o4ooQA Open this visualization in the Vega Editor>

@
let sel = selection
          . select "pick" Single [ 'BindLegend'
                                   ('BLField' \"Cluster\")
                                 ]

in toVegaLite (sel [] : selectionProperties "pick" "Select a legend item")
@

-}

legendSelection :: VegaLite
legendSelection :: VegaLite
legendSelection =
  let sel :: [SelectSpec] -> PropertySpec
sel = [SelectSpec] -> PropertySpec
selection
            ([SelectSpec] -> PropertySpec)
-> ([SelectSpec] -> [SelectSpec]) -> [SelectSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> Selection -> [SelectionProperty] -> [SelectSpec] -> [SelectSpec]
select Text
"legend" Selection
Single [ BindLegendProperty -> SelectionProperty
BindLegend
                                       (Text -> BindLegendProperty
BLField Text
"Cluster")
                                     ]

  in [PropertySpec] -> VegaLite
toVegaLite ([SelectSpec] -> PropertySpec
sel [] PropertySpec -> [PropertySpec] -> [PropertySpec]
forall a. a -> [a] -> [a]
: Text -> Text -> [PropertySpec]
selectionProperties Text
"legend" Text
"Select a legend item")


{-|

The Elm Vega-Lite walkthrough uses a dataset which has a
column for which a range-slider makes sense. The dataset I'm
using is less rich, and so I am going to use a HTML select
widget - a drop-down list of values - instead. This lets
the user select all stars from a given cluster, and is
introduced with the 'Bind' and 'ISelect' constructors.

The 'InOptions' list is given the values of the Cluster column that
can be selected: I start with a value not in the list (@none@) just to
indicate that no values are selected, and then the list of clusters in
this sub-sample (remembering that 'selectionProperties' applies a
declination cut off). Eagle-eyed readers will note that the cluster
names in this list (the @clusters@ variable) end in spaces: this is
because the input data file has the cluster names stored in an
eight-character field, even though it is a tab-separated file.
This surprised me when I first tried this visualization, and
using the value \"Blanco1\" did not select anything! Isn't working
with data so much fun!

<<images/vl/widgetselection.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1hJUBLAG2gFNY8oATeaAVywDoARAUQH0BJAMIAlAMpgAPGAC0AJgAMkAL4BdADTgoAY3SIyAczrEARvE0BrPbHTNE9OpCNVSeqkvURIWeLDP2ADuikiNCQ7gxM8IYaHsyw5PYAFtDQfsi4APTpCADubHqk0AnMRszINNrBVMFs2ljpHNZ6AEKxZlTpCQBuVHrw6V7I1LAd3b3pjNB9vaTwUvCzAGwAjAtzS3JSk0bkVEvwbIjoUglU8PQ0bNDInaHRUBjYTFEQL1B+3mXPry+QAOJeBnwkEQrEctDC3ygQgAgvxhCJ7CCsGDbpCPH5yAAPRGgmiotGQbhw0Q45F4iHfSBUHgY7FApEou4QRR3FnMsKQAAkyE0Jy8iWSqQy6VG+3yhWKbFI6HSPL5fVFUnIBXanQALGwAFbIHT4yBlHaaaDSxBfdGkcxUOz4UCQqlYPzQACeiJ0rgpHiMQWtYFtBIE5FKQzNBKCfmYISBBqoRvxaKg6EdJuQdAIwLdoSgTXISG0SygqigghkAGYAJz5gtFgQyBZyGQeQuQAByvxraoArPnIMomS82d9FB6oM6-K4o0E9Ds4z8yFRyPQU-g0wGg3je68B0ONDACtObTAqJjI1AAAo7eBlMDRo1geBgTSBwbkhOwUhVE8OdDJdA4bceKptHoScQ0gbRyEwEMPE6eBA3HKBLCoF1hzAnRgONXUbT7Dw5wXexV2fcFsKgG8MNNIE-AtNo7GHDxR3g9MsCCWDID7Ad2TufVSAAL3g4gYLgugOybCp0JNQxIAE5h4LVOQm1I8SKKoq0lH-H5E1MAoXQPKT4LkNgSxEtCCkU31JNg6S6CWeT5xjMj-GUuxFDUjw6TM3CfT+AFM31TRYL4yBeKsOhUFgsohxHJ0x3sABHZgkGNSZjW6NxOO09z3zwiisR8nl-IkoL0BCsKqAimAooYuKEoKJhSBSrcQEUIA Open this visualization in the Vega Editor>

@
let picked = "picked"

    clusters = [ \"none\", \"Blanco1 \", \"IC2391  \", \"IC2602  \", \"NGC2451 \" ]
    sel = selection
            . select picked Single [ Fields [ \"Cluster\" ]
                                   , 'Bind' [ 'ISelect' \"Cluster\" [ 'InOptions' clusters ] ]
                                   , Empty
                                   ]

   conf = configure
            . configuration (BackgroundStyle "beige")

in toVegaLite (conf [] :
               sel [] :
               selectionProperties picked \"Please select a cluster\")
@

Originally this example had the selection working both ways - that is
the HTML widget can be used to select a cluster and clicking on a point on
the visualization updated the HTML widget. However, this no-longer happens
and I don't know whether it is a change in Vega-Lite or I changed
something in the visualization!

Unlike the other plots shown in the tutorial, this is a screen grab
rather than a PNG file created by Vega Embed. The background color was
changed - following the approach used in
'stripPlotWithBackground' - to show where the visualization "ends" and
the HTML select element starts. It also shows the Vega Embed "drop-down"
menu in the top-right corner, namely the three dots in a circle.

-}

widgetSelection :: VegaLite
widgetSelection :: VegaLite
widgetSelection =
  let picked :: Text
picked = Text
"picked"

      clusters :: [Text]
clusters = [ Text
"none", Text
"Blanco1 ", Text
"IC2391  ", Text
"IC2602  ", Text
"NGC2451 " ]
      sel :: [SelectSpec] -> PropertySpec
sel = [SelectSpec] -> PropertySpec
selection
              ([SelectSpec] -> PropertySpec)
-> ([SelectSpec] -> [SelectSpec]) -> [SelectSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> Selection -> [SelectionProperty] -> [SelectSpec] -> [SelectSpec]
select Text
picked Selection
Single [ [Text] -> SelectionProperty
Fields [ Text
"Cluster" ]
                                     , [Binding] -> SelectionProperty
Bind [ Text -> [InputProperty] -> Binding
ISelect Text
"Cluster" [ [Text] -> InputProperty
InOptions [Text]
clusters ] ]
                                     , SelectionProperty
Empty
                                     ]

      conf :: [ConfigureSpec] -> PropertySpec
conf = [ConfigureSpec] -> PropertySpec
configure
               ([ConfigureSpec] -> PropertySpec)
-> BuildConfigureSpecs -> [ConfigureSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ConfigurationProperty -> BuildConfigureSpecs
configuration (Text -> ConfigurationProperty
BackgroundStyle Text
"beige")

  in [PropertySpec] -> VegaLite
toVegaLite ([ConfigureSpec] -> PropertySpec
conf [] PropertySpec -> [PropertySpec] -> [PropertySpec]
forall a. a -> [a] -> [a]
:
                 [SelectSpec] -> PropertySpec
sel [] PropertySpec -> [PropertySpec] -> [PropertySpec]
forall a. a -> [a] -> [a]
:
                 Text -> Text -> [PropertySpec]
selectionProperties Text
picked Text
"Please select a cluster")



{-|

The selection can also be bound to an axis (or both axes, as in this
case), using 'BindScales' (applying it to the 'intervalSelectionY' plot).

<<images/vl/bindscales.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1hJUBLAG2gFNY8oATeaAVywDoARAUQH0BJAMIAlAMpgAPGAC0AJgAMkAL4BdADTgoWeLADWdSAAd0pRNEjqIkRtHh1QES81jl9AC2jQDyXAHofCAHc2AHNSaFdmACNmZBoAY3RTKlM2BKwfDnRmYIAhJx0qH1cANypg+B8tZGpYItLyn2sK8tJ4KXg2gDYARk727rkpG0jyKm74NkR0KVcqeHoaNmhkYvMNSwxsJjt1h0NtWJ2HY6gAcS1g-URWSJo1k72hAEF+YRErm7uLB6gDcgAPD5YW60b4PSDcV6iIEg+4-SBUHh-QH4SDXYF3XYQRS7HHY76QAAkyDisy0bg8Xl8PnqE1C4SibFI6B8JLJFVpUnIYUKxQALGwAFbIRJwyCxUZxaDMxBHPYGUhxAr0OyQSImFWoknwUbIcxQZIJegmYJ6-AESAAT31kEBaig0EtBio+hMNWKOqUGkUBOl0FGquo-zMqI4CGCYEwYAAXuh0FgwOEqGB4P8qHrVFBMKRkiGoJF0B540oCYb0MbEJd8PY9glyJg5cdIB7yMwXajgrAqNawbXEsbpaLq1i9mQqORNVABK3ql8R5YJVQpTL9AqlVQVb2m47nVd4yZPSO8Q4fbtxaRo+2wMQW226ABWTOQBKIAcr6vNnV3-B8uRPxfLkOvyKsqSink26AGPAcRhNaH63lechsAAzE+L5vkBN5fle3T-uOS6DrKqJrqBijgXsKLXiQOYTvo5zwJc-5xDqV7EJesDoHQqA6rEPoOk6V6QAAjswSB+kwpClCWZ5wVRY60cRAI2tqAYfuxnH4Nx5C8U+O6CSJYlhBJUnHuRz6JGQVbXjiihAA Open this visualization in the Vega Editor>

@
let picked = "picked"

    sel = selection
            . select picked Interval [ Encodings [ 'ChX', ChY ], 'BindScales' ]

in toVegaLite (sel [] : selectionProperties picked "Drag or zoom the axes")
@

The image here was created after panning and zooming into the data.

-}

bindScales :: VegaLite
bindScales :: VegaLite
bindScales =
  let picked :: Text
picked = Text
"picked"

      sel :: [SelectSpec] -> PropertySpec
sel = [SelectSpec] -> PropertySpec
selection
              ([SelectSpec] -> PropertySpec)
-> ([SelectSpec] -> [SelectSpec]) -> [SelectSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> Selection -> [SelectionProperty] -> [SelectSpec] -> [SelectSpec]
select Text
picked Selection
Interval [ [Channel] -> SelectionProperty
Encodings [ Channel
ChY, Channel
ChX ], SelectionProperty
BindScales ]

  in [PropertySpec] -> VegaLite
toVegaLite ([SelectSpec] -> PropertySpec
sel [] PropertySpec -> [PropertySpec] -> [PropertySpec]
forall a. a -> [a] -> [a]
: Text -> Text -> [PropertySpec]
selectionProperties Text
picked Text
"Drag or zoom the axes")


-- $intro-coordinated-views
-- One of the more powerful aspects of selection-based interaction is in
-- coordinating different views – a selection of a data subset is projected
-- onto all other views of the same data.

{-|

The following plot doesn't contain anything new, but allows us to
select a rectangular-range on one plot, and see the same selection
automatically applied to the other plots. This is achieved by
combining the 'repeat' fuction with the 'selection'; this causes the
selection to be projected across all views as it is duplicated.

For this example we use all the clusters, rather than the subset of
Southern ones. One trick I use is to convert the Right Ascension
values (which have a domain of 0 to 360 degrees, and wrap around at
the 0\/360 mark), into their cosine values (remembering to convert to
radians first), and display that instead. This ensures the \"Blanco1\"
cluster members are spatially co-located on this axis - with values
close to 1 - rather than appearing near 0 and 360. This is more to
show things you /can/ do with Vega-Lite, rather than necesarily
things you __should__ do :-)

<<images/vl/coordinatedviews.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEBOCmAOsCGAXSAuMwYHsDuGYA2pAMY4DOASgIKQA0UAIgKID6AkgMJUDKkAXUZkcAGwCuAWwB2hEvFEAPBlADikpAHNBAX3rgoFRKUKgIESCmhJpFAGY5okuQfNZISCoRHU6w0kiipOKiqLDe5BQAFAAmqFIAdLScPLxgAFRgAAocYAD0YACMABwADACUkDquYEI1kBrQANYRAJbQpKLh+m6QcShIpjUW4tCi3gAWKCjwFOh5edZ4CZqtKBPiAEbiFLAdONIosIcJ5JJ5TDjimgBCo02weRMAbrCaSHkaFEfQT6-veX6H3erSQAFokOCAGyFKEQwqlMEDTZdQpIBLSHBgibIGJ7BIoCjPBjDKAOJyoIZuXrwJDQXZU6m9dRabzSKSbPYkpm9ZLcPhsjlcno8iwKZSYSDsySc6Dc0XMdj8-iS6Wy+WiyCwNjiwUyrmkiDVJnG8x6eq7LqkFCtA6mSDwVqkB4xe0oACeiG8rUOe2egSq5t6x3IMR92kwZmpIlEjkZTMg-ok4Ulmjg7o10fI0jDNrtkcN5kgdlasFErslXAk32FhYsltg1ttskljudsFdIs1Hq9qpwkh9AcLprcQejEvcJbLFfccEQlMl5AkMiqwh7KagAEdxDYbQMba9V6TIBnI8XS+X7XPkGhJdB8KuoOvvNvd2tUK1DyOwKax5AACQUKQOIaJM0yzPMeT-OiqzrFsCS2nkQEgR80FgqIayPM8AAsCQAFYUHaIoiNIJYRlg1Q6EAA Open this visualization in the Vega Editor>

@
let enc = encoding
            . position X [ PRepeat Column, PmType Quantitative ]
            . position Y [ PRepeat Row, PmType Quantitative ]
            . color
                [ MSelectionCondition (SelectionName \"picked\")
                  [ MName \"Cluster\", MmType Nominal ]
                  [ MString \"grey\" ]
                ]

    sel = selection
            . select \"picked\" Interval [ ]

    trans = transform
            . calculateAs "cos(datum.RA_ICRS * PI / 180)" "cosRA"

    spec = asSpec
             [ gaiaData
             , trans []
             , mark Circle []
             , enc []
             , sel []
             ]

in toVegaLite
     [ repeat
       [ RowFields [ \"cosRA\", \"DE_ICRS\" ]
       , ColumnFields [ \"plx\", \"Gmag\" ]
       ]
     , specification spec
     ]
@

-}

coordinatedViews :: VegaLite
coordinatedViews :: VegaLite
coordinatedViews =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
              ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Arrangement -> PositionChannel
PRepeat Arrangement
Column, Measurement -> PositionChannel
PmType Measurement
Quantitative ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Arrangement -> PositionChannel
PRepeat Arrangement
Row, Measurement -> PositionChannel
PmType Measurement
Quantitative ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color
                  [ BooleanOp -> [MarkChannel] -> [MarkChannel] -> MarkChannel
MSelectionCondition (Text -> BooleanOp
SelectionName Text
"picked")
                    [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal ]
                    [ Text -> MarkChannel
MString Text
"grey" ]
                  ]

      sel :: [SelectSpec] -> PropertySpec
sel = [SelectSpec] -> PropertySpec
selection
              ([SelectSpec] -> PropertySpec)
-> ([SelectSpec] -> [SelectSpec]) -> [SelectSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> Selection -> [SelectionProperty] -> [SelectSpec] -> [SelectSpec]
select Text
"picked" Selection
Interval [ ]

      trans :: [TransformSpec] -> PropertySpec
trans = [TransformSpec] -> PropertySpec
transform
                ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> [TransformSpec] -> [TransformSpec]
calculateAs Text
"cos(datum.RA_ICRS * PI / 180)" Text
"cosRA"

      spec :: VLSpec
spec = [PropertySpec] -> VLSpec
asSpec
               [ PropertySpec
gaiaData
               , [TransformSpec] -> PropertySpec
trans []
               , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Circle []
               , [EncodingSpec] -> PropertySpec
enc []
               , [SelectSpec] -> PropertySpec
sel []
               ]

  in [PropertySpec] -> VegaLite
toVegaLite
       [ [RepeatFields] -> PropertySpec
repeat
         [ [Text] -> RepeatFields
RowFields [ Text
"cosRA", Text
"DE_ICRS" ]
         , [Text] -> RepeatFields
ColumnFields [ Text
"plx", Text
"Gmag" ]
         ]
       , VLSpec -> PropertySpec
specification VLSpec
spec
       ]


{-|

If the interval selection is bound the the axes with 'BindScales'
then we can zoom and pan the related plots - i.e. changing the
range displayed in one plot will also change the two plots that
it shares an axis with in this two by two arrangement. The conditional
encoding of the 'color' channel has also been removed.

<<images/vl/coordinatedviews2.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4IgTgpgDhCGAuIBcowHsDuyDaIBKAggPoCSAwngMogA0IAIgKKkXUC6dAxmgDYCuAWwB2OEFB4APWiADiA2AHMQbAL50AzjE7JQ8sAGtkITgEswnHhGkATBLB0g+YHkYAW8eFHVIA9D7CwGAB0CibwrnwARnzqEOZoQvAQiUHcAj70aHwKAEJO+hA+rgBuEAqwPvLqSWBFpeU+tvAV5SawALSwHQBsAIzdnb0ADO3NkZa9sEFCaO2ucNZxQfDqxdIAZmhg8ogoYrBgsQ5yikZCgpFx0oQsVGcXV3TiUkgg5wKXYNJMt9Sv759pBAiM97h8ripIRoIJZOPATAkHFATJwCtYHJETEJ0a91JxYJZ1NJ4ABPGBGLE1YoEkBQkDJbjWLFKPbcHhbBzrEwwnEgMj8aqPECk8n-NACLE0tQgF6gLk8hyQGAIIxswQiaUiqyvACOfFgiTCCBMpVpdBJnO5PBxqGgcF24EwZuFZO1ID1BvhzXhpsh0oAJHj5vI3B4vL4fPUpqFwlEggifEGIPJI2UOjwwoVigAWIIAK3UiJUQA Open this visualization in the Vega Editor>

The image was captured after panning and zooming in the
\"parallax-RA_ICRS\" plot.

@
let enc = encoding
            . position X [ PRepeat Column, PmType Quantitative ]
            . position Y [ PRepeat Row, PmType Quantitative ]
            . color [ MName \"Cluster\", MmType Nominal ]

    sel = selection
            . select \"picked\" Interval [ BindScales ]

    spec = asSpec
             [ gaiaData
             , mark Circle []
             , enc []
             , sel []
             ]

in toVegaLite
     [ repeat
       [ RowFields [ \"RA_ICRS\", \"DE_ICRS\" ]
       , ColumnFields [ \"plx\", \"Gmag\" ]
       ]
     , specification spec
     ]
@

The \"cosine\" transformation has been removed in comparison to
'coordinatedViews'.

-}

coordinatedViews2 :: VegaLite
coordinatedViews2 :: VegaLite
coordinatedViews2 =
  let enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
              ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Arrangement -> PositionChannel
PRepeat Arrangement
Column, Measurement -> PositionChannel
PmType Measurement
Quantitative ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Arrangement -> PositionChannel
PRepeat Arrangement
Row, Measurement -> PositionChannel
PmType Measurement
Quantitative ]
              BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal ]

      sel :: [SelectSpec] -> PropertySpec
sel = [SelectSpec] -> PropertySpec
selection
              ([SelectSpec] -> PropertySpec)
-> ([SelectSpec] -> [SelectSpec]) -> [SelectSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> Selection -> [SelectionProperty] -> [SelectSpec] -> [SelectSpec]
select Text
"picked" Selection
Interval [ SelectionProperty
BindScales ]

      spec :: VLSpec
spec = [PropertySpec] -> VLSpec
asSpec
               [ PropertySpec
gaiaData
               , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Circle []
               , [EncodingSpec] -> PropertySpec
enc []
               , [SelectSpec] -> PropertySpec
sel []
               ]

  in [PropertySpec] -> VegaLite
toVegaLite
       [ [RepeatFields] -> PropertySpec
repeat
         [ [Text] -> RepeatFields
RowFields [ Text
"RA_ICRS", Text
"DE_ICRS" ]
         , [Text] -> RepeatFields
ColumnFields [ Text
"plx", Text
"Gmag" ]
         ]
       , VLSpec -> PropertySpec
specification VLSpec
spec
       ]


{-|

The ability to determine the scale of a chart based on a selection is
useful in implementing a common visualization design pattern, that of
'context and focus' (or sometimes referred to as 'overview and detail
on demand'). We can achieve this by setting the scale of one view
based on the selection in another. The detail view is updated whenever
the selected region is changed through interaction:

<<images/vl/contextandfocus.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAmCGAutIC4yghSBXATgGxSgAt54AHAZ2QHpqdYB3AOgHMBLeIrAIywoFMcAYwD2AO3j8JTUQFtqAERFYWAIVwBrftSIA3fi1jVZsCpJw79h6nETVDbWAFpYzgGwBGNy48AGJ4jcePwesExiIk5E-LDQgkzwFLqQADTgGJAAZiI4JvCE6BiYZLA4AgXpRZgA4iYshJBiWLLcgqmVVZAASgCCAPoAkgDCXQDKDU0tbWlVGWR4AB4Tza047bOYCgCigyPjqI0r0x1FkPx980sHk6uQJ2AAvh1PEA8zULqiYkIIhADaHUKp2ibBYJEIAA5fO9TiYcBoGmQRGwJOtOgw2NBOIQACy+aH3SACYJCeBscQFSDcHB8IiUqSiaAolgUf6QACekAAuikoPB2WR+A0UeZdLACA83oSyfBgg1RvwSfAwLAwABNMD0MQsfhgeAiMAALxEIlkYBRYFaeBEDDRpwZIiZ2oqGygVzQ91OmTYiugDVqsHqMI2RJ+ctQwEghsEIkImXFAilrsw-MFDQAjlhYBIOAg2Po7q6kyHORGsj68H6DpdUnyBUKDpnszK8wWXlV269g0CMnCEdXkajg5gMVi6ag8QTZmdvo7mS6Q6JrWsy97fQ0hng+OZa5BUw2oBFZCjxZBi9P3T3p2vK-66naQxQwwfI9GcLHUPG8Inh519xmsxzRAyQLX9TlgBY2FZMsZXDMAmjwPBOw7MCoFLD1kygG8qygGtUMwJ9xRfGBTVgFFKWJfhSXJMQGmpWkz3PEN-0bQCWxAoVPUee5kPbHl0kgAASJ9ohMBoSHIKhaCsMJ2E4HgmHJagRP4ExqBkpw8A4bRdBxJgACsKApd5IC+b16gjJ4HiAA Open this visualization in the Vega Editor>

@
let sel = selection . select \"brush\" Interval [ Encodings [ ChY ] ]

    encContext = encoding
                   . position X [ PName \"Gmag\", PmType Quantitative, PScale [ SZero False ] ]
                   . position Y [ PName \"plx\", PmType Quantitative ]

    specContext = asSpec [ width 400
                         , height 80
                         , sel []
                         , mark Point []
                         , encContext []
                         , title \"Select a Y range to zoom in below\" []
                         ]

    encDetail = encoding
                  . position X [ PName \"Gmag\"
                               , PmType Quantitative
                               , PScale [ SZero False ]
                               , PAxis [ 'AxNoTitle' ]
                               ]
                  . position Y [ PName \"plx\"
                               , PmType Quantitative
                                 -- prior to 0.11.0.0 this was 'SDomain'
                               , PScale [ 'SDomainOpt' ('DSelection' \"brush\") ]
                               ]
                  . color [ MName \"Cluster\", MmType Nominal ]

  specDetail =
      asSpec [ width 400, mark Point [], encDetail [] ]

in toVegaLite
     [ gaiaData
     , vConcat [ specContext, specDetail ]
     ]
@

Not shown here, but selecting a range of y-values in the top plot
(@specContext@) will cause the second plot (@specDetail@) to zoom
in on that range, as the selection is bound to the y axis of this
plot via 'DSelection'.

-}

contextAndFocus :: VegaLite
contextAndFocus :: VegaLite
contextAndFocus =
  let
    sel :: [SelectSpec] -> PropertySpec
sel =
        [SelectSpec] -> PropertySpec
selection ([SelectSpec] -> PropertySpec)
-> ([SelectSpec] -> [SelectSpec]) -> [SelectSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> Selection -> [SelectionProperty] -> [SelectSpec] -> [SelectSpec]
select Text
"brush" Selection
Interval [ [Channel] -> SelectionProperty
Encodings [ Channel
ChY ] ]

    encContext :: [EncodingSpec] -> PropertySpec
encContext =
        [EncodingSpec] -> PropertySpec
encoding
            ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Gmag", Measurement -> PositionChannel
PmType Measurement
Quantitative, [ScaleProperty] -> PositionChannel
PScale [ Bool -> ScaleProperty
SZero Bool
False ] ]
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"plx", Measurement -> PositionChannel
PmType Measurement
Quantitative ]

    specContext :: VLSpec
specContext =
        [PropertySpec] -> VLSpec
asSpec [ Double -> PropertySpec
width Double
400, Double -> PropertySpec
height Double
80, [SelectSpec] -> PropertySpec
sel [], Mark -> [MarkProperty] -> PropertySpec
mark Mark
Point [], [EncodingSpec] -> PropertySpec
encContext []
               , Text -> [TitleConfig] -> PropertySpec
title Text
"Select a Y range to zoom in below" []
               ]

    encDetail :: [EncodingSpec] -> PropertySpec
encDetail =
        [EncodingSpec] -> PropertySpec
encoding
            ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X
                [ Text -> PositionChannel
PName Text
"Gmag"
                , Measurement -> PositionChannel
PmType Measurement
Quantitative
                , [ScaleProperty] -> PositionChannel
PScale [ Bool -> ScaleProperty
SZero Bool
False ]
                , [AxisProperty] -> PositionChannel
PAxis [ AxisProperty
AxNoTitle ]
                ]
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"plx"
                         , Measurement -> PositionChannel
PmType Measurement
Quantitative
                         , [ScaleProperty] -> PositionChannel
PScale [ ScaleDomain -> ScaleProperty
SDomainOpt (Text -> ScaleDomain
DSelection Text
"brush") ]
                         ]
                  BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal ]

    specDetail :: VLSpec
specDetail =
        [PropertySpec] -> VLSpec
asSpec [ Double -> PropertySpec
width Double
400, Mark -> [MarkProperty] -> PropertySpec
mark Mark
Point [], [EncodingSpec] -> PropertySpec
encDetail [] ]

  in
    [PropertySpec] -> VegaLite
toVegaLite
      [ PropertySpec
gaiaData
      , [VLSpec] -> PropertySpec
vConcat [ VLSpec
specContext, VLSpec
specDetail ]
      ]


-- $intro-crossfilter
-- The final example in this section brings together ideas of view
-- composition and interactive selection with data filtering by implementing
-- /cross-filtering/: the selection of a subset of the data in one
-- view then only displaying that data in the other views.

{-|

Here we show distributions of the four main numeric quantities
in the dataset - position, magnitude, and prallax - using the
@totalEnc@ encoding, and add a second layer which repeats this
data but with a different color (@selectedEnc@), and that is tied to
the interval-selection along the x axis ('ChX').

<<images/vl/crossfilter.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEBOCmAOsCGAXSAuMxIGMD2ANgK4C2AdhmANqQBKAggPoCSAwrQMqQA0UAIgFEW7Lr0gBxEkgDmPKPAIAPSAF0Avt3BQAzohyVQECJAAmqJAa1GoRaAUqQAFihTxt6APQfoSAO4A6aQBLFEciACMibVhofDIUWHj-fBIPPjwiaQAhWwBrWA9HADdYaSQPKW0E6EKSso8zFHKyoKQAWiR2gDYARi6OnoAGNqbwglgepH8yPDbHZBMY-xRtIp4rI0gAMzxoKTRMQ2tN+CRoaMtj44kpWUxIMlJwmPWr6zomNk4HR5Jn6Feb2MCmU91+-0BQMggmE3zBTxemiBxlgjBBPwRAI21jU2LAuJxSM2BCQAE8XpgqHijldIFJoLkHOEzpDrol8CYgmQ7lg8e9QVhtkFYAQTAYYAhkAcoPhiORIBooChSYgHABHIhIeIhVBBEoKolQ0nimTSOBlBIOfBEeJySDK1X3DValA6136glvT2E6l8pU+MjaHZ7ShUbBbIIEari6LjHCuvAUe7haBRRwKtQqQ3XemM5Ms7PvWOweNBRPilNp8XsvCc7naUOQZRZpUq2AOLnVIpIexqDR+yA1us8mlvXCEXbi7vEdv3aSERZkaC1g1+4wC8PC0XiuCIVBWwikCiK+1t9Wa7VNd3t-vIqDGw6QU3m1CzmUZW1iB1vyDOy+6j0-W9IxgJULRbygAASbQcHmKQHGcVx3C8OopmCUIIn8MsPBguDylQtoCBCAoigAFn8AArbRyyJccyAjEdcTUIA Open this visualization in the Vega Editor>

Selecting a small range of parallax values in the fourth plot
highlights the associated data in the other three plots.

@
let sel = selection . select "brush" Interval [ Encodings [ ChX ] ]

    filterTrans = transform . filter ('FSelection' "brush")

    -- borrow a function from Elm
    pQuant = PmType Quantitative

    totalEnc = encoding
                . position X [ PRepeat Column, pQuant ]
                . position Y [ PAggregate Count, pQuant ]

    selectedEnc = totalEnc
                    . color [ MString "goldenrod" ]

in toVegaLite
     [ repeat [ ColumnFields [ \"RA_ICRS\", \"DE_ICRS\", \"Gmag\", \"plx\" ] ]
     , specification $
         asSpec
           [ gaiaData
           , layer
             [ asSpec [ mark Bar [], totalEnc [] ]
             , asSpec [ sel [], filterTrans [], mark Bar [], selectedEnc [] ]
             ]
           ]
     ]
@

-}

crossFilter :: VegaLite
crossFilter :: VegaLite
crossFilter =
 let
    sel :: [SelectSpec] -> PropertySpec
sel =
        [SelectSpec] -> PropertySpec
selection ([SelectSpec] -> PropertySpec)
-> ([SelectSpec] -> [SelectSpec]) -> [SelectSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> Selection -> [SelectionProperty] -> [SelectSpec] -> [SelectSpec]
select Text
"brush" Selection
Interval [ [Channel] -> SelectionProperty
Encodings [ Channel
ChX ] ]

    filterTrans :: [TransformSpec] -> PropertySpec
filterTrans =
        [TransformSpec] -> PropertySpec
transform
            ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Filter -> [TransformSpec] -> [TransformSpec]
filter (Text -> Filter
FSelection Text
"brush")

    -- borrow a function from Elm
    pQuant :: PositionChannel
pQuant = Measurement -> PositionChannel
PmType Measurement
Quantitative

    totalEnc :: [EncodingSpec] -> PropertySpec
totalEnc =
        [EncodingSpec] -> PropertySpec
encoding
            ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Arrangement -> PositionChannel
PRepeat Arrangement
Column, PositionChannel
pQuant ]
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Operation -> PositionChannel
PAggregate Operation
Count, PositionChannel
pQuant ]

    selectedEnc :: [EncodingSpec] -> PropertySpec
selectedEnc =
        [EncodingSpec] -> PropertySpec
totalEnc
            ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MString Text
"goldenrod" ]

 in
 [PropertySpec] -> VegaLite
toVegaLite
    [ [RepeatFields] -> PropertySpec
repeat [ [Text] -> RepeatFields
ColumnFields [ Text
"RA_ICRS", Text
"DE_ICRS", Text
"Gmag", Text
"plx" ] ]
    , VLSpec -> PropertySpec
specification (VLSpec -> PropertySpec) -> VLSpec -> PropertySpec
forall a b. (a -> b) -> a -> b
$
        [PropertySpec] -> VLSpec
asSpec
            [ PropertySpec
gaiaData
            , [VLSpec] -> PropertySpec
layer
                [ [PropertySpec] -> VLSpec
asSpec [ Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar [], [EncodingSpec] -> PropertySpec
totalEnc [] ]
                , [PropertySpec] -> VLSpec
asSpec [ [SelectSpec] -> PropertySpec
sel [], [TransformSpec] -> PropertySpec
filterTrans [], Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar [], [EncodingSpec] -> PropertySpec
selectedEnc [] ]
                ]
            ]
    ]


-- $intro-smoothing
-- Vega Lite 4 introduces several ways to \"smooth\" or \"fit\" your
-- data. I've already played around with kernel-density estimation
-- - via the 'density' transform that was used in 'densityParallax' -
-- so now I get to try out 'loess' and 'regression'.

{-|

The 'loess' transform will generate new coordinate pairs for the
independent and dependent values based on an existing pair.
The name stands for
<https://vega.github.io/vega-lite/docs/loess.html locally-estimated scatterplot smoothing>,
and here I use it to look for any possible relationship between the
magnitude and parallax of each star in a cluster. I don't expect
there to really be any (as we've seen before, the distribution
is pretty flat), but it's the data I have to play with in this
tutorial.

<<images/vl/loessexample.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4IgLgTghgdgzgMwPYQLYgFwG1QIJYA2YAphJiABQAmUYArqgHQAiAogPoCSAwgEoDKAAgB8AXkEAGAJSCAZIOq0GLDjwGCAPOIAs0kAF8AugBoQNMFEyg6EAuQAWYMAAc4GAPTvoAd0YBzPDB7OgAjOjhSAGMkGBJYxmjUd2YkOj8AIRsAa2J3ewA3Yj8od1QoOBIIPMLi93MS4rwoAFooFoA2AEZ21s6JZosQgmJOqEYYJGb7YigqUkYwOHyQU2Q0WisQZygICM2AcTK-chgGENIVkF4AQS4+fhOzi9NnAgAPR9RzslM2O4FPt9LsR2K8PhgQKcvhd9LDTHBnMRIptpng-I5MAAmACsElM3jwVCCWNxpgIUAAnhdsKAyhAsuRnEg8LFgTBolQWccMKBogQUJt8MQCFRyNwCOFKpdhn5iDBRRhTgQCPpTODcHhhQqQIcoMd4ZEoMNNgAvUhITAII0RVXgCmI8gARzosDAgVoeEKBlMFMFmpFjPelzghuNPJAZogFowVoINtMYHtxCdLti7rdXthqtAkFgiBQ6BpID8UbozhCvuwIHFkouJhA-OIcDggY+phi5F1xyMpjpDPDFSjOQA6oTiRhMfDIEgcuQhlBIgyE0nyAQWcnbXKOVzNuqQEKAxCu8HQ8nw5Ho7H43aHRDna7056Nz6-VrWyejWfQBfLdbiLbE1vEB7zTCwMw3WFDFtAASENpjKBwnFcDx3BqMYAiCUJGDwJB3Dg4gylQooWjXEhUO0RgACs4A7Uw+QYeAsVWBdiDAV9D2rCUKmeG8z0hJBUBZI0DH0IA Open this visualization in the Vega Editor>

@
let simplify = transform
               . filter (FExpr \"(datum.DE_ICRS >= 0) & (datum.DE_ICRS <= 40)\")

    baseEnc = encoding
             . position X [ PName \"Gmag\"
                          , PmType Quantitative
                          , PScale [ SZero False ]
                          ]
             . position Y [ PName \"plx\"
                          , PmType Quantitative
                          , PScale [ SZero False ]
                          ]

    rawEnc = baseEnc
            . color [ MName \"Cluster\"
                    , MLegend []
                    ]

    rawLayer = asSpec [ rawEnc [], mark Point [] ]

    trans = transform
            . 'loess' \"plx\" \"Gmag\" [ 'LsGroupBy' [ \"Cluster\" ] ]

    trendLayer = asSpec [ trans []
                        , baseEnc []
                        , mark Line [ MStroke \"black\"
                                    , MStrokeWidth 2
                                    ]
                        ]

    frameSpec = asSpec [ width 250
                       , height 250
                       , layer [ rawLayer, trendLayer ] ]

in toVegaLite
     [ gaiaData
     , simplify []
     , columns 2
     , facetFlow [ FName \"Cluster\", FmType Nominal ]
     , specification frameSpec
     ]
@

The data is filtered to select only four clusters, ensuring that
the two closest (i.e. they have the largest parallax values) are
included as they are likely to be the most-interesting to look
at (because of the spread of parallax values).

The 'LsGroupBy' option is used to ensure the calculation is done
per cluster, and then multiple layers are used to compare the
raw with the "smoothed" data in a faceted display.

-}

loessExample :: VegaLite
loessExample :: VegaLite
loessExample =
  let simplify :: [TransformSpec] -> PropertySpec
simplify = [TransformSpec] -> PropertySpec
transform
                 ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Filter -> [TransformSpec] -> [TransformSpec]
filter (Text -> Filter
FExpr Text
"(datum.DE_ICRS >= 0) & (datum.DE_ICRS <= 40)")

      baseEnc :: [EncodingSpec] -> PropertySpec
baseEnc = [EncodingSpec] -> PropertySpec
encoding
                ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Gmag"
                             , Measurement -> PositionChannel
PmType Measurement
Quantitative
                             , [ScaleProperty] -> PositionChannel
PScale [ Bool -> ScaleProperty
SZero Bool
False ]
                             ]
                BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"plx"
                             , Measurement -> PositionChannel
PmType Measurement
Quantitative
                             , [ScaleProperty] -> PositionChannel
PScale [ Bool -> ScaleProperty
SZero Bool
False ]
                             ]

      rawEnc :: [EncodingSpec] -> PropertySpec
rawEnc = [EncodingSpec] -> PropertySpec
baseEnc
               ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster"
                       , [LegendProperty] -> MarkChannel
MLegend []
                       ]

      rawLayer :: VLSpec
rawLayer = [PropertySpec] -> VLSpec
asSpec [ [EncodingSpec] -> PropertySpec
rawEnc [], Mark -> [MarkProperty] -> PropertySpec
mark Mark
Point [] ]

      trans :: [TransformSpec] -> PropertySpec
trans = [TransformSpec] -> PropertySpec
transform
              ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> Text -> [LoessProperty] -> [TransformSpec] -> [TransformSpec]
loess Text
"plx" Text
"Gmag" [ [Text] -> LoessProperty
LsGroupBy [ Text
"Cluster" ] ]

      trendLayer :: VLSpec
trendLayer = [PropertySpec] -> VLSpec
asSpec [ [TransformSpec] -> PropertySpec
trans []
                          , [EncodingSpec] -> PropertySpec
baseEnc []
                          , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Line [ Text -> MarkProperty
MStroke Text
"black"
                                      , Double -> MarkProperty
MStrokeWidth Double
2
                                      ]
                          ]

      frameSpec :: VLSpec
frameSpec = [PropertySpec] -> VLSpec
asSpec [ Double -> PropertySpec
width Double
250
                         , Double -> PropertySpec
height Double
250
                         , [VLSpec] -> PropertySpec
layer [ VLSpec
rawLayer, VLSpec
trendLayer ] ]

  in [PropertySpec] -> VegaLite
toVegaLite
       [ PropertySpec
gaiaData
       , [TransformSpec] -> PropertySpec
simplify []
       , Natural -> PropertySpec
columns Natural
2
       , [FacetChannel] -> PropertySpec
facetFlow [ Text -> FacetChannel
FName Text
"Cluster", Measurement -> FacetChannel
FmType Measurement
Nominal ]
       , VLSpec -> PropertySpec
specification VLSpec
frameSpec
       ]


{-|

This is the same data as 'loessExample', but using a linear
regression model to try and explain the data. Practically,
the only things that have changed are switching from
'loess' to 'regression', and displaying all the data in
a single visualization.

<<images/vl/regressionexample.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4IgLgTghgdgzgMwPYQLYgFwG1QIJYA2YAphJiABQAmUYArqgHQAiAogPoCSAwgEoDKAAgB8AXkEAGAJSCAZIOq0GLDjwGCAPOIAs0kAF8AugBoQAC2J4A5mbCYAzBImmaYKJlB0IBcrbAAHOAwAemDoAHdGKzwwMzoAIzo4UgBjJBgSDMY01GDmJDorACEvAGtiYLMAN2IrKGDUKDgSCEqauuDXerq8KABaKH6ANgBGIYGRiT63eIJiEahGGCQ+iygqUkYwOCqQU2Q0Wg8QfygIZOOAcUarchgGeNI9kF4AQS4+fjuHp9N-AgAHt9UI8yKY2B8BMDQc9iOx-kCMCB7iCnvp0aZwngqLEHE5TAASOApCyNXxgAJBULtRbRWIJRh4JDBYmk+o0voEGIVKraRgAKzg6WeBCgAE8nthQI0IKVyP4kHgMrCYGkqErbhhQGkCChjvhiAQqORuAQki0DKZEbg8IbjUjrlBbqZiVA5scAF6kJCYBBu5L6UxgMX+YjkACOdFgYBitDwNUtIDF+ttRvlgOervdWpAXogPowfoIAaDIbDSMj0djMYT6MDoEgsEQKHQUpAVnzdH88WT2BApvNTxMIAgtVHcDgTJg6aBpmFDpuBmHMrlOea+fKAHVsbiMAAmF2QJDlcizKApOWl0PkLkwMOBkDEVVIdUwTXapC6sg5g1ppED5o0StFM7XIR1nRALNy1APMCyLEtwDLCMowyat43vUxextUCkQRTMUjdaDc29X1-WIB9g2vCsUJjNwa3vdFDH0IA Open this visualization in the Vega Editor>

@
let simplify = transform
               . filter (FExpr \"(datum.DE_ICRS >= 0) & (datum.DE_ICRS <= 40)\")

    axis pos lbl = position pos [ PName lbl
                                , PmType Quantitative
                                , PScale [ SZero False ]
                                ]
    enc = encoding
          . axis X \"Gmag\"
          . axis Y \"plx\"
          . color [ MName \"Cluster\" ]

    rawLayer = asSpec [ enc [], mark Point [] ]

    trans = transform
            . 'regression' \"plx\" \"Gmag\" [ 'RgGroupBy' [ \"Cluster\" ] ]

    trendLayer = asSpec [ trans []
                        , enc []
                        , mark Line [ MStroke \"black\"
                                    , MStrokeWidth 2
                                    ]
                        ]

in toVegaLite
     [ width 300
     , height 300
     , gaiaData
     , simplify []
     , layer [ rawLayer, trendLayer ]
     ]
@

In this example I used the default method - 'RgLinear' - but other
options are possible (set with the 'RgMethod' option).

-}

regressionExample :: VegaLite
regressionExample :: VegaLite
regressionExample =
  let simplify :: [TransformSpec] -> PropertySpec
simplify = [TransformSpec] -> PropertySpec
transform
                 ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Filter -> [TransformSpec] -> [TransformSpec]
filter (Text -> Filter
FExpr Text
"(datum.DE_ICRS >= 0) & (datum.DE_ICRS <= 40)")

      axis :: Position -> Text -> BuildEncodingSpecs
axis Position
pos Text
lbl = Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
pos [ Text -> PositionChannel
PName Text
lbl
                                  , Measurement -> PositionChannel
PmType Measurement
Quantitative
                                  , [ScaleProperty] -> PositionChannel
PScale [ Bool -> ScaleProperty
SZero Bool
False ]
                                  ]
      enc :: [EncodingSpec] -> PropertySpec
enc = [EncodingSpec] -> PropertySpec
encoding
            ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> Text -> BuildEncodingSpecs
axis Position
X Text
"Gmag"
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> Text -> BuildEncodingSpecs
axis Position
Y Text
"plx"
            BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster" ]

      rawLayer :: VLSpec
rawLayer = [PropertySpec] -> VLSpec
asSpec [ [EncodingSpec] -> PropertySpec
enc [], Mark -> [MarkProperty] -> PropertySpec
mark Mark
Point [] ]

      trans :: [TransformSpec] -> PropertySpec
trans = [TransformSpec] -> PropertySpec
transform
              ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> Text
-> [RegressionProperty]
-> [TransformSpec]
-> [TransformSpec]
regression Text
"plx" Text
"Gmag" [ [Text] -> RegressionProperty
RgGroupBy [ Text
"Cluster" ] ]

      trendLayer :: VLSpec
trendLayer = [PropertySpec] -> VLSpec
asSpec [ [TransformSpec] -> PropertySpec
trans []
                          , [EncodingSpec] -> PropertySpec
enc []
                          , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Line [ Text -> MarkProperty
MStroke Text
"black"
                                      , Double -> MarkProperty
MStrokeWidth Double
2
                                      ]
                          ]

  in [PropertySpec] -> VegaLite
toVegaLite
       [ Double -> PropertySpec
width Double
300
       , Double -> PropertySpec
height Double
300
       , PropertySpec
gaiaData
       , [TransformSpec] -> PropertySpec
simplify []
       , [VLSpec] -> PropertySpec
layer [ VLSpec
rawLayer, VLSpec
trendLayer ]
       ]


-- XXX TODO: add an example showing R2 in plot title


-- $intro-error
-- Here we dive into some of the ways for representing the spread
-- of a value, focussing on the \"error\" of a variable.

{-|
We have already seen "error bars" in the 'layeredDiversion' plot, where
the 'Rule' type was used to draw a line between the 'Y' and 'Y2'
encodings. In that example the two positions were calculated \"on the
fly\" by Vega-Lite (using the 'Min' and 'Max' aggregation operations).
In this example I use the data to calculate the display range,
namely @plx - e_plx@ to @plx + e_plx@. These are mapped to the
'X' and 'X2' channels (not because it makes a better visualization,
but just to show you can create lines along the x axis), and a
small-multiples approach is used to separate out the clusters, but
only after a filter designed to select the two clusters - with the
\"most interesting\" data for this plot - has been applied.

<<images/vl/errormanual.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1wIxhJUBLAG2gFNY8oATeaAVywDoBhKt5OrAIAGALpgAvBLABybjLAAfRWBbsuvfoJHipsgBIzIAXwA0xCGXjJGkAA5UAHgH0q6SKagBjeFS9sqVlpbNQ5OB0cwAFpVVjDaZwiTcxJSSGtbCOcACwoPb19-QLoQuK4IsABqWPVOBKTjYlEUqCx4WABrW1gA4JbINXhGUFTINlgqW2zoaDtkXAB6BYQAd04AcwpobLYAI356L3REOhPOI6wFgBF0NnWAIXGO2gXsgDdadfgFtoF6V4+XwWgwWXwo8Ci8AhADYAIzQyGw4RRaDwXZUWiw+CcRDoKLZWjwJj0TjQZBvDwWKAYbCsYZUiD2drIYL4EapEiQADibXWtkQHF29EpHM5ACUAILOACS3DFAGV+YLhS1RfYnEqsEKGKqOZArgBRGVyxX4SACrUqhmc+oas0W7WQa2NVIusBmYiQAAkyC8BLaUxmc0WC0B2M22z2nAo6AWvv93zDUSoWxebwALJwAFbIY4iqAsjFeaAxxDDewULzPJjl3YURA1s2+3y0GyeGAATzsrKg9cEb18Jg9jNoiCOTHrfLZVMgjgATOXKLQqI2oFlcskZ456WqlyvMk5XO5dYzmxjywAvejufCoXws4d66BdnuQACObCQJdRJY++dG8COBQNhsjAWznmadjtL4gSRAAFL8ACUJhUo+jJHHwWBlqBe6rpAmh-DqUDPt2-LoFg9aDmhUAdouFDLnhPLwHy7Yka+H5flsrAUH+bqPpARyIJQU6kI0xhAA Open this visualization in the Vega Editor>

@
let trans = transform
              . filter (FExpr \"datum.Cluster[0] == \'C\' || datum.Cluster[0] == \'H\'\")
              . calculateAs \"datum.plx - datum.e_plx\" \"plx_lo\"
              . calculateAs \"datum.plx + datum.e_plx\" \"plx_hi\"

    errorEnc = encoding
                 . position X [ PName \"plx_lo\"
                              , PmType Quantitative
                              , PScale [SZero False]
                              , PAxis [ AxTitle "parallax (mas)" ]
                              ]
                 . position 'X2' [ PName \"plx_hi\" ]
                 . position Y [ PName \"Gmag\", PmType Quantitative ]
                 . column [ FName \"Cluster\", FmType Nominal ]

    sel = selection
            . select "picked" Interval [ BindScales ]

in toVegaLite [ gaiaData
              , trans []
              , errorEnc []
              , mark Rule []
              , sel []
              ]
@

For the interested reader, it was the calculation of the @\"plx_hi\"@
column that lead me to the discovery that the columns were being
read in as a string, and the introduction of the 'Parse' option
to 'gaiaData'.

As can be seen, the @e_plx@ terms are generally very small. This is
good for anyone using the data, as we want precise measurements, but
makes it harder for me to come up with meaningful visualizations! I
have taken advantage of the 'BindScales' interaction to zoom in on
a subset of stars which show larger parallax errors:

<<images/vl/errormanual-zoomed.png>>

-}

errorManual :: VegaLite
errorManual :: VegaLite
errorManual =
  let trans :: [TransformSpec] -> PropertySpec
trans = [TransformSpec] -> PropertySpec
transform
                ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Filter -> [TransformSpec] -> [TransformSpec]
filter (Text -> Filter
FExpr Text
"datum.Cluster[0] == 'C' || datum.Cluster[0] == 'H'")
                ([TransformSpec] -> [TransformSpec])
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> [TransformSpec]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> [TransformSpec] -> [TransformSpec]
calculateAs Text
"datum.plx - datum.e_plx" Text
"plx_lo"
                ([TransformSpec] -> [TransformSpec])
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> [TransformSpec]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> [TransformSpec] -> [TransformSpec]
calculateAs Text
"datum.plx + datum.e_plx" Text
"plx_hi"

      errorEnc :: [EncodingSpec] -> PropertySpec
errorEnc = [EncodingSpec] -> PropertySpec
encoding
                  ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"plx_lo"
                               , Measurement -> PositionChannel
PmType Measurement
Quantitative
                               , [ScaleProperty] -> PositionChannel
PScale [Bool -> ScaleProperty
SZero Bool
False]
                               , [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"parallax (mas)" ]
                               ]
                  BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X2 [ Text -> PositionChannel
PName Text
"plx_hi" ]
                  BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"Gmag", Measurement -> PositionChannel
PmType Measurement
Quantitative ]
                  BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FacetChannel] -> BuildEncodingSpecs
column [ Text -> FacetChannel
FName Text
"Cluster", Measurement -> FacetChannel
FmType Measurement
Nominal ]

      sel :: [SelectSpec] -> PropertySpec
sel = [SelectSpec] -> PropertySpec
selection
              ([SelectSpec] -> PropertySpec)
-> ([SelectSpec] -> [SelectSpec]) -> [SelectSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text
-> Selection -> [SelectionProperty] -> [SelectSpec] -> [SelectSpec]
select Text
"picked" Selection
Interval [ SelectionProperty
BindScales ]

  in [PropertySpec] -> VegaLite
toVegaLite [ PropertySpec
gaiaData
                , [TransformSpec] -> PropertySpec
trans []
                , [EncodingSpec] -> PropertySpec
errorEnc []
                , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Rule []
                , [SelectSpec] -> PropertySpec
sel []
                ]

{-|

Alternatively, I could have made life simpler for myself and used
the 'ErrorBar' (or 'ErrorBand') mark type, together with 'XError'
(or 'YError') to indicate that the channel gives the offset from
the central value. For this visualization I restrict to a single
cluster (since I now know there's only one in this sample which
begins with @C@), but retain the 'column' encoding as a means
to getting a useful title. I've also switched things so that the
errors are back along the y axis.

<<images/vl/errorauto.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1hJUBLAG2gFNY8oATeaAVywDoBhc55a2AgAwBdMAF5RYAOQdJkAL5CANOChZ4sANZ1INWJgBG6yMoiRG0eHVARTzWOW0ALaNAAOyXAHpPCAO5sAc1JoR2Z9HhoAY3REali2aKxPABF0ZgCAITsNKk9HADcqAPhPNV4aPMLiz3MS4tJ4AFp4JoA2AEZW5vaBRot9cip2+DZEdEbHKnh6GjZoZHzjFVMMbCYrZZtIV3VkKg2bQ6gAcTUA7URWfRolo62AJQBBAH0ASQ57gGULq5uTO6grnIAA8flhrrR-ndIMkAKJvD7ffCQS7gv6bQ46Z5A0HI1EQyAYsByTYkiByf6QAAkyEikzUThc7i8niqIyCITCbFI6E8tPpJTZjXIwVy+QALGwAFbIGK3KBURDReikRDnfDWLYAT1hsD0tA1JFIVHI9G0VGxIPkUKguLAxDIJrNyNO8HOiig0C1rn2yIAjswkNBgkxSIVrZtINFuFhEFYjU7tFweHxjJ7vb6oGMsKr4A4KZGtQcjgnTdocfLMbS85niAAvGjoOioPN7AvQr0+7QBoMh4Phm2meDA0jIePB6CDcvqPPkYdgAAUZQAlPJSSp21BoogyOr7SS5EA Open this visualization in the Vega Editor>

@
let trans = transform
              . filter (FExpr \"datum.Cluster[0] == \'C\'\")

    errorEnc = encoding
                . position Y [ PName \"plx\"
                             , PmType Quantitative
                             , PScale [SZero False]
                             , PAxis [ AxTitle \"parallax (mas)\" ]
                             ]
                . position 'YError' [ PName \"e_plx\" ]
                . position X [ PName \"Gmag\", PmType Quantitative ]
                . column [ FName \"Cluster\", FmType Nominal ]

in toVegaLite [ gaiaData
              , trans []
              , errorEnc []
              , mark 'ErrorBar' []
              ]
@
-}

errorAuto :: VegaLite
errorAuto :: VegaLite
errorAuto =
  let trans :: [TransformSpec] -> PropertySpec
trans = [TransformSpec] -> PropertySpec
transform
                ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Filter -> [TransformSpec] -> [TransformSpec]
filter (Text -> Filter
FExpr Text
"datum.Cluster[0] == 'C'")

      errorEnc :: [EncodingSpec] -> PropertySpec
errorEnc = [EncodingSpec] -> PropertySpec
encoding
                  ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"plx"
                               , Measurement -> PositionChannel
PmType Measurement
Quantitative
                               , [ScaleProperty] -> PositionChannel
PScale [Bool -> ScaleProperty
SZero Bool
False]
                               , [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"parallax (mas)" ]
                               ]
                  BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
YError [ Text -> PositionChannel
PName Text
"e_plx" ]
                  BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Gmag", Measurement -> PositionChannel
PmType Measurement
Quantitative ]
                  BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [FacetChannel] -> BuildEncodingSpecs
column [ Text -> FacetChannel
FName Text
"Cluster", Measurement -> FacetChannel
FmType Measurement
Nominal ]

  in [PropertySpec] -> VegaLite
toVegaLite [ PropertySpec
gaiaData
                , [TransformSpec] -> PropertySpec
trans []
                , [EncodingSpec] -> PropertySpec
errorEnc []
                , Mark -> [MarkProperty] -> PropertySpec
mark Mark
ErrorBar []
                ]



{-|

In this plot the error range is calculated by Vega-Lite,
and is taken from the standard deviation of the @Gmag@ field
('StdDev'). The 'MTicks' and 'MRule' constructors are used to
color the different parts of the error bars. Since the error bar
does not reference the central value, a separate layer is used
to add a square symbol ('SymSquare') at the average ('Mean')
value of the distribution.

<<images/vl/errorbars.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAWCmCWBzaAXSAuMBmADDgNOFACYCGKpGYoEEkArgE4A2VMKKADgM7oD0fRqQDuAOkTwU0egCN63WIwDGAewB2KWBtGqAtnwAiK+ogBCTANaw+0AG6xEpPrtLdNjG-cd8yFPo-hSAFpSYIA2AEYwkIicIIoZZlgI0lE1FSC4UmJFURRuW0hCWigAMxVGFzRMGhKoTlJGBSpauqgAcRdENjV6XRlFIqI2yAAlAEEAfQBJAGFRgGUevoHGIba6TmYAD2X+weKNyAMAURn5pcxIXv21w5HYSa3dq5vVyGHaAF9Pn4gvw6QYTwYhSKi4AhESAAEm4SjgLjYqC4vAEXlSEikslE8BUfDhCKc6KCzEk1lsABZRAArbjqdZQZikACeg0wAG1Pq1aJAXIwLC1PiVIIx6EkWpBVMwKmxNKRWAChTzYNtNBo2G4coV7nVIChmZxYGxFIwKjJGgyRih4EoLNwJVKZVdOEwtka-nVFQ81KpiPA1N0akq6MyJaV4LBmMQ2J1SN18FA4fKjTVIAAvRQqKileUKAFQfWGtgAR3opA0knI8HskA9JTrYC9tG5dD5AtT3HgGaoACYCInoKQi1duKXGkaE3qDSn6ip-Wgm8KtL7-YHqMGoKHU+HI9GrrH41A44hGA5yDPebBy0UC9OS2WKxRrTWG-9fkQALqA5cqP0BiUvNQkA7lGbCzMw8juDeU7DlA6S6P68q1k2krqOGa7AD8XxAA Open this visualization in the Vega Editor>

@
let cluster = position X [ PName \"Cluster\", PmType Nominal ]

    barOpts = [ 'MExtent' 'StdDev'
              , 'MTicks' [ 'MColor' \"purple\" ]
              , 'MRule' [ MColor \"teal\" ]
              ]
    range = [ mark ErrorBar barOpts
            , encoding
                . position Y [ PName \"Gmag\"
                             , PmType Quantitative
                             , PScale [ SZero False ]
                             ]
                $ []
            ]

    center = [ mark Point [ 'MShape' 'SymSquare', 'MSize' 20 ]
             , encoding
                 . position Y [ PName \"Gmag\"
                              , PmType Quantitative
                              , PmType 'Mean'
                              ]
                 $ []
             ]

in toVegaLite
      [ gaiaData
      , encoding (cluster [])
      , layer (map asSpec [ range, center ])
      , width 300
      , height 300
      ]
@

-}

errorBars :: VegaLite
errorBars :: VegaLite
errorBars =
  let cluster :: BuildEncodingSpecs
cluster = Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Cluster", Measurement -> PositionChannel
PmType Measurement
Nominal ]

      barOpts :: [MarkProperty]
barOpts = [ MarkErrorExtent -> MarkProperty
MExtent MarkErrorExtent
StdDev
                , [MarkProperty] -> MarkProperty
MTicks [ Text -> MarkProperty
MColor Text
"purple" ]
                , [MarkProperty] -> MarkProperty
MRule [ Text -> MarkProperty
MColor Text
"teal" ]
                ]
      range :: [PropertySpec]
range = [ Mark -> [MarkProperty] -> PropertySpec
mark Mark
ErrorBar [MarkProperty]
barOpts
              , [EncodingSpec] -> PropertySpec
encoding
                  ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"Gmag"
                               , Measurement -> PositionChannel
PmType Measurement
Quantitative
                               , [ScaleProperty] -> PositionChannel
PScale [ Bool -> ScaleProperty
SZero Bool
False ]
                               ]
                  ([EncodingSpec] -> PropertySpec) -> [EncodingSpec] -> PropertySpec
forall a b. (a -> b) -> a -> b
$ []
              ]

      point :: [PropertySpec]
point = [ Mark -> [MarkProperty] -> PropertySpec
mark Mark
Point [ Symbol -> MarkProperty
MShape Symbol
SymSquare, Double -> MarkProperty
MSize Double
20 ]
              , [EncodingSpec] -> PropertySpec
encoding
                  ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"Gmag"
                               , Measurement -> PositionChannel
PmType Measurement
Quantitative
                               , Operation -> PositionChannel
PAggregate Operation
Mean
                               ]
                  ([EncodingSpec] -> PropertySpec) -> [EncodingSpec] -> PropertySpec
forall a b. (a -> b) -> a -> b
$ []
              ]

  in [PropertySpec] -> VegaLite
toVegaLite
      [ PropertySpec
gaiaData
      , [EncodingSpec] -> PropertySpec
encoding (BuildEncodingSpecs
cluster [])
      , [VLSpec] -> PropertySpec
layer (([PropertySpec] -> VLSpec) -> [[PropertySpec]] -> [VLSpec]
forall a b. (a -> b) -> [a] -> [b]
map [PropertySpec] -> VLSpec
asSpec [ [PropertySpec]
range, [PropertySpec]
point ])
      , Double -> PropertySpec
width Double
300
      , Double -> PropertySpec
height Double
300
      ]


{-|
The next plot shows the 'ErrorBand' mark, which fills the area between
the chosen range with a color, and optional borders. Here the
blue band shows the calculated standard deviation - as used in
'errorBars' - and the gray band with borders shows the inter-quartile
range. On top of these are drawn the median (blue) and median (green
dashed) lines.

<<images/vl/errorband.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAWCmCWBzaAXSAuMBmADDgNOFACYCGKpGYoEEkArgE4A2VMKKADgM7oD0fRqQDuAOkTwU0egCN63WIwDGAewB2KWBtGqAtnwAiK+ogBCTANaw+0AG6xEpPrtLdNjG-cd8yFPo-hSAFpSYIA2AEYwkIicIIoZZlgI0lE1FSC4UmJFURRuW0hCWigAMxVGFzRMGhKoTlJGBSpauqgAcRdENjV6XRlFIqI2yAAlAEEAfQBJAGFRgGUevoHGIba6TmYAD2X+weKNyAMAURn5pcxIXv21w5HYSa3dq5vVyGHaAF9Pn4gvw6QYTwYhSKi4AhESAAEm4SjgLjYqC4vAEXlSEikslE8BUfDhCKc6KCzEk1lsABZRAArbjqdZQFCSJJsWYqXQNRjwNSIMBCHmwbgMyDMUgAT0GmAA2p9WrRIC5GBYWpBYNtNBo2G4coV8IyxZxYGxFIwKjJSGpiJAAZ86FpVMRud0arb5WKVaV4LBmFarp1SN09ZA4aQWTVIAAvRQqKilUMKAH6w1sACO9AtTIoTPs1tdf2+92oroVjWVLo2UBkFRyTRVblNVgMrmgVClYT1ACYALo2iuQVTMCpsRBCd2Fkqq9VaapQeApu6uugoA1Gq4ms0Wq158dQe0qR08lqLqDu8Oe72+jpdIpQENh6iR6Ox+OwROQZfJq5pjOScjwHP5iUgFgL2tBynQipllApJqEaO6qmoDpOkefankWFZ0OePpsP6gbHnQd6rg+Uams+zAJjuE4BiODjkERCqwI6FoMn2H70d+Gi-tmRrHsB-y-IW4FQJBKoDkOVw0VoN7BigDawE23AttK7ZgB2eoUp2XZBmxbAwUaoETnuB7OuhRxoUJE5YZekC4SxRyESqJExpgcbka+lHytRjC0ZobC6LAzEeUuK6pumnFZv+PEVnxIG-EQWlQkZyHhi8D5WayzDyO40k6a87LcqG1qgf26ieiZwA-F8QA Open this visualization in the Vega Editor>

@
let posY extra = position Y ([ PName \"Gmag\"
                             , PmType Quantitative
                             , PScale [ SZero False ]
                             ] ++ extra) []

    bands = [ [ encoding (posY [])
              , mark 'ErrorBand' [ MExtent StdDev ]
              ]
            , [ encoding (posY [])
              , mark ErrorBand [ MExtent 'Iqr'
                               , 'MBorders' [ 'MStrokeDash' [ 6, 2 ] ]
                               , MColor "gray"
                               ]
              ]
            , [ encoding (posY [ PAggregate Median ])
              , mark Line []
              ]
            , [ encoding (posY [ PAggregate Mean ])
              , mark Line [ MColor \"green\"
                          , MStrokeDash [ 6, 2, 4, 2 ]
                          ]
              ]
            ]

in toVegaLite
    [ gaiaData
    , encoding (position X [ PName \"Cluster\", PmType Nominal ] [])
    , layer (map asSpec bands)
    , width 300
    , height 300
    , title "Comparing ranges" []
    ]
@

Note that I don't think this is a good visualization
for this /particular/ dataset, since it implies there's a
connection or correlation between clusters, as given by the
x-axis ordering, but the aim here is to show how to use @hvega@
rather than creating sensible plots!

-}

errorBand :: VegaLite
errorBand :: VegaLite
errorBand =
  let posY :: [PositionChannel] -> [EncodingSpec]
posY [PositionChannel]
extra = Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y ([ Text -> PositionChannel
PName Text
"Gmag"
                               , Measurement -> PositionChannel
PmType Measurement
Quantitative
                               , [ScaleProperty] -> PositionChannel
PScale [ Bool -> ScaleProperty
SZero Bool
False ]
                               ] [PositionChannel] -> [PositionChannel] -> [PositionChannel]
forall a. [a] -> [a] -> [a]
++ [PositionChannel]
extra) []

      bands :: [[PropertySpec]]
bands = [ [ [EncodingSpec] -> PropertySpec
encoding ([PositionChannel] -> [EncodingSpec]
posY [])
                , Mark -> [MarkProperty] -> PropertySpec
mark Mark
ErrorBand [ MarkErrorExtent -> MarkProperty
MExtent MarkErrorExtent
StdDev ]
                ]
              , [ [EncodingSpec] -> PropertySpec
encoding ([PositionChannel] -> [EncodingSpec]
posY [])
                , Mark -> [MarkProperty] -> PropertySpec
mark Mark
ErrorBand [ MarkErrorExtent -> MarkProperty
MExtent MarkErrorExtent
Iqr
                                 , [MarkProperty] -> MarkProperty
MBorders [ [Double] -> MarkProperty
MStrokeDash [ Double
6, Double
2 ] ]
                                 , Text -> MarkProperty
MColor Text
"gray"
                                 ]
                ]
              , [ [EncodingSpec] -> PropertySpec
encoding ([PositionChannel] -> [EncodingSpec]
posY [ Operation -> PositionChannel
PAggregate Operation
Median ])
                , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Line []
                ]
              , [ [EncodingSpec] -> PropertySpec
encoding ([PositionChannel] -> [EncodingSpec]
posY [ Operation -> PositionChannel
PAggregate Operation
Mean ])
                , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Line [ Text -> MarkProperty
MColor Text
"green"
                            , [Double] -> MarkProperty
MStrokeDash [ Double
6, Double
2, Double
4, Double
2 ]
                            ]
                ]
              ]

  in [PropertySpec] -> VegaLite
toVegaLite
      [ PropertySpec
gaiaData
      , [EncodingSpec] -> PropertySpec
encoding (Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Cluster", Measurement -> PositionChannel
PmType Measurement
Nominal ] [])
      , [VLSpec] -> PropertySpec
layer (([PropertySpec] -> VLSpec) -> [[PropertySpec]] -> [VLSpec]
forall a b. (a -> b) -> [a] -> [b]
map [PropertySpec] -> VLSpec
asSpec [[PropertySpec]]
bands)
      , Double -> PropertySpec
width Double
300
      , Double -> PropertySpec
height Double
300
      , Text -> [TitleConfig] -> PropertySpec
title Text
"Comparing ranges" []
      ]


{-|

An alternative visualization of a distribution is the \"box and
whiskers\" plot, which can be achieved in @hvega@ with the
'Boxplot' mark. The example below shows the default settings, but
the various components can be controlled with 'MBox', 'MMedian',
'MOutliers', and 'MTicks'.

<<images/vl/errorbox.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAWCmCWBzaAXSAuMBmADDgNOFALYCGATgNYZQBGA9gB4AOANvWoRJACakqkaoCNwCu5VjRgoUzAM7oA9IvKkA7gDpE8FNFG1Rc2OQDG9AHYpYljWeKKAIvVGIAQuMqxF0AG6xEpIpkclbk3n4BinwCigHwpAC0pIkAbACMKUlpOAkCtKywaaQa5vQJcKQ8xhoocj6QXCKQAGb05GRomMIi3MwURkJEPdwA4mSIUuaixLTGDUPDkABKAIIA+gCSAMJLAMqT07Pk88NNbIwHM3ONp5AOAKKbO-uYkFNXxzeLsGvnl0eQBYQAC+C1BIMakDU8B4uhouAIREgABI5CY4GQpKhZAplBFitpdPoNPB6Io0RjAviEqwdF4fAAWDQAKzkFhOUGsZh48HMEy6C0gFy6LXgsFYPCkW1YhlCDSgKAAnsxYJN6MReaRJMCvpBFUJReLJa8xqQJvgFcrVa8AI6iUiWHT8eB+SDgsA6pFmczNJBCUHAoA Open this visualization in the Vega Editor>

@
toVegaLite
    [ gaiaData
    , encoding
        . position X [ PName \"Cluster\", PmType Nominal ]
        . position Y [ PName \"Gmag\", PmType Quantitative ]
        $ []
    , mark 'Boxplot' []
    , width 300
    , height 300
    ]
@

The 'Boxplot' option supports two different \"ranges\":

 * the default is the Tukey Box plot, where the whiskers span
   a range @Q1 - k * IQR@ to @Q3 + k * IQR@, @IQR = Q3 - Q1@,
   @Q1@ and @Q3@ are the lower and upper inter-quartile values
   (so 25 and 75 per cent of the distribution), and @k@
   defaults to 1.5 but can be changed with 'IqrScale';

 * or 'ExRange', which uses the full range of the data (i.e.
   minimum to maximum values).

-}

errorBox :: VegaLite
errorBox :: VegaLite
errorBox =
  [PropertySpec] -> VegaLite
toVegaLite
      [ PropertySpec
gaiaData
      , [EncodingSpec] -> PropertySpec
encoding
          ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Cluster", Measurement -> PositionChannel
PmType Measurement
Nominal ]
          BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Text -> PositionChannel
PName Text
"Gmag", Measurement -> PositionChannel
PmType Measurement
Quantitative ]
          ([EncodingSpec] -> PropertySpec) -> [EncodingSpec] -> PropertySpec
forall a b. (a -> b) -> a -> b
$ []
      , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Boxplot []
      , Double -> PropertySpec
width Double
300
      , Double -> PropertySpec
height Double
300
      ]


{-|

Here I combine 'errorBox' with 'smallMultiples2' so we can compare
the distribution (from the histogram) with that from the box plot.

<<images/vl/comparingerrors.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAmCGAutIC4yghSBXATgGxSgAt54AHAZ2QHpqdYB3AOgHMBLeIrAIywoFMcAYwD2AO3j8JTUQFtqAERFYWAIVwBrftSIA3fi1jVZsCpJw79h6nETVDbWAFpYzgGwBGNy48AGJ4jcePwesExiIk5E-LDQgkzwFLqQADTgGJAAZiI4JvCE6BiYZLA4AgXpRZgA4iYshJBiWLLcgqmVVZAASgCCAPoAkgDCXQDKDU0tbWlVGWR4AB4Tza047bOYCgCigyPjqI0r0x1FkPx980sHk6uQJ2AAvh1PEA8zUBRk-EIVp3iwAE82qgANr3QqzSAmHAaBrcUrrDZnMSiaBsMT1VAQpGiPA5ApZNj8PDQBpDPB8cypKDwAFfCYiWTo2AEFJQYIsKSk1BNPB4N73U5XNCE4ncqC1WD1NmQbjowjwHBYfgy2n0g4ARywsAkHAQbH0kAFGwyAN+JoyUpYOAMCH4DVEWAkiItNLp9s12t1iHgBvt71dkFgCzYFAJvvgwQaADkjjgwCJMmBRogykbBRgXhss0VjVVsadobCsRnMLJ+GidQTcfiDkFYEJYXmTbKRMLgJAzDgRFoGgwiBx7c2kcpI0S06hMiyBAGkWqPVBuG35iJ8hnh6cpKj0Zi0KWoDW1ljRSSyRSzNM3eqoBEmWIWdTIByuYRefzZ5D2yfxZBJdKrwukBajqEb6oaG6dGax66CyyqEAAHL4OZVMhrwdAAuukeaQAAJBQQjRCYDQkOQVC0FYYTsJwPBMGwIjUPhhFGBRTh4IO1C6AALEwABWFDiIikC4s0YhhqgADM7xZA2-D5MemREqeBzkpSl6QPODJ3g+2GiGICm7sATwPEAA Open this visualization in the Vega Editor>

@
let histEnc = encoding
                . position X [ PName \"Gmag\", PmType Quantitative, PBin [] ]
                . position Y yAxis
                . color [ MName \"Cluster\", MmType Nominal, MLegend [] ]

    errEnc = encoding
               . position X [ PName \"Gmag\", PmType Quantitative ]
               . position Y [ 'PNumber' 80 ]
               . color [ MName \"Cluster\", MmType Nominal, MLegend [] ]

    yAxis = [ PAggregate Count
            , PmType Quantitative
            , PAxis [ AxTitle \"Number of Stars\" ]
            ]

    boxOpts = [ 'MMedian' [ MColor \"black\" ]
              , 'MBox' [ MStroke \"white\" ]
              , 'MNoOutliers'
              ]

    histSpec = asSpec [ mark Bar [], histEnc [] ]
    errSpec = asSpec [ mark Boxplot boxOpts, errEnc [] ]

    combinedSpec = asSpec [ layer [ histSpec, errSpec ] ]

in toVegaLite
    [ gaiaData
    , columns 3
    , facetFlow [ FName \"Cluster\", FmType Nominal ]
    , specification combinedSpec
    ]
@

The main additions here are the configuration of the box plot - with
'MMedian', 'MBox' (used to ensure the box is visually distinct from
the bar for the Pleiades cluster, where they overlap), and 'MNoOutliers'
(to turn off the display of the outliers) - and the use of 'PNumber'
to define the location on the y axis of the boxplot visualization.
Note that 'PNumber' is defined in pixel units, with 0 being the
top of the visualization and 80 was found by trial and error.

-}

comparingErrors :: VegaLite
comparingErrors :: VegaLite
comparingErrors =
 let histEnc :: [EncodingSpec] -> PropertySpec
histEnc = [EncodingSpec] -> PropertySpec
encoding
                ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Gmag", Measurement -> PositionChannel
PmType Measurement
Quantitative, [BinProperty] -> PositionChannel
PBin [] ]
                BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [PositionChannel]
yAxis
                BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal, [LegendProperty] -> MarkChannel
MLegend [] ]

     errEnc :: [EncodingSpec] -> PropertySpec
errEnc = [EncodingSpec] -> PropertySpec
encoding
                ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [ Text -> PositionChannel
PName Text
"Gmag", Measurement -> PositionChannel
PmType Measurement
Quantitative ]
                BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [ Double -> PositionChannel
PNumber Double
80 ]
                BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [MarkChannel] -> BuildEncodingSpecs
color [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal, [LegendProperty] -> MarkChannel
MLegend [] ]

     yAxis :: [PositionChannel]
yAxis = [ Operation -> PositionChannel
PAggregate Operation
Count
             , Measurement -> PositionChannel
PmType Measurement
Quantitative
             , [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"Number of Stars" ]
             ]

     boxOpts :: [MarkProperty]
boxOpts = [ [MarkProperty] -> MarkProperty
MMedian [ Text -> MarkProperty
MColor Text
"black" ]
               , [MarkProperty] -> MarkProperty
MBox [ Text -> MarkProperty
MStroke Text
"white" ]
               , MarkProperty
MNoOutliers
               ]

     histSpec :: VLSpec
histSpec = [PropertySpec] -> VLSpec
asSpec [ Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar [], [EncodingSpec] -> PropertySpec
histEnc [] ]
     errSpec :: VLSpec
errSpec = [PropertySpec] -> VLSpec
asSpec [ Mark -> [MarkProperty] -> PropertySpec
mark Mark
Boxplot [MarkProperty]
boxOpts, [EncodingSpec] -> PropertySpec
errEnc [] ]

     combinedSpec :: VLSpec
combinedSpec = [PropertySpec] -> VLSpec
asSpec [ [VLSpec] -> PropertySpec
layer [ VLSpec
histSpec, VLSpec
errSpec ] ]

 in [PropertySpec] -> VegaLite
toVegaLite
     [ PropertySpec
gaiaData
     , Natural -> PropertySpec
columns Natural
3
     , [FacetChannel] -> PropertySpec
facetFlow [ Text -> FacetChannel
FName Text
"Cluster", Measurement -> FacetChannel
FmType Measurement
Nominal ]
     , VLSpec -> PropertySpec
specification VLSpec
combinedSpec
     ]


-- $intro-dashboard
-- In the following visualization I try to combine as many of the
-- concepts we have explored in this tutorial into one. There are
-- layers, combined visualizations, and a
-- selection that ties the different plots together! How much more
-- could you want?

{-|

This is based on the
<https://vega.github.io/vega-lite/examples/concat_marginal_histograms.html Marginal Histogram>
example from the Vega-Lite
<https://vega.github.io/vega-lite/examples/ Example Gallery>.
There is very-little new in this plot, in that pretty-much everything
has been shown before. However, there are some interesting wrinkles,
such as

  * combining multiple plots, in this case the \"top\" area - which is
    a histogram on top of a plot which is itself a \"map\" and
    a histogram - and \"bottom" area - which is just a point plot -
    requires judicious use of 'asSpec';

  * selection works in __both__ the main plots - the \"map\" and \"point\"
    plots - to highlight all stars in the same cluster, and I was
    pleasantly surprised to find out I could just use the same selection
    specification (@selCluster@) in both (hopefully I am not just
    enjoying a hole in the Vega-Lite specification);

  * I have been perhaps too defensive in defining the Right Ascension
    and Declination axes in the relevant plots, as I want to make sure
    the histogram bins and plot axes are well aligned (that is the
    @'Nice' False@ statements may not be needed when defining the
    histogram axes);

  * I am not 100% sure I understand what is going on with the
    grid labels on the Declination axis, as I had thought I was
    asking for marks every 15 degrees, but the plot shows them
    every 30 degrees (however, if I change the @deTicks@ array then
    the marks change in ways I currently do not understand);

  * and I have decided to display Right Ascension in hours, rather than
    degrees, because why have one way to measure a value when you can
    have many!

<<images/vl/combinedplot.png>>

<https://vega.github.io/editor/#/url/vega-lite/N4KABGBEAuBOCGA7AzgMwPawLaQFxgG1hJ5k8oAlAQUgBooBjeAGwYFdn5oBTcyAEy5ssAOmoB9AJIBhCgGUwAejABGAKyQAvgF1a4RukSoAlgHNyxAG7HuAdwuRkcdAGte+GAhQAHeLG6I0FqaehACXPAW+mFssMx8ABbQ0N7IuIqKCLYipsbQCWwARmzI3LAMhjyBIhVYigAi6GymAEKxbooJltym8IpYpDywnd29ioLQfb3G8AC08HMAbCqL8yoADLOThczcKvAiiOizCdzw-GUi0MiWdNFQGNhcURCvUL6wpS9vr5AA4gNzB5EMJCmU7j9fhIZPI+CCsGDYBDIe9mAAPOGg8GhFGQeoAUSksjkmIR2Pub0g3HE3nRpMRkApYE09xZEBC+kglgqiCYQXwBHuoEhXJ5fPIgpRYGFUqgpzMSXIG3WOKlkFsxn4+XIABZ1iqmZTOABPcECw0-GWy34DWAuBwVZiYPimBDGuhQaDG7zuKCFPxaVXWqABCr8YyIIHSi1qjH4K3BykmbjMfh8ajIxOUwoRhzcNFVfmEFVgABMOt0UCc3G85FL9EgiGMDF9qBYpQ5WZ+jkwRYE3GQLcQ4cjma7nu9vsgAEc2EhoHkuMZumOuyQ0cYyPgQcxmDGUZ3x5B3fH92rByxfcR+OgBrmBSWAMz69baQ-jqDwUyunpcKcVNhAlXLMYEnPhZ3nRcFxXIM13gDct2lGA8l2cgd2YNlx0wxNsNld9ZQTa1PCQNBMBwAViBMZghgcUpdgYBdDD4bxm3tTQdFg2VIFte0PH9JFOLVUN0BHKNCODSBHWdU8PzCSwWDYKcf3dQSJJ5cNGMQb5ZIeGxUz4aRmBKGjVMTRwU24BjjCYjwWIYe1TIkr0fThW8IxYRkdNwnDHJFONox0yBk30jwM183Ecy0+MqQLAIiwIEty0rRweFrfB6ygJsW3INtmA7cKRWQXs+AuQcAlE4CzOcqcIMCKDl14Aru3gzc0I4PcP3wsyTwC2THCYVDopvO8ouLehn31N8mspL8f16Hg+AAoDpt+arwLnOrJmgxqz1xFrEOIBdoEGsB0O84NzqlS6fmuiBtAtLrXnEykEjFZ5zUTZ7cXlUxFXwPUDTXHiHHQXwGDyHr1hENQGzWjxwfKVDHrVDUtQSXV9Sa8z6M0hw7N4pC4arCNTFQhtgv4RCCEgQzjPBN9kdxYSKpko8pKRVmPy5BSlP8FTdpFdS8ms0avrMimDKM6sBIF3E6Ms3HbNYyqQKJxs3MQDzZbeW6DxWyB-LFriJdCmgVrCC8TuvW94HvMaywrBsstbdtuEZoiitgPtSqHCrzYnFyPFqo6lxg7Xfn2hwjpOyAKAVaAwCoMqUBFsAAAoEiaT4AEotHD93cR6o21RNqACSJWF-f6y8HGG23RoIWYAE4Sxb5LnZy12C7VNXg-qsPAsjzm+vkoyBwlcPIWbwGdNeWYAHYYcnn5ZkWGfZ7AWYdSXjeIFmZ9-beWZ1EP1519nk-l7eA+r9ebfT4gNeH7ARfn5b2-dFv5DjqnepLOYdyml04XB-AOXOy9daQkga8aB3depmR+n9MAAMsao21PgZUWMTRmkIOHYuuJgbRXZi6N0HpQKBz9AGOBIpmYk20n1Q2t8LYDSvAIG2dsErjRfFNJhn5vz+Hmv+Joy1eHkJqhtEO20VZwQQlHFCvozq32oYXehgVS54kJDCEkz9ICRTzLFICApp70Dbg2asaVVAw0ys2F2eU3Y6N7hI-uO1d6flkdudqSiIH5xWvgkUcASKPHIoQSixhqI4OIPLKyNl3jK3Yp-QKhDKEy0CrQ0cw8ubEIySPHmJDuD81cZJQwGkRaqNnkFPSaYPC02ltIo8UTFaxPsnUtcasjhYHcvEXh0CrqnwNmUrmltWF1w4U+bhyi1SzQEX+RawiggOLAkHJxW0GotIkkPQm8i2q7h6XrL+RdRHqPLlotZRE9HRXzIWCUxiwCmKrKlJUVjGw2M7nYiZuJHGQRWQPXe65WoeN3F4ry+dZb3RwvuBJXFkBgzoelc+IpCjCMpnwVAUt0YPSZJCyk0L4Dg3SWWeFfokWISCmizyMCgziW4n4AmxBQa4ohuQKGTy1YIwYEjTi6pNToLAGoUshLsYK1KdFfGUdFnE0jGTXSKZkUChplLGiDNOVpLEhaIpToObwNxKPRSeSClcSFo0-BFSZWSzpikiSDThVNIcrtMRrkOmay6daa6jN+nZKTJU5idJHLVytvajwTpzBOxefgXK+V9wBqgH3b5LiuIbMOls2yfgWCcDROnDpu5jDzHKKUdSudXWqWPAM346iARfjqVGmcyzQ5xsme4zZP8+B-DAICJs0A2AXDzldJkuFMKQsgAAEkHKcAYiRkipHSIoUYBxcj5CKCIayigR3cAGNO38swAE8GnTqEQAArIqWkcTf39TwAszbbbwDABMK9qBYC3jAH4AAGsuXAKgAAc6xd3rCbo+ee76yFIAYJnTV3FNT8CRiATQQA Open this visualization in the Vega Editor (although the link is long, and may not work with Internet Explorer)>

@
let trans = transform
              . calculateAs \"datum.RA_ICRS / 15\" \"RA\"

    quant n = [ PName n, PmType Quantitative ]

    big = 400
    small = 100
    wmain = width big
    hmain = height big
    wsub = width small
    hsub = height small
    noTitle = PAxis [ AxNoTitle ]

    raAxis = [ PScale [ SDomain (DNumbers [ 0, 24 ])
                      , SNice (IsNice False)
                      ]
             , PSort [ Descending ]
             , PAxis [ AxTitle \"Right Ascension (hours)\" ]
             ]

    deMin = -90
    deMax = 90
    deStep = 15

    -- we do not get ticks/grids at all these values, but it does
    -- something (e.g. if do not specify the axis ticks are different)
    --
    deTicks = Numbers [ deMin, deMin + deStep .. deMax ]
    deAxis = [ PScale [ SDomain (DNumbers [ deMin, deMax ])
                      , SNice (IsNice False)
                      ]
             , PAxis [ AxTitle \"Declination (degrees)\"
                     , AxValues deTicks
                     ]
             ]

    colorEnc = color [ MSelectionCondition (SelectionName \"pick\")
                         [ MName \"Cluster\", MmType Nominal ]
                         [ MString \"grey\" ]
                     ]
    mapEnc = encoding
               . position X (quant \"RA\" ++ raAxis)
               . position Y (quant \"DE_ICRS\" ++ deAxis)
               . colorEnc

    circleMark = mark Circle [ MOpacity 0.5 ]

    mapSpec = asSpec [ mapEnc []
                     , circleMark
                     , wmain
                     , hmain
                     , selCluster []
                     ]

    -- histogram of the RA values
    --
    raBinning = [ PBin [ 'Extent' 0 24
                       , Step 2
                       , Nice False
                       ]
                , PSort [ Descending ]
                , PAxis []
                ]

    -- histogram of the Declination values
    --
    deBinning = [ PBin [ Extent deMin deMax
                       , Step deStep
                       , Nice False
                       ]
                , PAxis []
                ]

    histAxis = [ PAggregate Count
               , PmType Quantitative
               , noTitle
               , PScale [ SDomain (DNumbers [ 0, 3000 ]) ]
               ]

    raEnc = encoding
              . position X (quant \"RA\" ++ raBinning)
              . position Y histAxis

    deEnc = encoding
              . position Y (quant \"DE_ICRS\" ++ deBinning)
              . position X histAxis

    allRA = [ raEnc []
            , mark Bar [ MColor \"gray\" ]
            ]
    filtRA = [ filterCluster []
             , raEnc
                 . colorEnc
                 $ []
             , mark Bar []
             ]

    allDE = [ deEnc []
            , mark Bar [ MColor \"gray\" ]
            ]
    filtDE = [ filterCluster []
             , deEnc
                 . colorEnc
                 $ []
             , mark Bar []
             ]

    raSpec = asSpec [ wmain, hsub, layer [ asSpec allRA, asSpec filtRA ] ]
    deSpec = asSpec [ hmain, wsub, layer [ asSpec allDE, asSpec filtDE ] ]

    borderSpacing = 20

    mapAndDecSpec = asSpec [ spacing borderSpacing
                           , 'bounds' 'Flush'
                           , hConcat [ mapSpec, deSpec ]
                           ]

    histSpecs = [ raSpec, mapAndDecSpec ]

    -- select the cluster which the star belongs to; do not use
    -- \"nearest click\" as that means a user can not cancel the
    -- selection.
    --
    pick = \"pick\"
    selCluster = selection
                   . select pick Single [ Fields [ \"Cluster\" ] ]

    filterCluster = transform
                      . filter (FSelection pick)

    plxOpts = [ PScale [ SType ScLog, SNice (IsNice False) ]
              , PAxis [ AxTitle \"parallax (milli-arcsecond)\" ]
              ]
    gmagOpts = [ PAxis [ AxTitle \"G magnitude\" ] ]

    encData = encoding
                . position X (quant \"plx\" ++ plxOpts)
                . position Y (quant \"Gmag\" ++ gmagOpts)

    parallaxSpec = asSpec [ width (big + borderSpacing + small)
                          , encData
                              . colorEnc
                              $ []
                          , circleMark
                          , selCluster []
                          ]

    allSpecs = [ asSpec [ spacing borderSpacing
                        , bounds Flush
                        , vConcat histSpecs
                        ]
               , parallaxSpec ]


in toVegaLite
   [ gaiaData
   , trans []
   , vConcat allSpecs
     -- remove the "other" axis (e.g. top of Y, right for X)
   , configure
       . configuration (ViewStyle [ 'ViewStroke' \"transparent\" ])
       $ []
   , title \"Gaia data from arXiv:1804.09378\" [ 'TAnchor' 'AMiddle' ]
   ]
@

Here is the visualization after selecting a star:

<<images/vl/combinedplot-selected.png>>

-}

combinedPlot :: VegaLite
combinedPlot :: VegaLite
combinedPlot =
  let trans :: [TransformSpec] -> PropertySpec
trans = [TransformSpec] -> PropertySpec
transform
                ([TransformSpec] -> PropertySpec)
-> ([TransformSpec] -> [TransformSpec])
-> [TransformSpec]
-> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> [TransformSpec] -> [TransformSpec]
calculateAs Text
"datum.RA_ICRS / 15" Text
"RA"

      quant :: Text -> [PositionChannel]
quant Text
n = [ Text -> PositionChannel
PName Text
n, Measurement -> PositionChannel
PmType Measurement
Quantitative ]

      big :: Double
big = Double
400
      small :: Double
small = Double
100
      wmain :: PropertySpec
wmain = Double -> PropertySpec
width Double
big
      hmain :: PropertySpec
hmain = Double -> PropertySpec
height Double
big
      wsub :: PropertySpec
wsub = Double -> PropertySpec
width Double
small
      hsub :: PropertySpec
hsub = Double -> PropertySpec
height Double
small
      noTitle :: PositionChannel
noTitle = [AxisProperty] -> PositionChannel
PAxis [ AxisProperty
AxNoTitle ]

      raAxis :: [PositionChannel]
raAxis = [ [ScaleProperty] -> PositionChannel
PScale [ DomainLimits -> ScaleProperty
SDomain ([Double] -> DomainLimits
DNumbers [ Double
0, Double
24 ])
                        , ScaleNice -> ScaleProperty
SNice (Bool -> ScaleNice
IsNice Bool
False)
                        ]
               , [SortProperty] -> PositionChannel
PSort [ SortProperty
Descending ]
               , [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"Right Ascension (hours)" ]
               ]

      deMin :: Double
deMin = -Double
90
      deMax :: Double
deMax = Double
90
      deStep :: Double
deStep = Double
15

      -- we do not get ticks/grids at all these values, but it does
      -- something (e.g. if do not specify the axis ticks are different)
      --
      deTicks :: DataValues
deTicks = [Double] -> DataValues
Numbers [ Double
deMin, Double
deMin Double -> Double -> Double
forall a. Num a => a -> a -> a
+ Double
deStep .. Double
deMax ]
      deAxis :: [PositionChannel]
deAxis = [ [ScaleProperty] -> PositionChannel
PScale [ DomainLimits -> ScaleProperty
SDomain ([Double] -> DomainLimits
DNumbers [ Double
deMin, Double
deMax ])
                        , ScaleNice -> ScaleProperty
SNice (Bool -> ScaleNice
IsNice Bool
False)
                        ]
               , [AxisProperty] -> PositionChannel
PAxis [ Text -> AxisProperty
AxTitle Text
"Declination (degrees)"
                       , DataValues -> AxisProperty
AxValues DataValues
deTicks
                       ]
               ]

      colorEnc :: BuildEncodingSpecs
colorEnc = [MarkChannel] -> BuildEncodingSpecs
color [ BooleanOp -> [MarkChannel] -> [MarkChannel] -> MarkChannel
MSelectionCondition (Text -> BooleanOp
SelectionName Text
"pick")
                           [ Text -> MarkChannel
MName Text
"Cluster", Measurement -> MarkChannel
MmType Measurement
Nominal ]
                           [ Text -> MarkChannel
MString Text
"grey" ]
                       ]
      mapEnc :: [EncodingSpec] -> PropertySpec
mapEnc = [EncodingSpec] -> PropertySpec
encoding
                 ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X (Text -> [PositionChannel]
quant Text
"RA" [PositionChannel] -> [PositionChannel] -> [PositionChannel]
forall a. [a] -> [a] -> [a]
++ [PositionChannel]
raAxis)
                 BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y (Text -> [PositionChannel]
quant Text
"DE_ICRS" [PositionChannel] -> [PositionChannel] -> [PositionChannel]
forall a. [a] -> [a] -> [a]
++ [PositionChannel]
deAxis)
                 BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BuildEncodingSpecs
colorEnc

      circleMark :: PropertySpec
circleMark = Mark -> [MarkProperty] -> PropertySpec
mark Mark
Circle [ Double -> MarkProperty
MOpacity Double
0.5 ]

      mapSpec :: VLSpec
mapSpec = [PropertySpec] -> VLSpec
asSpec [ [EncodingSpec] -> PropertySpec
mapEnc []
                       , PropertySpec
circleMark
                       , PropertySpec
wmain
                       , PropertySpec
hmain
                       , [SelectSpec] -> PropertySpec
selCluster []
                       ]

      -- histogram of the RA values
      --
      raBinning :: [PositionChannel]
raBinning = [ [BinProperty] -> PositionChannel
PBin [ Double -> Double -> BinProperty
Extent Double
0 Double
24
                         , Double -> BinProperty
Step Double
2
                         , Bool -> BinProperty
Nice Bool
False
                         ]
                  , [SortProperty] -> PositionChannel
PSort [ SortProperty
Descending ]
                  , [AxisProperty] -> PositionChannel
PAxis []
                  ]

      -- histogram of the Declination values
      --
      deBinning :: [PositionChannel]
deBinning = [ [BinProperty] -> PositionChannel
PBin [ Double -> Double -> BinProperty
Extent Double
deMin Double
deMax
                         , Double -> BinProperty
Step Double
deStep
                         , Bool -> BinProperty
Nice Bool
False
                         ]
                  , [AxisProperty] -> PositionChannel
PAxis []
                  ]

      histAxis :: [PositionChannel]
histAxis = [ Operation -> PositionChannel
PAggregate Operation
Count
                 , Measurement -> PositionChannel
PmType Measurement
Quantitative
                 , PositionChannel
noTitle
                 , [ScaleProperty] -> PositionChannel
PScale [ DomainLimits -> ScaleProperty
SDomain ([Double] -> DomainLimits
DNumbers [ Double
0, Double
3000 ]) ]
                 ]

      raEnc :: [EncodingSpec] -> PropertySpec
raEnc = [EncodingSpec] -> PropertySpec
encoding
                ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X (Text -> [PositionChannel]
quant Text
"RA" [PositionChannel] -> [PositionChannel] -> [PositionChannel]
forall a. [a] -> [a] -> [a]
++ [PositionChannel]
raBinning)
                BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y [PositionChannel]
histAxis

      deEnc :: [EncodingSpec] -> PropertySpec
deEnc = [EncodingSpec] -> PropertySpec
encoding
                ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [EncodingSpec] -> PropertySpec
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
Y (Text -> [PositionChannel]
quant Text
"DE_ICRS" [PositionChannel] -> [PositionChannel] -> [PositionChannel]
forall a. [a] -> [a] -> [a]
++ [PositionChannel]
deBinning)
                BuildEncodingSpecs -> BuildEncodingSpecs -> BuildEncodingSpecs
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Position -> [PositionChannel] -> BuildEncodingSpecs
position Position
X [PositionChannel]
histAxis

      allRA :: [PropertySpec]
allRA = [ [EncodingSpec] -> PropertySpec
raEnc []
              , Mark -> [MarkProperty] -> PropertySpec
mark Mark
Bar [ Text -> MarkProperty
MColor Text
"gray" ]
              ]
      filtRA :: [PropertySpec]
filtRA = [ [TransformSpec] -> PropertySpec
filterCluster []
               , [EncodingSpec] -> PropertySpec
raEnc
                   ([EncodingSpec] -> PropertySpec)
-> BuildEncodingSpecs -> [Encodi