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 want to create a barplot with three groups. Compared are two criteria: water consumption and CO2 emission. To make it more comprehensive here the plot I already generated

enter image description here

I approached this problem by using the example from Stacked barplots and text labels in x-coordinate. You can see that the waterconsumption has very small values in comparisson to the ones of the co2-emission. So the Question is:

How can I create a second y-axis for the values of the water consumption?

MWE:

\documentclass{article}
\usepackage{pgfplots,pgfplotstable}
\usetikzlibrary{pgfplots.groupplots}
\pgfplotsset{compat=1.4}

\pgfplotstableread{
Criterion   Wasserverbrauch   {CO$_2$-Emission}
Komp        8           2349
Komp+PV 8           452
Sorp        14          1006
}\Rapperswil

\pgfplotstableread{
Criterion   Wasserverbrauch   {CO$_2$-Emission}
Komp        36          10220
Komp+PV 36          5891
Sorp        85          3160
}\Palermo

\pgfplotstableread{
Criterion   Wasserverbrauch   {CO$_2$-Emission}
Komp        97          25657
Komp+PV 97          18306
Sorp        184         7461
}\Jeddah

\begin{document}

\begin{tikzpicture}
  \begin{groupplot}
    [group style= {
    columns=3,xlabels at=edge bottom,
    group name=plots,   y descriptions at=edge left,
    },
    ybar,
    ymin=0,
    ymax=27e3,
    enlarge x limits={abs=.5},
    width=0.45\textwidth,
    height=0.6\textwidth,
    scaled y ticks=base 10:-3,
    xticklabels from table={\Rapperswil}{Criterion},
    x tick label style={rotate=90,anchor=east},
    xtick=data,legend columns=2]

    \nextgroupplot[xlabel=Rapperswil,legend to name=grouplegend,ylabel={[kg CO$_2$ per year]}]
    \pgfplotstableforeachcolumn\Rapperswil\as\col{%
        \ifnum\pgfplotstablecol=0
        \else
        \edef\tmp{%
            \noexpand\addplot table [x expr=\noexpand\coordindex,y=\col] {\noexpand\Rapperswil};
            \noexpand\addlegendentry {\col}%
        }%
        \tmp
        \fi
    }

    \nextgroupplot[xlabel=Palermo]
    \pgfplotstableforeachcolumn\Palermo\as\col{%
        \ifnum\pgfplotstablecol=0 
        \else
        \edef\tmp{%
            \noexpand\addplot table [x expr=\noexpand\coordindex,y=\col] {\noexpand\Palermo};
        }%
        \tmp
        \fi
    }

    \nextgroupplot[xlabel=Jeddah]
    \pgfplotstableforeachcolumn\Jeddah\as\col{%
        \ifnum\pgfplotstablecol=0 
        \else
        \edef\tmp{%
            \noexpand\addplot table [x expr=\noexpand\coordindex,y=\col] {\noexpand\Jeddah};
        }%
        \tmp
        \fi
    }
  \end{groupplot}

  \node at (plots c2r1.north) [anchor=south, yshift=.6cm] {\ref{grouplegend}};
\end{tikzpicture}

\end{document}
share|improve this question
    
My answer in that question should still suffice. You just need to scour the manual for how to make the plots look like you want. For instance how not to display the axis on the right hand side, how to add a second y-axis etc. How to fix the y-min/max. –  zeroth Dec 20 '11 at 13:30
    
This is very similar to the question Different scales on the same plot. See if you can adapt that approach (which is taken directly from the manual, like zeroth suggested) to your situation. If you run into trouble, feel free to edit your question to make it more specific. –  Jake Dec 20 '11 at 22:31
    
Sorry but I just don't get it. for the axis environment it is quite easy but how does it work in the groupplot? –  Lorzen Dec 21 '11 at 13:14
    
Admittedy a non-answer (hence the comment), but why not use GNUPlot? There's a great reference-ish page here ;) –  mpr Dec 21 '11 at 16:02
    
@Lorzen: When there's more than one other person commenting on a question, you have to include @<name> to notify a user of your comment. –  Jake Dec 22 '11 at 7:52
show 1 more comment

1 Answer

up vote 8 down vote accepted

The approach for combining different y-axes when using groupplots is very similar to that when using just one plot: You draw the second set of plots on top of the first one, hiding the axis lines and axis labels, and moving the y(tick)labels to the other side:

\documentclass{article}
\usepackage{pgfplots,pgfplotstable}
\usetikzlibrary{pgfplots.groupplots}
\pgfplotsset{compat=1.4}

\pgfplotstableread{
Criterion   Wasserverbrauch   {CO$_2$-Emission}
Komp        8           2349
Komp+PV 8           452
Sorp        14          1006
}\Rapperswil

\pgfplotstableread{
Criterion   Wasserverbrauch   {CO$_2$-Emission}
Komp        36          10220
Komp+PV 36          5891
Sorp        85          3160
}\Palermo

\pgfplotstableread{
Criterion   Wasserverbrauch   {CO$_2$-Emission}
Komp        97          25657
Komp+PV 97          18306
Sorp        184         7461
}\Jeddah

\begin{document}

\begin{tikzpicture}
\pgfplotsset{
    water/.style={
        fill=cyan,
        draw=cyan!50!black
    },
    co2/.style={
        fill=orange,
        draw=orange!50!black
    }
}

  \begin{groupplot}[
    group style={
        columns=3,
        group name=plots,
        x descriptions at=edge bottom,
        y descriptions at=edge left,
    },
    ylabel={[ML]}, ylabel style=cyan!50!black,
    yticklabel style=cyan!50!black,
    ybar, /pgf/bar shift=-6pt, % We have to manually move the bars
    ymin=0, ymax=270,
    enlarge x limits={abs=.5},
    width=0.4\textwidth,
    height=0.6\textwidth,
    xticklabels from table={\Rapperswil}{Criterion},
    x tick label style={rotate=90,anchor=east},
    xtick=data, xtick pos=left,
    legend columns=2]

    \nextgroupplot[xlabel=Rapperswil,legend to name=grouplegend]
    \addplot [water] table [x expr=\coordindex,y index=1] {\Rapperswil};
        \addlegendentry {Wasserverbrauch}%
    \addlegendimage{co2,ybar legend}
        \addlegendentry{CO$_2$-Emission}

    \nextgroupplot[xlabel=Palermo]
    \addplot [water] table [x expr=\coordindex,y index=1] {\Palermo}; 

    \nextgroupplot[xlabel=Jeddah]
     \addplot [water] table [x expr=\coordindex,y index=1] {\Jeddah};
  \end{groupplot}

  \begin{groupplot}
    [group style= {
        columns=3,
        group name=plots,
        y descriptions at=edge right,
    },
    ybar, /pgf/bar shift=6pt,
    ymin=0, ymax=2.7e4,
    enlarge x limits={abs=.5},
    width=0.4\textwidth,
    height=0.6\textwidth,
    xtick=\empty, axis line style=transparent,
    ylabel={[kg CO$_2$ per year]},yticklabel style=orange!75!black,
    ylabel style=orange!75!black]

    \nextgroupplot[scaled y ticks=false]
    \addplot [co2] table [x expr=\coordindex,y index=2] {\Rapperswil};

    \nextgroupplot[scaled y ticks=false]
     \addplot [co2] table [x expr=\coordindex,y index=2,
        ] {\Palermo}; 

    \nextgroupplot
     \addplot [co2] table [x expr=\coordindex,y index=2] {\Jeddah};
  \end{groupplot}

  \node at (plots c2r1.north) [anchor=south, yshift=.6cm] {\ref{grouplegend}};
\end{tikzpicture}

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