棒グラフ, 積み上げ棒グラフ

カテゴリカルデータの為の棒グラフを描いてく.

References

基本的な棒グラフ

with boxes

plot ..
    using <x>:<y>{:xticlabels(<xlabel>)}{:yticlabels(<ylabel>)}
    with boxes

で折れ線グラフが棒グラフになる. xticlabels とか xticlabel とか xtic はただの省略形で全て同じ意味.

set boxwidth

set boxwidth <width> {relative|absolute}

棒の幅を指定できる. relative はデータ間隔に対する相対的な幅を指定し, absolute は今の x 軸に関して絶対的な幅を指定する. 特に対数スケールを x 軸に指定している場合に absolute を使うと幅にも対数スケールが適用されるので注意.

set boxwidth をしていない初期状態は set boxwidth 1 relative された状態になる. relativeabsolute も指定せず set boxwidth <width> とした場合は absolute 扱いになる.

set style fill

set style fill
    {transparent} solid
    {border lc rgb <color>}
params default value explanation
transparent -- -- 透明度を有効にする (1.0 で不透明)
border -- -- 棒の枠線を描画する
color -- (color) 枠線の色

with boxes で描画される棒の塗りつぶしのスタイルを指定する. ただし, この color は枠線の色を指定するものであり, 塗りつぶしの色を指定するものではないことに注意. 塗りつぶしの色は fillcolor オプションや ls オプションで指定する.

with boxes fillcolor

plot ..
    with boxes fillcolor <color>

Examples

最も簡素な例

$data <<EOD
apple 100
banana 200
candy 250
EOD

set yrange [0:300]
set boxwidth 0.8 relative
plot $data with boxes
Gnuplot Produced by GNUPLOT 6.0 patchlevel 0 0 50 100 150 200 250 300 apple banana candy $data using 0:2:xtic(1) $data using 0:2:xtic(1)

デフォルトではこんな見た目.

using によって \(x\) はデータの行番号 (0, 1, 2, ..), \(y\) は 2 列目の値 (100, 200, 250), xticlabel(1) ( xtic ) によって x 軸の目盛ラベルが 1 列目の値 (apple, banana, candy) になる.

枠線や塗りつぶしの色を指定する

$data <<EOD
apple 100
banana 200
candy 250
EOD

set yrange [0:300]
set boxwidth 0.8 relative
set style fill solid 0.8 border lc rgb 'black'
plot $data using 0:2:xtic(1) with boxes fillcolor '#55aaff'
Gnuplot Produced by GNUPLOT 6.0 patchlevel 0 0 50 100 150 200 250 300 apple banana candy $data using 0:2:xtic(1) $data using 0:2:xtic(1)

set style によって枠線の色や塗りつぶしの透明度を指定. fillcolor によって塗りつぶしの色を指定した.

Clustered histogram

いくつかのクラスタに横断するパラレルデータに関する棒グラフを描く. 例えば果物ごとというクラスタに横断する「輸入額」と「輸出額」など.

時代は with boxes から with histogram

plot .. using <y>{:xtic(<xlabel>)}{:ytic(<ylabel>)} with histogram

<x> を与えないことに注意.

set style histogram clustered {gap <gapsize>}

cluster モードを有効にする. <gapsize> はクラスタ間の隙間の大きさを指定する (デフォルトは 2).

Examples

# <item>, <import-amount>, <export-amount>
$data <<EOD
apple  100 150
banana 200 180
candy  250  10
EOD

set style histogram clustered gap 1

set yrange [0:300]
set boxwidth 0.8 relative
set style fill solid 0.8 border lc rgb 'black'

# 塗りつぶしの色
set style line 1 lc rgb '#55aaff'
set style line 2 lc rgb '#ffaa55'

plot $data using 2:xtic(1) with histogram ls 1 title 'import amount' ,\
     ''    using 3         with histogram ls 2 title 'export amount'
Gnuplot Produced by GNUPLOT 6.0 patchlevel 0 0 50 100 150 200 250 300 apple banana candy import amount import amount export amount export amount

積み上げ棒グラフ (Rowstacked histogram)

Clustered histogram が横に並べるなら, 積み上げ棒グラフは縦に積み上げる.

rowstacked

set style histogram rowstacked

積み上げ棒グラフモードを有効にする.

先の例

# <item>, <import-amount>, <export-amount>
$data <<EOD
apple  100 150
banana 200 180
candy  250  10
EOD

を行列と見たとき, 各行ごとに積み上げ棒グラフが描かれる.

関連するものに columnstacked があるが, ここでは扱わない.

Examples

$data <<EOD
apple  100 150
banana 200 180
candy  250  10
EOD

set style histogram rowstacked

set grid ytics
set yrange [0:]
set boxwidth 0.8 relative
set style fill solid 0.8 border lc rgb 'black'

# 塗りつぶしの色
set style line 1 lc rgb '#55aaff'
set style line 2 lc rgb '#ffaa55'

plot $data using 2:xtic(1) with histogram ls 1 title 'import amount' ,\
     ''    using 3         with histogram ls 2 title 'export amount'
Gnuplot Produced by GNUPLOT 6.0 patchlevel 0 0 50 100 150 200 250 300 350 400 apple banana candy import amount import amount export amount export amount