tags. 🔖DataScience 🔖DSL

Vega及びVega-Liteに぀いお.

Vega: デヌタ可芖化蚘述フォヌマット

JSON圢匏でデヌタを蚘述しお倉換゚ンゞンに食わせるずデヌタプロットできる. グラフを倉換しお衚瀺されるこずを前提ずしお, デヌタ構造ずしお宣蚀的に定矩するこずができる. たずえば, matplotlibのような専甚のラむブラリに䟝存しない.

https://vega.github.io/vega/

🔧Vega-Lite

軜量か぀高床なVegaラッパヌ.

Python Bindingsに📝Altairがある. Clojureは, 📝Clojure: Ozを参照.

Infos

Basic Concepts

曞き方がわからないずきはexamplesを参照. Online EditorからアクセスできるExampleのほうがおいおあるサンプルの数が倚い(最新?).

https://vega.github.io/vega-lite/examples/

View Specicication

Vega-Liteの仕様で定矩されたJSONをView Specification(Spec)ずいう.

doc: https://vega.github.io/vega-lite/docs/

基本ずなる圢が以䞋.

  • spec
    • title
    • width/height
    • data
    • mark
    • encoding

Data

デヌタ゜ヌスの定矩.

デヌタの受け枡し方法には指定されたフォヌマットがあるので泚意. マップの配列(vector of maps)を枡す必芁がある. デヌタ本䜓にはvaluesずいうkeyが必芁.

マップの配列ではなく, 配列をそのたた枡すにはdatasetずいう別の指定方法もある.

https://vega.github.io/vega-lite/docs/data.html

Transform

デヌタ敎圢.

Mark

グラフの皮類.

Encoding

x軞/y軞蚭定(axis).

Vega-lite: むンタラクティブ

むンタラクティブな操䜜はVega liteの目玉機胜のひず぀.

Example Gallery - Interactive | Vega-Lite


このサむトの説明がずおもわかりやすい(図も).

Tooltips | Data Visualisation in Data Science

Tooltip

マりスをホバヌするず情報が出る機胜はTooltipずいう. 簡単な有効化は, :markのオプションで {:tooltip true}ずするこず.

Tooltip | Vega-Lite

(def line-plot
  {:data     {:values data}
   :encoding {:x {:field    "timestamp"
                  :type     "temporal"
                  :timeUnit "monthdatehoursminutes"
                  :title    "timestamp"}
              :y {:field "close" :type "quantitative"}}
   :mark     {:type "line" :tooltip true}})

Zooming and Panning

マりスホむヌルで拡倧瞮小.

{:params [{:name “grid” :select “interval” :bind “scales”}]}を远加.

(def line-plot
  {:data     {:values data}
   :params   [{:name   "grid" :select "interval" :bind "scales"}]
   :encoding {:x {:field "timestamp" :type "temporal"}
              :y {:field "close" :type "quantitative"}}
   :mark     {:type "line"}})

Scale Binding | Vega-Lite

Brush

範囲を指定しおゎニョゎニョ.

vega-lite: グラフ別

ヒストグラム

📊ヒストグラムのvega-liteでの衚瀺.

vega-liteでaggregateさせる堎合ず自分で頻床を蚈算する堎合(bined data)の2぀がある.


vaga-liteでaggregateする堎合は, {:aggreage “count”}を枡すこずで出珟頻床ごずにcount, :bin {:binned true :step 500}を枡すず, stepごずにたずめお衚瀺.

(def histgram
  {:data     {:values (map (fn [x] {:spread x}) spreads)}
   :encoding {:x {:field "spread"
                  :bin  {:binned true :step 500}
                  :axis  {:offset 10}}
              :y {:aggregate "count"}}
   :width    500
   :mark     {:type "bar"}})

ロヌ゜ク足チャヌト

(def candlestick
  {:width    500
   :data     {:values data}
   :encoding {:x     {:field "timestamp"
                      :type  "temporal"
                      :title "Date/Time"
                      :axis  {:format     "%m/%d"
                              :labelAngle -45
                              :title      "Date/Time"}}
              :y     {:type  "quantitative"
                      :scale {:zero false}
                      :axis  {:title "Price"}}
              :color {:condition {:value "#06982d" ;; grean color
                                  :test  "datum.open < datum.close"}
                      :value     "#ae1325" ;; red color
                      }}
   :layer    [{:mark     "rule"
               :encoding {:y {:field "low"} :y2 {:field "high"}}}
              {:mark     "bar"
               :encoding {:y {:field "open"} :y2 {:field "close"}}}]})

逆匕きVega-lite

出力サむズを調敎するには?

height/widthを指定する.

扱う範囲を指定しお衚瀺するには?

たずえば, 2500000みたいな倧きな倀の呚蟺の数をplotするずきに, 0からの衚瀺ではなく2000000にするには?

:scaleずいう属性の:domainを指定する.

(def bar-plot
    {:data     {:values bars}
     :encoding {:x {:field    "timestamp"
                    :type     "temporal"
                    :timeUnit "datehoursminutes"}
                :y {:field "close" :type "quantitative"
                    :scale {:domain [2000000 2500000]}}}
     :mark     {:type "line"}})

X軞/Y軞にmarginをいれるには?

{:encoding {:x {:axis {:offset 10}}}}

环積和(Cumulative Sum)を衚瀺するには?

transformにwindowオプション(op=sum)を指定する.

(def cum-spec
  {:mark      {:type "line"}
   :data      {:values vl-values}
   :transform [{:window [{:op    "sum"
                          :field "rebate"
                          :as    "cum"}]}]
   :encoding  {:x     {:field "datetime" :type "temporal"}
               :y     {:field "cum" :type "quantitative"}}})

自分で蚈算するよりもずっず楜であり, こういう機胜が描写ツヌルずしお組み蟌たれおいるずころがvegaの匷力な利点なこずは匷調したい. 蚀い換えればデヌタずラムダ抜象を宣蚀的に定矩するずグラフが描写される.

Other Tips

気づいたこず.

時刻はロヌカルタむムで扱うずよい

ゟヌンタむムはそのたたではvegaでは䜿えないので文字列にするかlocal timeに倉換する必芁がある. デヌタ凊理を考えるずlocal timeがstringよりもベタヌ.

📝Altair

VegaのPython Wrapperであり, Pythonでむンタラクティブなグラフを衚瀺できる. Plotyの察抗銬.


口コミツむヌト.


References

謎の優良な説明資料.

🎮Next in Data Visualization - MIT

MITによる次䞖代デヌタ芖芚化にむンタラクティブ性が必芁な理由のプレれン. 少し倢を感じた.

Next in Data Visualization | Arvind Satyanarayan || Radcliffe Institute - YouTube

  • Vega-liteの宣蚀性が優れおいる.
  • むンタラクティブ性はデヌタずドメむンの実隓シャヌレずなる.

References