横向き棒グラフ

概要

横向きの棒グラフを描画したい. 通常の with boxes による棒グラフの描画は必ず縦向きであるが カテゴリカルデータであって x 軸にラベルを表示する場合, そのラベルが長いとラベル同士が重なったり見切れてしまい見づらい. y 軸にラベルを表示して棒グラフを横向き(水平方向)にすることでこれを解決する.

つまり,

Gnuplot Produced by GNUPLOT 6.0 patchlevel 0 0 50 100 150 200 250 300 apple banana candy gnuplot_plot_1

これを

Gnuplot Produced by GNUPLOT 6.0 patchlevel 0 apple banana candy 0 50 100 150 200 250 $data using (0.5*$2):0:(0.5*$2):(myboxwidth/2.):ytic(1)

こうしたい.

参考

手法

直接に横向きの棒グラフを描画する機能は gnuplot にはない. しかし比較的自由に矩形を描画することは with boxxyerror を使うことで可能. これを利用(悪用)して横向きの棒グラフを描画する.

plot .. using <x>:<y>:<xdelta>:<ydelta> with boxxyerror
plot .. using <x>:<y>:<xlow>:<xhigh>:<ylow>:<yhigh> with boxxyerror
params default value explanation
x -- (double) x 矩形の中心の x 座標
y -- (double) y 矩形の中心の y 座標
xdelta -- (double) 矩形の幅の半分
ydelta -- (double) 矩形の高さの半分
xlow -- (double) 矩形の左端の x 座標
xhigh -- (double) 矩形の右端の x 座標
ylow -- (double) 矩形の下端の y 座標
yhigh -- (double) 矩形の上端の y 座標

xdelta , ydelta で矩形の幅と高さを指定することを考える. 値 \(x=100\) の apple を \(y=0\) に描画するには

とすればよい.

set offsets <left>, <right>, <top>, <bottom>

描画範囲は gnuplot によって自動的に決定されるが, set offsets で余白を設定することができる.

Examples

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

# tics style
set style line 11 lc rgb '#808080' lt 1 lw 3
set border 0 back ls 11
set tics out nomirror
set style line 12 lc rgb '#808080' lt 0 lw 1

# grid style
set grid back ls 12

# box style
set style fill solid 0.5 border lc rgb '#55aaff'
set style line 1 lc rgb '#77ccff'

set yrange [0:*]
set xrange [0:*]
set style fill solid
unset key

boxheight = 0.5
set offsets 0,0,0.5-boxheight/2.,0.5

plot $data using (0.5*$2):0:(0.5*$2):(boxheight/2.):ytic(1) with boxxyerror fillcolor ls 1
Gnuplot Produced by GNUPLOT 6.0 patchlevel 0 apple banana candy 0 50 100 150 200 250 $data using (0.5*$2):0:(0.5*$2):(boxheight/2.):ytic(1)