I am making xbar plots with pgfplots. Within different age groups ("Agen interval") of men a single winner is choosen (Winner), based on some test, and on this winner two different variables are measured ("Meas. 1" and "Meas. 2"). This double measurement on winners is done in several different states/contries. For each contry the two results are shown as two barplots on the same row, see the attached code.
My questions are:
(1) How to put every contry name in a specific vertical position along the y axis next to it? For instance, I want the contry name to appear in a vertical position halfway between the lower and the upper x axis in the barplot to the right of it.
(2) How to make the columns of barplots to be aligned vertically, irrespective of the length of the contry names?
(3) How can I place the y labels ("Age interval" and "Winer") on exactly the same base line (vertically aligned) as the titles "Meas. 1" and "Meas. 2" ? now I have adjusted it manually by means of the code line
every axis y label/.style = {at={(yticklabel cs:0.94)}, rotate=0,anchor =center},
so I would like to exchange "(yticklabel cs:0.94)" with some coordinate variable for the title.
(4) How can I pull the y label, and the y tick labels below it somewhat away from the y axis? I.e. the column starting with "Age interval" and the column starting with "Meas. 1" should have more horizontal space between them.
(5) I would like to make a title called "Contry" above the column with contry names, and this title should also be vertically aligned with the titles "Meas. 1" and "Meas. 2"
(6) I want to put all the rows and colums described above inside a "figure" environment in order to add a caption. How do I prevent this figure environment to de-align the colums of contry names and barplots?
(7) How can I make sure that the horizontal space between rows of barplots is identical to the vertical space between columns of barplots?
My code is:
% !TeX program = pdflatex
% !TeX encoding = ISO-8859-1
\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\pgfplotsset{
compat=newest,
width=4cm,
height=4cm,
title style = {yshift = 10pt},
xtick align = inside,
xtick pos = both,
xticklabel pos = upper,
xmin=-5,
xmax = 140,
ytick align = outside,
ytick pos = left,
yticklabel pos=left,
ymin =0,
ymax = 5,
ytick=data,
xbar,
nodes near coords,
every node near coord/.append style = {anchor=west},
}
% Measurements contry 1
\begin{filecontents}{data1.dat}
Age-interval Y-Position meas1 meas2 winner
20-30 1 15 45 John
30-40 2 20 13 Al
40-50 3 12 4 Andrew
50-60 4 24 1 Tom
\end{filecontents}
% Measurements contry 2
\begin{filecontents}{data2.dat}
Age-interval Y-Position meas1 meas2 winner
20-30 1 44 30 Peter
30-40 2 15 33 Steve
40-50 3 2 48 David
50-60 4 66 98 Alister
\end{filecontents}
% Measurements contry 3
\begin{filecontents}{data3.dat}
Age-interval Y-Position meas1 meas2 winner
20-30 1 13 22 Arne
30-40 2 1 48 Per
40-50 3 2 4 Ola
50-60 4 33 61 Anders
\end{filecontents}
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Ohio
\begin{tikzpicture}
\begin{axis}[
title = {Meas. 1},
yticklabel pos=left,
yticklabels from table={data1.dat}{Age-interval},
ylabel = {Age interval},
every axis y label/.style = {at={(yticklabel cs:1.3)}, rotate=0,anchor =center},
bar width=4pt,
]
\addplot table [
y=Y-Position,
x=meas1,
] {data1.dat};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}[
title = {Meas. 2},
yticklabel pos=right,
yticklabels from table={data1.dat}{winner},
ylabel = {Winner},
every axis y label/.style = {at={(yticklabel cs:0.94)}, rotate=0,anchor =center},
ytick pos = right,
bar width=4pt,
]
\addplot table [
y=Y-Position,
x=meas2,
] {data1.dat};
\end{axis}
\end{tikzpicture}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
South Carolina
\begin{tikzpicture}
\begin{axis}[
yticklabel pos=left,
yticklabels from table={data2.dat}{Age-interval},
bar width=4pt,
xticklabels = none,
]
\addplot table [
y=Y-Position,
x=meas1,
] {data2.dat};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}[
yticklabel pos=right,
yticklabels from table={data2.dat}{winner},
ytick pos = right,
bar width=4pt,
xticklabels = none,
]
\addplot table [
y=Y-Position,
x=meas2,
] {data2.dat};
\end{axis}
\end{tikzpicture}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Norway
\begin{tikzpicture}
\begin{axis}[
yticklabel pos=left,
yticklabels from table={data3.dat}{Age-interval},
bar width=4pt,
xticklabels = none,
]
\addplot table [
y=Y-Position,
x=meas1,
] {data3.dat};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}[
yticklabel pos=right,
yticklabels from table={data3.dat}{winner},
ytick pos = right,
bar width=4pt,
xticklabels = none,
]
\addplot table [
y=Y-Position,
x=meas2,
] {data3.dat};
\end{axis}
\end{tikzpicture}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}
