#
# Boxplot demo
#
reset

print "*** Boxplot demo ***"

set style fill solid 0.5 border -1
set style boxplot outliers pointtype 7
set style data boxplot
set boxwidth  0.5
set pointsize 0.5

unset key
set border 2
set xtics ("A" 1, "B" 2) scale 0.0
set xtics nomirror
set ytics nomirror
set yrange [0:100]

plot 'silver.dat' using (1):2, '' using (2):(5*$3)

pause -1 'Hit <cr> to continue: Compare sub-datasets'

# Comparing sub-datasets

set xtics auto
set yrange [*:*]
set title "Distribution of energy usage of the continents, grouped by type of energy source\n"
set ylabel "Billion Tons of Oil Equivalent"

plot 'energy_circles.dat' using (1):($8/1.e6):(0):4

pause -1 'Hit <cr> to continue: Assign selected colors to each factor'

set linetype 51 lc "dark-red"
set linetype 52 lc "dark-red"
set linetype 53 lc "dark-red"
set linetype 54 lc "midnight-blue"
set linetype 55 lc "sea-green"
set linetype 56 lc "sea-green"
set linetype 57 lc "sea-green"

set title "Distribution of energy usage of the continents, grouped by type of energy source,\n assign individual colors (linetypes) to the factors taken from column 4\n"

plot 'energy_circles.dat' using (1):($8/1.e6):(0):4 lt 51 lc variable

pause -1 'Hit <cr> to continue: Sort factors alphabetically'

# Sort factors alphabetically

set style boxplot sorted

set title "Distribution of energy usage of the continents, sorted by name of energy source\n"

plot 'energy_circles.dat' using (1):($8/1.e6):(0):4 lc rgb "gold"

pause -1 'Hit <cr> to continue: The same, with iteration and manual filtering'

# The same as above, with manual filtering
# Note that you have to specify the factors and you have to set the xtics as well.
# However, you have greater control over the appearance of the plot
# e.g. the order of the boxplots, their colors, the tic labels
# The previous form is intended for interactive usage while the latter form is better suited
# to creating publication-ready graphs.
# Note: we turn off autoscaling for the last two categories so that outliers do not
# expand the range on y even though they are not shown.

factors = "Nuclear Coal Gas Oil Hydroelectric Renewable"
NF = words(factors)
set xtic ("" 1)
set for [i=1:NF] xtics add (word(factors,i) i) 
set style boxplot nooutliers
set style boxplot medianlinewidth 2.5

t(x) = x/1.e6
filter(col, factor_col, level) = (strcol(factor_col) eq word(factors, level)) ? t(column(col)) : 1/0

set title "Distribution of energy usage explicitly ordered by name of energy source\n"

plot for [i=1:NF-2]  'energy_circles.dat' using (i):(filter(8, 4, i)), \
     for [i=NF-1:NF] 'energy_circles.dat' using (i):(filter(8, 4, i)) noauto

pause -1 'Hit <cr> to continue: boxplot demo finished'

reset
