4

I would like to create something like the pictured chart below:

Divergent Stacked Bar

I have this now which gives me a good stacked bar chart but I'd like to align the green areas on the right side.

MWE:

\documentclass{standalone}

\usepackage[tikz]{ocgx2}
\usepackage{pgfplotstable}
\pgfplotsset{width=12.2cm, height=7cm}

\begin{document}

\pgfplotstableread{
Place       Excellent Ok     Bad    Horrible
Croatia     70        29     1      0
Lithuania   58        37     5      0
Estonia     50        47     3      0
Latvia      12        78     10     0
Albania     5         40     48     7
}\testdata

\begin{tikzpicture}
\begin{axis}[
            legend cell align=left,
            legend columns=5,
            legend style={at={(0.5,-0.15)},anchor=north,draw=none},
            xbar stacked,
            xmin=0,
            xmax=100,
            xticklabel=\pgfmathprintnumber{\tick}\,$\%$,
            ytick=data,
            yticklabels from table={\testdata}{Place}
            ]
\addlegendimage{empty legend}
\addlegendentry{ppm levels:}
\addplot [fill=green] table [x=Excellent, meta=Place ,y expr=\coordindex] {\testdata};
\addlegendentry{(0)-1000}
\addplot [fill=yellow] table [x=Ok, meta=Place ,y expr=\coordindex] {\testdata};
\addlegendentry{(1000)-2500}
\addplot [fill=orange!90] table [x=Bad, meta=Place ,y expr=\coordindex] {\testdata};
\addlegendentry{(2500)-5000}
\addplot [fill=red] table [x=Horrible, meta=Place ,y expr=\coordindex] {\testdata};
\addlegendentry{(5000)-..}
\end{axis}
\end{tikzpicture}

\end{document}

Output:

Stacked Bar Chart

3

Not sure if this is what you're after, but have a look. I used two axis environments, with the green plots in the first, and the red ones in the second.

output of code

\documentclass{standalone}

\usepackage[tikz]{ocgx2}
\usepackage{pgfplotstable}
\pgfplotsset{width=12.2cm, height=7cm}

\begin{document}

\pgfplotstableread{
Place       Excellent Ok     Bad    Horrible
Croatia     70        29     1      0
Lithuania   58        37     5      0
Estonia     50        47     3      0
Latvia      12        78     10     0
Albania     5         40     48     7
}\testdata

\begin{tikzpicture}
\begin{axis}[
            name=ax1,
            legend cell align=left,
            legend columns=-1,
            legend style={at={(1,-0.15)},anchor=north,draw=none},
            xbar stacked,
            xmin=-100,
            xmax=0,
            xticklabel=\pgfmathparse{abs(\tick)}\pgfmathprintnumber{\pgfmathresult}\,$\%$,
            ytick=data,
            yticklabels from table={\testdata}{Place}
            ]
\addlegendimage{empty legend}
\addlegendentry{ppm levels:}
\addplot [fill=green] table [x expr=-\thisrow{Excellent}, meta=Place ,y expr=\coordindex] {\testdata};
\addlegendentry{(0)-1000}
\addplot [fill=yellow] table [x expr=-\thisrow{Ok}, meta=Place ,y expr=\coordindex] {\testdata};
\addlegendentry{(1000)-2500}

\addlegendimage{fill=orange!90}
\addlegendimage{fill=red}
\addlegendentry{(2500)-5000}
\addlegendentry{(5000)-..}
\end{axis}

\begin{axis}[
            at=(ax1.south east),
            xbar stacked,
            xmin=0,
            xmax=100,
            xticklabel=\pgfmathprintnumber{\tick}\,$\%$,
            ytick=data,
            yticklabels={}
            ]
\addplot [fill=orange!90] table [x=Bad, meta=Place ,y expr=\coordindex] {\testdata};

\addplot [fill=red] table [x=Horrible, meta=Place ,y expr=\coordindex] {\testdata};

\end{axis}
\end{tikzpicture}

\end{document}
0

Thanks to Torbjorn T. I got what I wanted. I changed the following to make it work as intended:

'\pgfplotsset{width=6.1cm, height=7cm}' and moved the yellow field to the right of the alignment.

\documentclass{standalone}

\usepackage[tikz]{ocgx2}
\usepackage{pgfplotstable}
\pgfplotsset{width=6.1cm, height=7cm}

\begin{document}

\pgfplotstableread{
Place       Excellent Ok     Bad    Horrible
Croatia     70        29     1      0
Lithuania   58        37     5      0
Estonia     50        47     3      0
Latvia      12        78     10     0
Albania     5         40     48     7
}\testdata

\begin{tikzpicture}
\begin{axis}[
            name=ax1,
            legend cell align=left,
            legend columns=-1,
            legend style={at={(1,-0.15)},anchor=north,draw=none},
            xbar stacked,
            xmin=-100,
            xmax=0,
            xticklabel=\pgfmathparse{abs(\tick)}\pgfmathprintnumber{\pgfmathresult}\,$\%$,
            ytick=data,
            yticklabels from table={\testdata}{Place}
            ]
\addlegendimage{empty legend}
\addlegendentry{ppm levels:}
\addplot [fill=green] table [x expr=-\thisrow{Excellent}, meta=Place ,y expr=\coordindex] {\testdata};
\addlegendentry{(0)-1000}
\addlegendimage{fill=yellow}
\addlegendimage{fill=orange!90}
\addlegendimage{fill=red}
\addlegendentry{(1000)-2500}
\addlegendentry{(2500)-5000}
\addlegendentry{(5000)-..}
\end{axis}

\begin{axis}[
            at=(ax1.south east),
            xbar stacked,
            xmin=0,
            xmax=100,
            xticklabel=\pgfmathprintnumber{\tick}\,$\%$,
            ytick=data,
            yticklabels={}
            ]

\addplot [fill=yellow] table [x=Ok, meta=Place ,y expr=\coordindex] {\testdata};

\addplot [fill=orange!90] table [x=Bad, meta=Place ,y expr=\coordindex] {\testdata};

\addplot [fill=red] table [x=Horrible, meta=Place ,y expr=\coordindex] {\testdata};

\end{axis}
\end{tikzpicture}

\end{document}

Output: Divergent stacked bar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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