0

I'm trying to draw a bar diagram but it seems that the bars are reversed.

Let's take "Canada" for example, I would like to have 48 as first value, 24 and then 59, not the other way around. I can't figure out how to reverse the bars.

Resulting wrong image and code below. enter image description here

\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat = newest}

\begin{document}

\pgfplotstableread[header = true, col sep = comma]{
State,      Science,  Engineering,  Bachelor
Canada,     48,       24,           59
France,     38,       31,           55
Germany,    44,       22,           54
Italy,      55,       39,           62
Spain,      44,       32,           57
Sweden,     44,       30,           62
UK,         38,       23,           56
US,         43,       22,           57
OECD,       42,       27,           58
}{\datatable}

\begin{tikzpicture}[scale=1]
\begin{axis}[
    ,width = 6in, height = 12cm
    ,xbar
    ,xmajorgrids
    ,tickwidth = 0pt
    ,x axis line style = { opacity = 0 }
    ,xlabel = {\textbf{Percent (\%) Female Graduates}}
    ,ylabel = {\textbf{States}}
    ,xbar = 2pt% space of 2pt between adjacent bars
    ,bar width = 7pt
    ,xmin = 0, xmax = 70
    ,ytick = data% crucial line for the yticklabels directive
    ,yticklabels from table = {\datatable}{State}
    ,nodes near coords
    ,every node near coord/.append style={font=\ttfamily\footnotesize}
    ,y tick label style = {
        ,rotate = 0
        ,anchor = east
    }
    ,legend style = {
        ,draw = none
        ,anchor = north
        ,at = {(0.5,-0.125)}
        ,legend columns = 3
        ,legend style={
            /tikz/every even column/.append style={column sep=.5cm},
            /tikz/every odd column/.append style={column sep=.1cm}
        }
        ,align = left
    }
    ,cycle list name = exotic
    ,every axis plot/.append style = {fill, draw = none, no markers}%
]

    \foreach \i in {Science, Engineering, Bachelor} {
        \addplot+ table [
            y expr=-\coordindex,% Use negative coordinate index as y coordinate
            x=\i,
        ]{\datatable};
    }
    
    \legend{Sciences, Engineering, All bachelor programs}

\end{axis}
\end{tikzpicture}
\end{document}

1 Answer 1

0

Not sure why but reversing the order of \foreach \i in {Science, Engineering, Bachelor} to:

\foreach \i in {Bachelor,Engineering,Science}

works like so:

enter image description here

I am sure someone might have the technical answer but this is a workaround that orders as you state in your question.

\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat = newest}

\begin{document}

\pgfplotstableread[header = true, col sep = comma]{
State,      Science,  Engineering,  Bachelor
Canada,     48,       24,           59
France,     38,       31,           55
Germany,    44,       22,           54
Italy,      55,       39,           62
Spain,      44,       32,           57
Sweden,     44,       30,           62
UK,         38,       23,           56
US,         43,       22,           57
OECD,       42,       27,           58
}{\datatable}

\begin{tikzpicture}[scale=1]
\begin{axis}[
    ,width = 6in, height = 12cm
    ,xbar
    ,xmajorgrids
    ,tickwidth = 0pt
    ,x axis line style = { opacity = 0 }
    ,xlabel = {\textbf{Percent (\%) Female Graduates}}
    ,ylabel = {\textbf{States}}
    ,xbar = 2pt% space of 2pt between adjacent bars
    ,bar width = 7pt
    ,xmin = 0, xmax = 70
    ,ytick = data% crucial line for the yticklabels directive
    ,yticklabels from table = {\datatable}{State}
    ,nodes near coords
    ,every node near coord/.append style={font=\ttfamily\footnotesize}
    ,y tick label style = {
        ,rotate = 0
        ,anchor = east
    }
    ,legend style = {
        ,draw = none
        ,anchor = north
        ,at = {(0.5,-0.125)}
        ,legend columns = 3
        ,legend style={
            /tikz/every even column/.append style={column sep=.5cm},
            /tikz/every odd column/.append style={column sep=.1cm}
        }
        ,align = left
    }
    ,cycle list name = exotic
    ,every axis plot/.append style = {fill, draw = none, no markers}%
]

    \foreach \i in {Bachelor,Engineering,Science} {
        \addplot+ table [
            y expr=-\coordindex,% Use negative coordinate index as y coordinate
            x=\i,
        ]{\datatable};
    }
    
\legend{All bachelor programs, Engineering, Science}

\end{axis}
\end{tikzpicture}
\end{document}

If you want the legends to be in order use the option reverse legend:

enter image description here

\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat = newest}

\begin{document}

\pgfplotstableread[header = true, col sep = comma]{
State,      Science,  Engineering,  Bachelor
Canada,     48,       24,           59
France,     38,       31,           55
Germany,    44,       22,           54
Italy,      55,       39,           62
Spain,      44,       32,           57
Sweden,     44,       30,           62
UK,         38,       23,           56
US,         43,       22,           57
OECD,       42,       27,           58
}{\datatable}

\begin{tikzpicture}[scale=1]
\begin{axis}[
    ,reverse legend
    ,width = 6in, height = 12cm
    ,xbar
    ,xmajorgrids
    ,tickwidth = 0pt
    ,x axis line style = { opacity = 0 }
    ,xlabel = {\textbf{Percent (\%) Female Graduates}}
    ,ylabel = {\textbf{States}}
    ,xbar = 2pt% space of 2pt between adjacent bars
    ,bar width = 7pt
    ,xmin = 0, xmax = 70
    ,ytick = data% crucial line for the yticklabels directive
    ,yticklabels from table = {\datatable}{State}
    ,nodes near coords
    ,every node near coord/.append style={font=\ttfamily\footnotesize}
    ,y tick label style = {
        ,rotate = 0
        ,anchor = east
    }
    ,legend style = {
        ,draw = none
        ,anchor = north
        ,at = {(0.5,-0.125)}
        ,legend columns = 3
        ,legend style={
            /tikz/every even column/.append style={column sep=.5cm},
            /tikz/every odd column/.append style={column sep=.1cm}
        }
        ,align = left
    }
    ,cycle list name = exotic
    ,every axis plot/.append style = {fill, draw = none, no markers}%
]

    \foreach \i in {Bachelor,Engineering,Science} {
        \addplot+ table [
            y expr=-\coordindex,% Use negative coordinate index as y coordinate
            x=\i,
        ]{\datatable};
    }
    
    \legend{All bachelor programs, Engineering, Science}

\end{axis}
\end{tikzpicture}
\end{document}
5
  • The problem is that now the colors of the legend are not correct Feb 11 at 16:05
  • 1
    @EmanueleNardi It has been fixed now
    – JamesT
    Feb 11 at 16:10
  • I used the same solution as a temporary solution, by changing the order of the items in the legend and using the reverse legend keyword Feb 11 at 16:13
  • I was wondering if there was a cleaner solution Feb 11 at 16:14
  • 1
    @EmanueleNardi I do not know TikZ axis very well, I just tried to solve the issue in your question (and solved it), you will need to ask a separate question or wait for someone more experienced in TikZ to answer. I am genuinely confused as to why it went in the wrong order though
    – JamesT
    Feb 11 at 16:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .