How to set the columnhead below each boxplot?

36 Views Asked by At

I have a little code, that allows plotting data from multiple columns in one file for a boxplot.

    \begin{gnuplot}[terminal=tikz, terminaloptions={color size 10cm,5cm}]
        reset session
        
        datafile = 'boite2.txt'
        colMin = 1
        colMax = 3
        
        set datafile separator '\t'
        plot for [col=colMin:colMax] datafile using (col):col w boxplot notitle
\end{gnuplot}

Now, I would like to have the columnheader (...-Medium) of each column to be shown below each box on the x-axis. But I couldn't find any working solution. I've used this data

TB-Medium   LB-Medium   XX-Medium
42.9    42.9    42.9
38.1    38.1    38.1
33.3    33.3    33.3
37.1    37.1    37.1
34.8    34.8    34.8
36.6    36.6    36.6
37.4    37.4    37.4
35.1    35.1    35.1
39.8    39.8    39.8
45.8    45.8    45.8
57.8    57.8    57.8
42.7    42.7    42.7

What I get is this with each boxplot numbered from 1 to ... enter image description here

I would really appreciate some help. And maybe you can suggest where to find examples or documentation to that specific matter.

I'm using gnuplottex in LaTeX with gnuplot 5.4.

3

There are 3 best solutions below

2
Ethan On BEST ANSWER

Yet a third alternative. This one is, I think, what the program actually intended.

The boxplot style allows for up to 4 input columns in the using specifier. These are (x position) : (data) : (box width) : (category). The 3rd and 4th columns are optional. If a 4th input column is given, the program generates a separate boxplot for each category and places the category string underneath as a label. See help style boxplot. For your case we can use the column header as a single "category" that applies to all the data points in that column:

set datafile separator '\t'
unset key

datafile = 'boite2.txt'
colMin = 1
colMax = 3
width = 0.5

plot for [col=colMin:colMax] \
     datafile using (col):col:(width):(columnhead(col)) w boxplot

enter image description here

0
Ethan On

There are probably several ways to do this. Here is a method that loads an array of titles as a side effect of the with boxplots component of the plot command and then adds a second component with labels to display them. It has the disadvantage of requiring a known y coordinate for the labels. You could determine that via a separate stats command before plotting.

datafile = 'boite2.txt'
colMin = 1
colMax = 3

# left-hand y border only; no x borders
set border 2
unset xtics
set ytics rangelimited nomirror
unset key 

# array of titles generated from column headers
array Title[colMax]
title_y = 30

# serial evaluation used to set array entry and return index 
maketitle(i) = (Title[col]=columnhead(i), i) 

set datafile separator '\t'
plot for [col=colMin:colMax] datafile using (maketitle(col)):col w boxplot, \
     Title using 1:(title_y):(Title[$1]) with labels

enter image description here

1
theozh On

Here is another solution. For some reason (which I don't know), the most compact and intuitive command doesn't seem to work:

plot for [col=colMin:colMax] $Data u (col):col:xtic(columnhead(col)) w boxplot

You will get x-axis numbers instead of the desired columnheaders. Another compact, but working attempt is the following:

Script:

### boxplot with columnheader as xticlabels
reset session

$Data <<EOD
TB-Medium   LB-Medium   XX-Medium
42.9    42.9    42.9
38.1    38.1    38.1
33.3    33.3    33.3
37.1    37.1    37.1
34.8    34.8    34.8
36.6    36.6    36.6
37.4    37.4    37.4
35.1    35.1    35.1
39.8    39.8    39.8
45.8    45.8    45.8
57.8    57.8    57.8
42.7    42.7    42.7
EOD

colMin = 1
colMax = 3
set key noautotitle

plot for [col=colMin:colMax] $Data u (col):col w boxplot, \
     for [col=colMin:colMax] '' u (col):(NaN):xtic(columnhead(col))
### end of script

Result:

enter image description here