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 have earlier asked two questions here related to barplots and labels/ticks for double y-axes: (1) How to make an xbar plot, with pgfplots, having different y tick labels on the left and right side y axis?, and (2) How to order barplots in aligned rows and columns?, and got good answers.

In my former question number (2) I needed to order barplots in 3 rows and 2 colums. In each row the leftmost barplot had outer yticks and ytick labeles on its left side, while the rightmost barplot had yticks and ytick labels on its right side. In addition, the ylabel for each barplot was turned horizontally and placed above the ytick labels.

My new question here is a combination of my previous two : How can I reduce the number of barplot columns, in question (2), from two to one, but retain all other structure, i.e. the single barplot in each row should have outer yticks, ytick labels and ylabel on both its left and right side?

Although the answer I got to my original two questions are very good ones, I am, unfortunalely, not able to combine them to answer my new question.

Below I have added the code I got from StackExchange as an answer to my previous question (2).

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

This code produces the following figure:

Barplots organized in rows and columns by the code-answer to my original question (2)

So posing my new question in a new way: how to delete the second column of barplots, that one below and including the "Meas. 2" heading, in the figure above and keeping the columns "Region", "Age interval", "Meas. 1" and "Winner", as well as equipping the barplots under "Meas. 1" with outer yticks also on its right side?


Thanks Jake. That answer helped me a lot! Here you have your bounty points :) Though...I have two follow-up questions. I am not sure if this is the right place, but I think these questions are very related to the example I use above:

1) How can I left/center/right justify the words in each of the colums "Region", "Age interval", and "Winner"?

2) If I want to add another column with additional information about each winner, say his or her body weigth, how can I add that? I guess in case that the y-tick options are used up since I have used them on both left and right side of the bar plots?

share|improve this question
    
What do you mean by the second column other than not drawing them? Also please consider accepting the answers to your previous questions. That would mark the question as solved and would indicate the solution is conclusive. –  percusse May 4 '12 at 13:36
    
@percusse: Does my included figure answer your queistion? My problem is that the answer to my former question (1) does not work in the context of my former question (2). I am not able to combine the two answers into one –  Espen Donali May 4 '12 at 13:42
    
Thank you for your edit. What I am trying to understand is the deletion part. Do you want to keep the Winner name list and Meas 2. title? That part is not clear for me. What if you don't draw the second column? What gets lost? Can you clarify that part if possible? –  percusse May 4 '12 at 13:45
    
Also every answer is specific to each question. So as far as I can tell they answer your concerns on your previous problems. This is a different problem with probably in need of a different answer. –  percusse May 4 '12 at 13:48
    
Ok, I see your point. I want to delete the column "Meas. 2", but retain the column "Winner". In addition, the columns of barplots below "Meas. 1" should get outer yticks on its right side, pointing towards the names of the winners in the "Winner" column. –  Espen Donali May 4 '12 at 14:01

1 Answer 1

up vote 3 down vote accepted
+100

You can use extra y ticks for this. Using every extra y tick/.style, you can pass all options to these additional y ticks that you can pass to "normal" ticks. The only annoyance with this approach is that you can't use extra y ticks=data, instead you'll have to manually specify the ticks (using, in this case, extra y ticks={1,...,4}).

\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};}
    },
    extra y ticks={1,...,4},
    every extra y tick/.style={
        yticklabel style={name=name label},
        ytick pos=right,
        yticklabel pos=right,
        ytick align = outside
    },
    winner title/.style={
        extra description/.append code={\node at (name label.west|-xlabel.base) [anchor=base west] {#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},
    winner title=Winner,
    yticklabel pos=left,
    yticklabels from table={data1.dat}{Age-interval},
    every extra y tick/.append style={
        yticklabels from table={data1.dat}{winner}
    },
    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}[
yticklabel pos=left,
yticklabels from table={data2.dat}{Age-interval},
every extra y tick/.append style={
    yticklabels from table={data2.dat}{winner}
},
bar width=4pt,
xticklabels = none,
region=South Carolina
]

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

\begin{axis}[
yticklabel pos=left,
yticklabels from table={data3.dat}{Age-interval},
every extra y tick/.append style={
    yticklabels from table={data3.dat}{winner}
},
bar width=4pt,
xticklabels = none,
region=Norway
]

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

\end{tikzpicture}
\caption{Results of some contest for men}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}
share|improve this answer
    
Hi Jake, thanks, this is promissing. But there seems to be an error in your code. The same set of men are winners in each and every region. In the block of code below the line "every extra y tick/.style" there is a line saying yticklabels from table={data1.dat}{winner}, resulting that all three regions get the same list of winners. As in my original example, the winners are unique for each region. So..how to add two different sets of y-ticklabels to each barplot? The left one being "Age-interval" and the right one being "Winner" –  Espen Donali May 7 '12 at 18:03
    
Well, you just need to add every extra y tick/.append style={yticklabels from table ...} to each of the plots, instead of putting it in the axis options. I've edited my answer. –  Jake May 7 '12 at 18:10

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.