ロング型からワイド型へのデータ変換①(Data conversion from long to wide type(1))

同じデータソースを使って複数のプラットフォームを扱うとき、必ずあるのが独自ルール。「何を」「どの視点で」「誰のために」可視化するかで、準備も変わります。


今回は、Flourishで可視化するために、TableauとExploratryで用いたデータの形を変えます。

  • 使用データ:[ Google Cloud Public Datasets] chicago_crime_data (Chicago Police Department crime data from 2001 to present)
  • 期間:2010-01-01 ~ 2020-12-31(10年間)


Flourishって、豊富なテンプレに合えば即可視化できます。以下はバー&パイチャートのテンプレ一部。素敵すぎん?この画面見るだけでテンション上がる。パッケージに惑わされて、目的が曇りそうになる危険を感じたことがある。

現在のデータの形(Tableau/Exploratry)

上記は2010年ぶんのみ、一部です。フルは×10年分(2010-2020データだから)。

SQLは過去記事を参照してください。各年それぞれ、犯罪タイプごとの集計でした。

これをFlourishで可視化するために、テンプレに合うスタイルに変換します。もちろんFlourishの中にも機能があるけど、ヘビーユーザーじゃないのでデータソースをテンプレに適した形に寄せるほうが、私にとっては早いです。

ではどのテンプレを使うのか。

このあたり近いですね。Flourishのグラフでは、「横棒」(旗っぽい)がBar、「縦棒」(筍っぽい)がColumnで表されます。TableauやExploratryは縦/横変換がパレット上にあるのでどっちから始めてもいいんですが、Flourishの場合はX軸Y軸の型もテンプレに含まれているので中身を見ます。


【Bar chart(stacked)テンプレ】

Bar chart(stacked) のサンプル中身です。上段がプレビュー、下段がデータ画面。イメージのためスペース省いてます。Flourishはデータ画面でラベルや計算に使う行を指定するスタイルです。縦横変換した場合、右下のラベルは使えなくなる(対応するラベルがひっくり返ってしまう)ので、再設定が必要になります。

このサンプルでは、X軸がSUM、Y軸に国です。私が作りたいのはX軸にSUM、Y軸に年なので、違うみたい。


【Column chart(stacked)テンプレ】

X軸が国、Y軸が数値(B+C)。イメージとしてはこっちが近い。これかな。


プラットフォームのテンプレ

この【Bar】【Column】、データ同じですね。でも、縦横変換でグラフの形は変わりません。同じデータの配列でも、Flourish内での可視化行程が違うから。だからFlourishでは自分のデータソースの配列をテンプレに寄せたほうが早い。言い換えれば、各テンプレで変換行程を固定しているからこそ、この舞台で動くビジュアルがすぐ表現できる。


そして気付く。これは…今のデータじゃ私のイメージを表現できない。

  • 比較のためにColumn chartを使う
  • X軸に年(2010-2020)
  • Y軸に年ごとの犯罪タイプ積み上げて総数表示

つまりFlourishデータ画面で言うと

  • A列(X軸)に年(2010-2020)
  • B列以降に犯罪タイプ別の件数と総数
  • 1行目(表頭)は年、総数、各犯罪タイプ

にしないといけない。

「年」×「犯罪タイプ」×「件数」のロングデータから、「年」×「犯罪タイプ件数」のワイドデータに変換します。②につづく。


********



When dealing with multiple platforms using the same data source, there are always unique rules. Depending on 'what', 'what perspective' and 'for whom' you want to visualise, your preparation will vary.


In this case, we will change the form of the data used in Tableau and Exploratry in order to visualise it in Flourish.

  • Data used: [ Google Cloud Public Datasets] chicago_crime_data (Chicago Police Department crime data from 2001 to present)
  • Period: 2010-01-01 - 2020-12-31 (10 years)

Flourish is an instant visualisation if it fits the extensive templates. Below is a partial bar and pie chart template.

Isn't it too nice? I get excited just looking at this screen. I was in danger of looking too good and almost forgetting the purpose.

Current form of data (Tableau/Exploratry)

The above is only partly for 2010. Full is for x 10 years (because it is 2010-2020 data).

See previous articles for SQL. Each year has been aggregated for each type of crime.

To visualise this in Flourish, I will convert to a style that fits the templates. Of course, there is functionality within Flourish, but I'm not a heavy user, so it's quicker for me to get the data source into a form suitable for templates.

So which templates do I use?

The four types above are similar.

In Flourish graphs, the 'horizontal stretch graph' (flag-like) is represented by Bar and the 'vertical stretch graph' (bamboo-shoot-like) is represented by Column.

In Tableau and Exploratry, the vertical/horizontal conversion is in the palette, so you can start with either, but in Flourish, the X-axis and Y-axis types are also included in the templates, so you can see what's inside.


[Bar chart (stacked) templates]

Bar graph (stacked) of sample contents. The top row is the preview and the bottom row is the data screen. Flourish is a style for specifying labels and rows for calculations on the data screen.

If you convert the horizontal to vertical, the labels in the bottom right corner will no longer be available (the corresponding labels will be flipped) and you will need to re-set them.

In this example, the X-axis is SUM and the Y-axis is COUNTRY. It looks different because what I want to create is SUM on the X-axis and Year on the Y-axis.


[Column chart (stacked) templates]

The x-axis is the country and the y-axis is the SUM (B+C). This is closer to the image. This one.


Platform template

This [Bar] and [Column], the data is the same.

But the shape of the graph does not change with the vertical and horizontal conversion. That's because the visualisation process within Flourish is different, even though it's the same data array.

So in Flourish it is faster to make your own data source closer to the template. In other words, because the conversion process is fixed in each template, the visuals that work on this stage can be expressed immediately.


Then I realise. This is... the current data can't represent my image.

  • Use a column chart for comparison
  • Year (2010-2020) on the X axis
  • Y-axis shows total number of crime types stacked by year

In other words, on the Flourish data screen

  • Year (2010-2020) in column A (X-axis)
  • Column B onwards: number of offences by type and total number of offences
  • Row 1 (at the top of the table) is year, total and each type of crime


Convert from long data of 'year' x 'type of crime' x 'number of crimes' to wide data of 'year' x 'number of types of crime'. To be continue with (2).