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).
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.
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 ] ]
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.
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 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.
let selLabel = "naming is hard" sel = selection . select selLabel Interval [ ] in toVegaLite (sel [] : selectionProperties selLabel "Drag a rectangle to select points")
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).
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.
let sel = selection . select "pick" Single [ BindLegend (BLField "Cluster") ] in toVegaLite (sel [] : selectionProperties "pick" "Select a legend item")
let selLabel = "this is just a label" sel = selection . select selLabel Multi [] in toVegaLite (sel [] : selectionProperties selLabel "Shift click to select points")
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.
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).
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: 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).
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.
let sel = selection . select "pick" Single [ Fields [ "Cluster" ] , Empty , Nearest True ] in toVegaLite (sel [] : selectionProperties "pick" "Select a point, select a cluster")
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.