1
votes

I have stacked bar-charts with gnuplot. Currently I fetch only the data file and and set the title of data manually. Since I do sorting my data base on amount of data, it tax time to re type and look at the order.

This is my Gnuplot file:

set term pos eps font 20
set style data histogram
set style histogram rowstacked
set style fill solid border -1
set key invert reverse right outside 
set boxwidth 0.75
set format y "%.0f%%"
set style line 2 lc rgb "#EDEBE4" lt 1 lw 2
set style line 3 lc rgb "#A7ABA6" lt 1 lw 2
set title "Classification"

set ylabel "Percentage"
set xlabel "System"
set yrange [0:100]
set output 'output.eps'
plot 'datafile' \
    using($2):xtic(1)   t "stack-1" lt -1 fs pattern 3 , \
''  using($3)           t "stack-2" lt -1 fs pattern 2, \
''  using($4)           t "stack-3" lt -1 fs pattern 5, \
''  using($5)           t "stack-4" lt -1 fs pattern 9, \
''  using($6)           t "stack-5" ls 3, \
''  using($7)           t "stack-6" lt -1 fs pattern 6, \
''  using($8)           t "stack-7" lt -1 fs pattern 4, \
''  using($9)           t "stack-8" ls 2   

this is my current datafile:

CS  35.08   33.12   22.49   3.72    2.73    1.03    1.76    0.08
FL  58.22   9.36    21.46   4.34    3.65    2.97    0.00    0.00
HB  40.27   19.29   18.52   14.37   6.13    0.91    0.29    0.21
HD  30.32   22.51   31.63   1.10    9.51    2.53    1.50    0.90
MR  34.65   24.37   15.59   7.46    15.42   1.56    0.66    0.29
ZK  29.65   18.54   30.63   6.91    9.46    1.28    2.85    0.68
All 36.74   23.88   22.01   7.40    7.18    1.42    1.06    0.31

I have to change my datafile to be like this:

    stacked-1   stack-2 stack-3 stack-4 stack-5 stack-6 stack-7 stack-8
CS  35.08   33.12   22.49   3.72    2.73    1.03    1.76    0.08
FL  58.22   9.36    21.46   4.34    3.65    2.97    0.00    0.00
HB  40.27   19.29   18.52   14.37   6.13    0.91    0.29    0.21
HD  30.32   22.51   31.63   1.10    9.51    2.53    1.50    0.90
MR  34.65   24.37   15.59   7.46    15.42   1.56    0.66    0.29
ZK  29.65   18.54   30.63   6.91    9.46    1.28    2.85    0.68
All 36.74   23.88   22.01   7.40    7.18    1.42    1.06    0.31

output:

enter image description here

How to create barchart title/Legend to be automatically from my first row in my datafile?. Also My script still need to use pattern style manually. Thanks!

1

1 Answers

1
votes

With title columnhead(2) you can select the header of a certain column to be used as key entry. So your plot command becomes

plot 'datafile' \
    using 2:xtic(1) title columnhead(1) lt -1 fs pattern 3 , \
''  using 3         title columnhead(2) lt -1 fs pattern 2, \
''  using 4         title columnhead(3) lt -1 fs pattern 5, \
''  using 5         title columnhead(4) lt -1 fs pattern 9, \
''  using 6         title columnhead(5) ls 3, \
''  using 7         title columnhead(6) lt -1 fs pattern 6, \
''  using 8         title columnhead(7) lt -1 fs pattern 4, \
''  using 9         title columnhead(8) ls 2   

That is quite verbose, because your first column doesn't have a header so that the column from which the header is selected is off by one from the column which is used for the values.

If you would insert a dummy header for the first column, then it is enough so use set key autotitle columnheader.

Change datafile to

desc stacked-1   stack-2 stack-3 stack-4 stack-5 stack-6 stack-7 stack-8
CS  35.08   33.12   22.49   3.72    2.73    1.03    1.76    0.08
FL  58.22   9.36    21.46   4.34    3.65    2.97    0.00    0.00
HB  40.27   19.29   18.52   14.37   6.13    0.91    0.29    0.21
HD  30.32   22.51   31.63   1.10    9.51    2.53    1.50    0.90
MR  34.65   24.37   15.59   7.46    15.42   1.56    0.66    0.29
ZK  29.65   18.54   30.63   6.91    9.46    1.28    2.85    0.68
All 36.74   23.88   22.01   7.40    7.18    1.42    1.06    0.31

and then use

set key invert reverse right outside autotitle columnheader
plot 'datafile' \
    using 2:xtic(1) lt -1 fs pattern 3 , \
''  using 3         lt -1 fs pattern 2, \
''  using 4         lt -1 fs pattern 5, \
''  using 5         lt -1 fs pattern 9, \
''  using 6         ls 3, \
''  using 7         lt -1 fs pattern 6, \
''  using 8         lt -1 fs pattern 4, \
''  using 9         ls 2