0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rubyで散布図行列(ペアプロット)を作る

Last updated at Posted at 2019-02-12

はじめに

Rubyでペアプロットを作る場合のスニペット。

image.png

Google colaboratory で実行した場合
(改良前なので若干上と画像が違います)
image.png

環境

(Jupyter Notebook や IRuby の導入は済んでいるものとします)
あと、一応Google Colaboratoryを使うという手段もあります。

iris 読み込み

ここではrdatasets Gemを使う。

require 'daru'
require 'daru/view'
require 'rdatasets'

iris = RDatasets.load :datasets, :iris
iris.head(3)

Daru::View.plotting_library = :googlecharts # Google Colab ではその都度指定する必要あり

image.png

ペアプロットを描く その1

# Daru::View.plotting_library = :googlecharts # Google Colab ではその都度指定する必要あり

vector_names = iris[0..3].vectors.to_a

vector_names.each do |row|
  plots = vector_names.map do |column|
    if row != column
      Daru::View::Plot.new(iris[column, row],
                           type: :scatter,
                           height: 100,
                           width: 100,
                           pointSize: 1)
    else
      Daru::View::Plot.new(iris[row],
                           title: row,
                           height: 100,
                           width: 100,
                           type: :histogram)
    end
  end
  IRuby.display Daru::View::PlotList.new(plots).show_in_iruby
end

普通に頑張ってループまわして描いた感じです。これでも実用にたえると思いますが、見た目がややチープですね。
image.png

ペアプロットを描く その2

とりあえず見た目を改善します。
(なお「見た目」とは、コードの見た目ではなく、プロットの見た目のことを指しています)

# Daru::View.plotting_library = :googlecharts # Google Colab ではその都度指定する必要あり

vector_names = iris[0..3].vectors.to_a

opts = {
  chartArea: { left: 5, right: 5, top: 5, bottom: 5 },
  height: 100,
  width: 100,
  hAxis: {viewWindowMode: "maximized"},
  vAxis: {viewWindowMode: "maximized"},
  backgroundColor: '#f0f0f0',
  gridlineColor: 'white',
  colors: ['#4784BF']
}

vector_names.each do |row|
  plots = vector_names.map do |column|
    if row != column

      Daru::View::Plot.new(iris[column, row], opts.merge(
                                                type: :scatter,
                                                pointSize: 1
                                              ))
    else
      Daru::View::Plot.new(iris[row], opts.merge(
                                        title: row,
                                        titlePosition: 'in',
                                        type: :histogram
                                      ))
    end
  end
  IRuby.display Daru::View::PlotList.new(plots).show_in_iruby
end

Seabornをリスペクトしてみました。先程よりも見やすくなったかと思います。

image.png

関数にしてみた

ほとんど何も変わっていない。

def pair_plot(df, width: 100, height: 100, color: '#4784BF')
  vector_names = df.only_numerics.vectors.to_a

  opts = {
    chartArea: { left: 5, right: 5, top: 5, bottom: 5 },
    height: height,
    width: width,
    hAxis: { viewWindowMode: 'maximized' },
    vAxis: { viewWindowMode: 'maximized' },
    backgroundColor: '#f0f0f0',
    gridlineColor: 'white',
    colors: [color],
    adapter: :googlecharts
  }

  vector_names.each do |row|
    plots = vector_names.map do |column|
      if row != column

        Daru::View::Plot.new(
          df[column, row], opts.merge(type: :scatter,
                                      pointSize: 1)
        )
      else
        Daru::View::Plot.new(
          df[row], opts.merge(title: row,
                              titlePosition: 'in',
                              type: :histogram)
        )
      end
    end
    IRuby.display Daru::View::PlotList.new(plots).show_in_iruby
  end
end

Rのデータセットをペアプロットしてみよう

USArrests

require 'rdatasets'
df = RDatasets.datasets.USArrests
pair_plot(df, color: "green")

image.png

fgl

df = RDatasets.MASS.fgl
pair_plot(df, height:60, width: 60)

image.png

こんな感じで、描けるか描けないかでいったら、描ける。

おまけ

Species ごとに平均などを見たい場合は group_by

iris.Species.to_category
hoge = iris.group_by ["Species"]
hoge.mean
# hoge.max
# hoge.min
# hoge.median

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?