1
votes

Here is my data set:

type size name
label_0 1 nameOfData_0
label_0 2 nameOfData_1
label_0 3 nameOfData_2
label_1 2 nameOfData_3
label_2 1 nameOfData_4
label_0 2 nameOfData_5
label_1 3 nameOfData_6
label_3 2 nameOfData_7
label_3 1 nameOfData_8

I would like the plot to looks like:

enter image description here

I would like each label to be a stack and each nameOfData_X to fit in the correct stack according to its size. If possible add also the legend for each element of the stack.

I know I could reformat the data to process it easily via gnuplot but I don't want to.

Any ideas on how I could display this graph via gnuplot?

Thanks for your help!

1

1 Answers

0
votes

Maybe there is a way to achieve this with the gnuplot built-in stacked histogram style, check help histograms. The following solution is not too obvious but seems to give the desired result and uses the plotting style with boxxyerror (check help boxxyerror).

  1. you need a list of unique elements of column 1. Here it will be in the order of the first occurrences.
  2. during plotting in loops you add your contributions depending on a "filter" function myAdd().
  3. you change the color when the addition is zero (dy=0).
  4. you add labels in a similar way with an offset

This code can probably be simplified but may act as a starting point.

Code:

### "manual" stacked histogram
reset session


$Data <<EOD
type size name
label_0 1 nameOfData_0
label_0 2 nameOfData_1
label_0 3 nameOfData_2
label_1 2 nameOfData_3
label_2 1 nameOfData_4
label_0 2 nameOfData_5
label_1 3 nameOfData_6
label_3 2 nameOfData_7
label_3 1 nameOfData_8
EOD

# get a unique list from datablock column 1
set table $Dummy
    Headers = 1
    addToList(list,col) = list.( strstrt(list,'"'.strcol(col).'"') > 0 ? '' : \
                          ' "'.strcol(col).'"')
    plot Uniques='' $Data u (Uniques=addToList(Uniques,1),'') skip Headers w table
unset table
N = words(Uniques)
Unique(i) = word(Uniques,i)

set xrange [1:N]
set xtics out noenhanced
set grid x,y
set offsets 0.5,0.5,0.5,0
unset key

set style fill transparent solid 0.7 border
myBoxWidth = 0.8
myAdd(colD,colF,i) = strcol(colF) eq Unique(i) ? column(colD) : 0
myLabel(col1,col2) = dy==0 ? '' : sprintf("%s\n%g",strcol(col1),column(col2))

plot for [i=1:N] y=y0=(c=1,0) $Data u (i):(dy=myAdd(2,1,i), y=y+dy,(y0+y)/2.): \
         (myBoxWidth/2.):(y0=y,dy/2.):(dy==0?c:c=c+1) skip Headers w boxxy lc variable, \
     for [i=1:N] y=y0=0 $Data u (i):(dy=myAdd(2,1,i), y=y+dy,(y0+y)/2.): \
         (y0=y,myLabel(3,2)):xtic(Unique(i)) skip Headers w labels offset 0,0.5 noenhanced
### end of code

Result:

enter image description here