2

The first column, which corresponds to the leftmost bar in each group is shifted heavily to the left. Why?

Example picture

(The tikzpicture has to be in a beamer environment.)

   \documentclass{beamer}
    \usepackage{siunitx, pgfplotstable,booktabs, pgfplots, filecontents}
    \beamertemplatenavigationsymbolsempty
    \pgfplotsset{compat=1.15}
    \begin{filecontents*}{data.csv}
    1,11,35,1,1
    2,9,5,1,1
    3,0,3,5,5
    \end{filecontents*}

\sisetup{range-units=single}
\begin{frame}{Doses and Reported Effects}
\noindent
\begin{tikzpicture}
    \begin{axis}[ybar=3.2em,
            ymin=0,
            ymax=40,
            bar width=1em,
            ylabel={No. of comments},
            xtick={1,2,3},
            width=8cm,
            axis y line* = left,
            axis x line* = bottom,
            ylabel near ticks,
            table/x index={0},
            xticklabels = {
                \strut \SIrange{0}{250}{\mg},
                \strut \SIrange{300}{500}{\mg},
                \strut $>$500
            },
            enlarge x limits =0.25,
            %xticklabel style={yshift=-10ex},
            minor x tick num = 1,
            minor tick length=1em,
            every node near coord/.append style={anchor=north,yshift=1.2em},
            legend entries={sweating,feel hot, diaphoresis, no side effects},
            legend columns=1,
            legend style={draw=none,nodes={inner sep=3pt},at={(1.2,0.8)}}]
    \addplot[draw=black,fill=red!20, nodes near coords] table[y index=1, col sep =comma]{data.csv}; 
    \addplot[draw=black,fill=red!40, nodes near coords] table[y index=4,col sep =comma]{data.csv}; 
    \addplot[draw=black,fill=red!60, nodes near coords] table[y index=3,col sep =comma]{data.csv}; 
    \addplot[draw=black,fill=white, nodes near coords] table[y index=2,col sep =comma]{data.csv}; 
    \end{axis}
\end{tikzpicture}
\end{frame}

1 Answer 1

3

The main issue disappeared after I removed =3.2em after ybar. The length after ybar is the bar shift, which, when turned on, unsurprisingly shifts bars.

\documentclass{beamer}
\usepackage{siunitx, pgfplotstable,booktabs, pgfplots, filecontents}
\beamertemplatenavigationsymbolsempty
\pgfplotsset{compat=1.15}
\begin{filecontents*}{data.csv}
1,11,35,1,1
2,9,5,1,1
3,0,3,5,5
\end{filecontents*}

\begin{document}
\sisetup{range-units=single}
\begin{frame}{Doses and Reported Effects}
\noindent
\begin{tikzpicture}
    \begin{axis}[ybar,
            ymin=0,
            ymax=40,
            bar width=1em,
            ylabel={No.\ of comments},
            xtick={1,2,3},
            width=8cm,
            axis y line* = left,
            axis x line* = bottom,
            ylabel near ticks,
            table/x index={0},%xtick={1,2,3},
            xticklabels = {
                {\strut \SIrange{0}{250}{\mg}},
                {\strut \SIrange{300}{500}{\mg}},
                {\strut $>$500}
            },
            enlarge x limits =0.25,
            %xticklabel style={yshift=-10ex},
            minor x tick num = 1,
            minor tick length=1em,
            every node near coord/.append style={anchor=north,yshift=1.2em},
            legend entries={sweating,feel hot, diaphoresis, no side effects},
            legend columns=1,
            legend style={draw=none,nodes={inner sep=3pt},at={(1.2,0.8)}}]
    \addplot[draw=black,fill=red!20, nodes near coords] table[x
    expr=\coordindex+1,y index=1,col sep =comma,header=false]{data.csv}; 
    \addplot[draw=black,fill=red!40, nodes near coords] table[y index=4,col sep =comma,header=false]{data.csv}; 
    \addplot[draw=black,fill=red!60, nodes near coords] table[y index=3,col sep =comma,header=false]{data.csv}; 
    \addplot[draw=black,fill=white, nodes near coords] table[y index=2,col sep =comma,header=false]{data.csv}; 
    \end{axis}
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

There were also curious effects from siunitx that went away after I put the \SIRange commands into groups. (You also forgot to add \begin{document} and \end{document}. This is not too important but represents an unnecessary threshold for other users to look at the question.)

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.