On Tue, 5 Dec 2017, Artur Tarassow wrote:
I want to plot a stacked barplot over time by providing the matrix
'mat' (rows=Time, cols=variables) and a vector 'xmat'. xmat is based
on the $obsdate command holding daily dates in the format YYYYMMDD.
My objective is to handle date strings on the x-axis correctly. But
this drives me nuts...
Here's a somewhat improved version of my suggestion. As it stands it's
adapted to quarterly data. It would be straightforward to generalize
it to cover monthly data, daily would require a little more work.
In this version I've simplified things a little by assuming that
Artur's "xmat" (time variable) is not an optional extra but is the
last column of the matrix argument to stackplot().
<hansl>
set verbose off
function void stackplot (matrix mat,
int nstep[1::4] "Print only n-th value on xaxis",
string fname)
k = cols(mat)
tmpfile = sprintf("%s/stackedbar.gp", $dotdir)
outfile @tmpfile --write
printf "set encoding utf8\n"
printf "set nokey\n"
printf "set style data histogram \n"
printf "set style histogram rowstacked\n"
printf "set style fill solid border -1\n"
printf "set style fill solid 0.35\n"
printf "set xtics nomirror rotate by -45\n"
# write data block
printf "$data << EOF\n"
loop i=1..rows(mat) -q
loop j=1..k-1 -q
printf "%g ", mat[i,j]
endloop
cond = (i == 1 || (i+1) % nstep == 0)
printf "%g\n", cond ? mat[i,k] : -1
endloop
printf "EOF\n"
# define a gnuplot function
printf "# start literal lines\n"
printf "qdate(x)=sprintf(\"%%gQ%%g\",floor(x),10*(x-floor(x)))\n"
printf "# end literal lines\n"
# write plot specification
printf "plot \\\n"
loop j=1..k-1 -q
if j == 1
printf "$data using 1:xtic($%d < 0. ? \"\" : qdate($%d))",
k, k
else
printf "$data using %d", j
endif
printf "%s\n", j == k-1 ? "" : ",\"
endloop
outfile --close
gnuplot --input="@tmpfile" --output="@fname"
end function
open data9-7 -q
smpl 1975:1 1980:4
# quarterly data
series date = $obsmajor + $obsminor/10
stackplot({QNC}~{QNC/2}~{date}, 2, "display")
</hansl>
Allin