Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
add comment

1 Answer

  1. You can use extra description/.append code={\node at (yticklabel cs:0.5) [anchor=west] {<Region name>};} to place a node halfway up the axis to the left of the ticklabels. However, in this case, I'd go for a different approach, since you're aligning several things to each other.
  2. You can put all your axis environments in the same tikzpicture and use a \matrix{<first axis> & <second axis> \\ <third axis> & <fourth axis>\\}; to arrange your plots in a grid. This will automatically align the actual axis areas, ignoring the tick labels and labels.
  3. If you use the xlabel option instead of the title option for typesetting the x axis label, you can name that node using xlabel style={name=xlabel}, and name the y tick labels using yticklabel style={name=yticklabel}. Then you can align the y axis label using ylabel style={at=(yticklabel.east|-xlabel.base), anchor=base east}. This will place the y axis label at the horizontal position of the y tick label and at the vertical position of the base of the x axis label.
  4. You can use the key yticklabel shift=<length> for this.
  5. This can be done similarly to the way we aligned the y axis label. I would suggest you align the region name using extra description/.append code={\node (region label) at ({yticklabel cs:0.5}-|ylabel.west) [anchor=east,xshift=-1ex] {<region name>};}. That way, it will be placed 1ex to the left of the y axis label, regardless of how wide that label is. After that, you can align the region title using extra description/.append code={\node at (region label.east|-xlabel.base) [anchor=base east] {<region title>};}.
  6. \begin{figure}\begin{tikzpicture}\matrix{ <axis> & <axis> ...};\end{tikzpicture}\caption{<caption>}\end{figure}
  7. \matrix[column sep=<length>, row sep=<length>]{<axes>};

\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}

\pgfplotsset{
    compat=newest,
    width=4cm,
    height=4cm,
    title style = {yshift = 10pt,name=title},
    xtick align = inside,
    xtick pos = both,
    xticklabel pos = upper,
    yticklabel style={name=yticklabel},
    xlabel style={name=xlabel},
    ylabel style={rotate=-90},
    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},
    yticklabel shift=0.25cm,
    region/.style={
        extra description/.append code={\node (region label) at ({yticklabel cs:0.5}-|ylabel.west) [anchor=east,xshift=-1ex] {#1};}
    },
    region title/.style={
        extra description/.append code={\node at (region label.east|-xlabel.base) [anchor=base east] {#1};}
    }
}

% 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}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\begin{figure}
\begin{tikzpicture}
\matrix[column sep=4pt, row sep=2pt]{
\begin{axis}[
    xlabel = {Meas. 1},
    yticklabel pos=left,
    yticklabels from table={data1.dat}{Age-interval},
    ylabel = {Age interval},
    ylabel style = {
        at=(yticklabel.east|-xlabel.base),
        anchor=base east,
        name=ylabel
    },
    bar width=4pt,
    region=Ohio,
    region title=Region
]

\addplot table [
y=Y-Position,
x=meas1,
] {data1.dat};
\end{axis}
&
\begin{axis}[
xlabel = {Meas. 2},
yticklabel pos=right,
yticklabels from table={data1.dat}{winner},
ylabel = {Winner},
    ylabel style = {
        at={(yticklabel.west|-xlabel.base)},
        anchor=base west,
    },
ytick pos = right,
bar width=4pt,
]

\addplot table [
y=Y-Position,
x=meas2,
] {data1.dat};
\end{axis}
\\
\begin{axis}[
yticklabel pos=left,
yticklabels from table={data2.dat}{Age-interval},
bar width=4pt,
xticklabels = none,
region=South Carolina
]

\addplot table [
y=Y-Position,
x=meas1,
] {data2.dat};
\end{axis}
&
\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}

\\

\begin{axis}[
yticklabel pos=left,
yticklabels from table={data3.dat}{Age-interval},
bar width=4pt,
xticklabels = none,
region=Norway
]

\addplot table [
y=Y-Position,
x=meas1,
] {data3.dat};
\end{axis}
&

\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}
\caption{Results of some contest for men}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.