Search code examples
gnuplotboxplot

How to plot grouped boxplot by gnuplot


I wonder how to use gnuplot to plot this figure: enter image description here

There are two problems I have:

  • the ytic is ..., 10^2, 10^1, 10^2, 10^3, ... How to handle such a case?
  • I know gnuplot support boxplot, but how to regroup boxplot according to some label?

Since I don't have the original data for the figure, I make up some data by myself.

There are two companies A, B, and C, selling different fruits with four prices.

Apple prices of company A: 1.2 1.3 1.4 1.1

Banana prices of company A: 2.2 2.1 2.4 2.5

Orange prices of company A: 3.1 3.3 3.4 3.5

Apple prices of company B: 1.2 1.3 1.4 1.1

Banana prices of company B: 2.2 2.1 2.4 2.5

Orange prices of company B: 3.1 3.3 3.4 3.5

Apple prices of company C: 2.2 1.3 1.4 2.1

Banana prices of company C: 3.2 3.1 3.4 2.5

Orange prices of company C: 2.1 3.3 1.4 2.5

I wonder how to plot those numbers by gnuplot.


Solution

  • Your question is not very detailed and your own coding attempt is missing, hence, there is a lot of uncertainty. I guess there is no simple single command to get your grouped boxplots. There are for sure several ways to realize your graph, e.g. with multiplot.

    The assumption for the example below is that all files have the data organized in columns and equal number of columns and same fruits in the same order. Otherwise the code must be adapted. It all depends on the degree of "automation" you would like to have. Vertical separation lines can be drawn via headless arrows (check help arrow). So, see the following example as a starting point.

    Data:

    'Company A.dat'

    Apples   Bananas   Oranges
    1.2      2.2       3.1
    1.3      2.1       3.3
    1.4      2.4       3.4
    1.1      2.5       3.5
    

    'Company B.dat'

    Apples   Bananas   Oranges
    1.2      2.2       3.1
    1.3      2.1       3.3
    1.4      2.4       3.4
    1.1      2.5       3.5
    

    'Company C.dat'

    Apples   Bananas   Oranges
    2.2      3.2       2.1
    1.3      3.1       3.3
    1.4      3.4       1.4
    2.1      2.5       2.5
    

    Code:

    ### grouped boxplots
    reset session
    
    FILES     = 'A B C'
    File(n)   = sprintf("Company %s.dat",word(FILES,n))
    myXtic(n) = sprintf("Company %s",word(FILES,n))
    
    set xlabel "Fruit prices"
    set ylabel "Price"
    set yrange [0:5]
    set grid y
    
    set key noautotitle
    set style fill solid 0.3
    
    N    = words(FILES)    # number of files
    COLS = 3               # number of columns in file
    PosX = 0               # x-position of boxplot
    
    plot for [n=1:N] for [COL=1:COLS] PosX=PosX+1 File(n) u (PosX):COL w boxplot lc COL, \
         for [COL=1:COLS] File(1) u (NaN):COL w boxes lc COL ti columnhead, \
         for [n=1:N] File(1) u ((n-1)*COLS+COLS/2+1):(NaN):xtic(myXtic(n))
    ### end of code
    

    Result:

    enter image description here