トレンドライン

概要

トレンドラインとは、データの全体的な方向を示すグラフに重ねて表示される線です。Google グラフでは、散布図、棒グラフ、縦棒グラフ、折れ線グラフのトレンドラインを自動的に生成できます。

Google グラフでは、線形、多項式、指数の 3 種類のトレンドラインがサポートされています。

線形トレンドライン

線形のトレンドラインは、グラフのデータを最も近い直線です。(正確には、すべてのポイントから距離までの二乗距離の合計を最小化する線です)。

下のグラフでは、散布図上の直線トレンドラインでトウガラシの年齢とトランクの直径を比較できます。トレンドラインにカーソルを合わせると、Google グラフによって計算された式(直径 4.885 + 0.730)を確認できます。

グラフにトレンドラインを描画するには、trendlines オプションを使用して、使用するデータ系列を指定します。

google.charts.setOnLoadCallback(drawChart);
function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['Diameter', 'Age'],
    [8, 37], [4, 19.5], [11, 52], [4, 22], [3, 16.5], [6.5, 32.8], [14, 72]]);

  var options = {
    title: 'Age of sugar maples vs. trunk diameter, in inches',
    hAxis: {title: 'Diameter'},
    vAxis: {title: 'Age'},
    legend: 'none',
    trendlines: { 0: {} }    // Draw a trendline for data series 0.
  };

  var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));
  chart.draw(data, options);
}

線形トレンドラインは、最も一般的なトレンドラインです。ただし、データを説明するために曲線が最適となる場合があります。その場合は、別の種類のトレンドラインが必要です。

指数トレンドライン

データが eax+b 形式の指数で最もよく説明される場合は、次のように type 属性を使用して指数トレンドラインを指定できます。

注: 線形トレンドラインとは異なり、指数トレンドラインを計算する方法はいくつかあります。現時点では 1 つのメソッドしか提供していませんが、将来的にはさらにサポートする予定です。これにより、現在の指数トレンドの名前や動作が変更される可能性があります。

このグラフでは、visibleInLegend: true を使用して凡例に指数曲線を表示します。

google.charts.setOnLoadCallback(drawChart);
function drawChart() {
  var data = google.visualization.arrayToDataTable([
    ['Generation', 'Descendants'],
    [0, 1], [1, 33], [2, 269], [3, 2013]
 ]);

  var options = {
    title: 'Descendants by Generation',
    hAxis: {title: 'Generation', minValue: 0, maxValue: 3},
    vAxis: {title: 'Descendants', minValue: 0, maxValue: 2100},
    trendlines: {
      0: {
        type: 'exponential',
        visibleInLegend: true,
      }
    }
  };

  var chart = new google.visualization.ScatterChart(document.getElementById('chart_div2'));
  chart.draw(data, options);
}

色を変更する

デフォルトでは、トレンドラインの色はデータ系列と同じですが、明るい色です。これは color 属性でオーバーライドできます。ここでは、計算で実質的な期間に年ごとに計算された円の桁数をグラフ化し、指数関数的なトレンドラインを緑色にしています。

トレンドラインの仕様は次のとおりです。

    trendlines: {
      0: {
        type: 'exponential',
        color: 'green',
      }
    }

多項式トレンドライン

多項式トレンドラインを生成するには、polynomial タイプと degree を指定します。誤解を招く可能性がありますので、慎重に使用してください。以下の例では、ほぼ線形のデータセットが 3 次トレンドラインでプロットされています。

最後のデータポイントより後の落下は、横軸を人為的に 15 に人為的に増やした場合にのみ表示されています。hAxis.maxValue を 15 に設定しないと、以下のようになります。

同じデータ、同じ多項式、異なるウィンドウ。

オプション
  var options = {
    title: 'Age vs. Weight comparison',
    legend: 'none',
    crosshair: { trigger: "both", orientation: "both" },
    trendlines: {
      0: {
        type: 'polynomial',
        degree: 3,
        visibleInLegend: true,
      }
    }
  };
完全な HTML
<html>
 <head>
   <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   <script type="text/javascript">
     google.charts.load("current", {packages:["corechart"]});
     google.charts.setOnLoadCallback(drawChart);
     function drawChart() {
       var data = google.visualization.arrayToDataTable([
         ['Age', 'Weight'],
         [ 8,      12],
         [ 4,      5.5],
         [ 11,     14],
         [ 4,      5],
         [ 3,      3.5],
         [ 6.5,    7]
       ]);

       var options = {
         title: 'Age vs. Weight comparison',
         legend: 'none',
         crosshair: { trigger: 'both', orientation: 'both' },
         trendlines: {
           0: {
             type: 'polynomial',
             degree: 3,
             visibleInLegend: true,
           }
         }
       };

       var chart = new google.visualization.ScatterChart(document.getElementById('polynomial2_div'));
       chart.draw(data, options);
     }
   </script>
 </head>
 <body>
  <div id='polynomial2_div' style='width: 900px; height: 500px;'></div>
 </body>
</html>

不透明度と線の太さを変更する

トレンドラインの透明度を変更するには、opacity を 0.0 ~ 1.0 の値に、ラインの幅を lineWidth オプションに設定します。

    trendlines: {
      0: {
        color: 'purple',
        lineWidth: 10,
        opacity: 0.2,
        type: 'exponential'
      }
    }

ほとんどの場合は lineWidth オプションで十分ですが、外観を見たい場合は pointSize オプションを使用して、トレンドライン内の選択可能なドットのサイズをカスタマイズできます。

光が波でかつ粒子であるように、トレンドラインは線と点群の両方です。ユーザーに表示されたものは、ユーザーによる操作の方法によって異なります。通常は線ですが、トレンドラインにカーソルを合わせると、特定のポイントがハイライト表示されます。このとき、直径は次と等しくなります。

  • トレンドライン pointSize(定義されている場合)またはそれ以外の...
  • グローバルの場合は pointSize、それ以外の場合は...
  • 7

ただし、グローバル オプションかトレンドライン pointSize オプションのいずれかを設定すると、トレンドラインの lineWidth とは別個に、選択可能なすべてのポイントが表示されます。

オプション
  var options = {
    legend: 'none',
    hAxis: { ticks: [] },
    vAxis: { ticks: [] },
    pointShape: 'diamond',
    trendlines: {
      0: {
        type: 'polynomial',
        degree: 3,
        visibleInLegend: true,
        pointSize: 20, // Set the size of the trendline dots.
	opacity: 0.1
      }
    }
  };
完全な HTML
<html>
  <head>
  <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  <script type="text/javascript">
    google.charts.load("current", {packages:["corechart"]});
    google.charts.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['X', 'Y'],
          [0, 4],
          [1, 2],
          [2, 4],
          [3, 6],
          [4, 4]
        ]);

        var options = {
          legend: 'none',
          hAxis: { ticks: [] },
          vAxis: { ticks: [] },
          colors: ['#703583'],
          pointShape: 'diamond',
          trendlines: {
            0: {
              type: 'polynomial',
              degree: 3,
              visibleInLegend: true,
              pointSize: 20, // Set the size of the trendline dots.
              opacity: 0.1
            }
          }
      };

      var chart = new google.visualization.ScatterChart(document.getElementById('chart_pointSize'));

      chart.draw(data, options);
    }
    </script>
  </head>
  <body>
    <div id="chart_pointSize" style="width: 900px; height: 500px;"></div>
  </body>
</html>

ポイントを表示する

トレンドラインは、グラフに多くのドットを打ち込むことで構成されます。トレンドラインの pointsVisible オプションでは、特定のトレンドラインのポイントを表示するかどうかを決定します。すべてのトレンドラインのデフォルト オプションは true ですが、最初のトレンドラインのポイントの表示をオフにする場合は、trendlines.0.pointsVisible: false を設定します。

下のグラフは、各地点の視認性を傾向ごとにコントロールする方法を示したものです。

オプション
        var options = {
          height: 500,
          legend: 'none',
          colors: ['#9575cd', '#33ac71'],
          pointShape: 'diamond',
          trendlines: {
            0: {
              type: 'exponential',
              pointSize: 20,
              opacity: 0.6,
              pointsVisible: false
            },
            1: {
              type: 'linear',
              pointSize: 10,
              pointsVisible: true
            }
          }
        };
    
完全な HTML
<html>
  <head>
    <meta charset="utf-8"/>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {packages:['corechart']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['X', 'Y', 'Z'],
          [0, 4, 5],
          [1, 2, 6],
          [2, 4, 8],
          [3, 6, 10],
          [4, 4, 11],
          [5, 8, 13],
          [6, 12, 15],
          [7, 15, 19],
          [8, 25, 21],
          [9, 34, 23],
          [10, 50, 27]
        ]);

        var options = {
          height: 500,
          legend: 'none',
          colors: ['#9575cd', '#33ac71'],
          pointShape: 'diamond',
          trendlines: {
            0: {
              type: 'exponential',
              pointSize: 20,
              opacity: 0.6,
              pointsVisible: false
            },
            1: {
              type: 'linear',
              pointSize: 10,
              pointsVisible: true
            }
          }
        };

        var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));

        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="chart_div"></div>
  </body>
</html>

    

ラベルを変更する

デフォルトでは、visibleInLegend を選択すると、ラベルはトレンドラインの式を表示します。labelInLegend を使用して別のラベルを指定できます。ここでは、2 つのシリーズのトレンドラインを表示し、凡例のラベルを「バグライン」(シリーズ 0 の場合)と「テストライン」(シリーズ 1 の場合)に設定します。

    trendlines: {
      0: {
        labelInLegend: 'Bug line',
        visibleInLegend: true,
      },
      1: {
        labelInLegend: 'Test line',
        visibleInLegend: true,
      }
    }

相関関係

統計学では R2 と呼ばれる決定係数により、トレンドラインがデータにどの程度一致するかが識別されます。完全な相関は 1.0、完全な反相関は 0.0 です。

グラフの凡例で R2 を表すには、showR2 オプションを true に設定します。

<html>
  <head>
    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Age', 'Weight'],
          [ 8,      12],
          [ 4,      5.5],
          [ 11,     14],
          [ 4,      5],
          [ 3,      3.5],
          [ 6.5,    7]
        ]);

        var options = {
          hAxis: {minValue: 0, maxValue: 15},
          vAxis: {minValue: 0, maxValue: 15},
          chartArea: {width:'50%'},
          trendlines: {
            0: {
              type: 'linear',
              showR2: true,
              visibleInLegend: true
            }
          }
        };

        var chartLinear = new google.visualization.ScatterChart(document.getElementById('chartLinear'));
        chartLinear.draw(data, options);

        options.trendlines[0].type = 'exponential';
        options.colors = ['#6F9654'];

        var chartExponential = new google.visualization.ScatterChart(document.getElementById('chartExponential'));
        chartExponential.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="chartLinear" style="height: 350px; width: 800px"></div>
    <div id="chartExponential" style="height: 350px; width: 800px"></div>
  </body>
</html>