Search code examples
plotgnuplothistogramstacked

Labels per category and per bar for clustered stacked histogram


Below is the gnuplot code that creates clustered stacked histogram.

set terminal postscript eps enhanced 14
set datafile separator ";"
set output 'stacked_boxes.eps'
set auto x
set yrange [0:30]
set xtics 1
set style fill solid border -1
num_of_categories=2
set boxwidth 0.3/num_of_categories
dx=0.2
offset=-0.1
ddx=0.2
set key off
plot 'data.explorer0.csv' using ($1+offset):($3+$4) title "explorer0(sync)" linecolor rgb "#cc0000" with boxes, \
     ''                   using ($1+offset):3 title "explorer0(wait)" linecolor rgb "#ff0000" with boxes, \
     'data.explorer1.csv' using ($1+offset+dx):($3+$4) title "explorer1(sync)" linecolor rgb "#cc0000" with boxes, \
     ''                   using ($1+offset+dx):3 title "explorer1(wait" linecolor rgb "#ff0000" with boxes, \
     'data.collector0.csv' using ($1+dx+ddx):($3+$4) title "collector0(post)" linecolor rgb "#00cc00" with boxes, \
     ''                   using ($1+dx+ddx):3 title "collector0(poll)" linecolor rgb "#00ff00" with boxes, \
     'data.collector1.csv' using ($1+dx+ddx+dx):($3+$4)  linecolor rgb "#00cc00" with boxes, \
     ''                   using ($1+dx+ddx+dx):3 notitle linecolor rgb "#00ff00" with boxes here

How can I create labels for both per bar and per category? The four bars together make a category. Let the categories be (c0, c1, c2) and the bars per category be (e0, e1, c0, c1).

The plot can be reproduced using following data files:

data.explorer0.csv:

#level;explorerid;sync;wait;etc
"0";"e0";"2";"2"
"2";"e0";"4";"4"
"4";"e0";"6";"6"

data.explorer1.csv:

#level;explorerid;sync;wait;etc
"0";"e1";"7";"5"
"2";"e1";"6";"10"
"4";"e1";"5";"5"

data.collector0.csv:

#level;collectorid;sync;wait;etc
"0";"c0";"2";"2"
"2";"c0";"4";"4"
"4";"c0";"6";"6"

data.collector1.csv:

#level;collectorid;sync;wait;etc
"0";"c1";"5";"5"
"2";"c1";"8";"10"
"4";"c1";"9";"5"

stacked clustered plot


Solution

  • I've found the source of your script: Gnuplot Histogram Cluster (Bar Chart) with One Line per Category :) I had thought about a solution like solution 2 mentioned in that answer, but that doesn't work properly with the red and green colors beneath each other plus stacking the data.

    The script you posted needs only very little modifications. In order to put the labels contained in the second column as xticklabels, you can use the xtic function in the using statement.

    The category labels can be placed properly only explicitely with set labels. The x-coordinate of the label is given in units of the first x-axis, the y-value as absolute value in character units. Additionally you need to manually increase the bottom margin to make space for the category labels, which aren't accounted in the automatic margin calculations. This is done with set bmargin 3, which sets the bottom margin to three character heights.

    Here is the modified script

    set terminal postscript eps enhanced 14
    set datafile separator ";"
    set output 'stacked_boxes.eps'
    set auto x
    set yrange [0:30]
    set xtics 1 nomirror
    set style fill solid border -1
    num_of_categories=2
    set boxwidth 0.3/num_of_categories
    dx=0.2
    offset=-0.1
    ddx=0.2
    set key off
    
    set bmargin 3
    set label center "cat0" at first 0+offset/2+dx+ddx/2, char 1
    set label center "cat1" at first 2+offset/2+dx+ddx/2, char 1
    set label center "cat2" at first 4+offset/2+dx+ddx/2, char 1
    
    plot 'data.explorer0.csv' using ($1+offset):($3+$4):xtic(2) title "explorer0(sync)" linecolor rgb "#cc0000" with boxes, \
         ''                   using ($1+offset):3 title "explorer0(wait)" linecolor rgb "#ff0000" with boxes, \
         'data.explorer1.csv' using ($1+offset+dx):($3+$4):xtic(2) title "explorer1(sync)" linecolor rgb "#cc0000" with boxes, \
         ''                   using ($1+offset+dx):3 title "explorer1(wait" linecolor rgb "#ff0000" with boxes, \
         'data.collector0.csv' using ($1+dx+ddx):($3+$4):xtic(2) title "collector0(post)" linecolor rgb "#00cc00" with boxes, \
         ''                   using ($1+dx+ddx):3 title "collector0(poll)" linecolor rgb "#00ff00" with boxes, \
         'data.collector1.csv' using ($1+dx+ddx+dx):($3+$4):xtic(2)  linecolor rgb "#00cc00" with boxes, \
         ''                   using ($1+dx+ddx+dx):3 notitle linecolor rgb "#00ff00" with boxes
    

    and its output using gnuplot 4.6.5:

    enter image description here