5

I'm trying to make a grouped stacked bar graph. I've got this far, with the code below. My problem is that there should be groups of three bars - but this code seems to have only produced groups of two. Also I need two sets of labels - One for ethnicity and one for generation, but not sure how to make this happen, especially given that the current labels dont work. Any help appreciated.

Many thanks, Polly

\begin{tikzpicture}

\begin{axis}[ybar stacked,bar shift=8pt,ymin=0,ymax=11,   x tick label 
style={rotate=45,anchor=east},symbolic x coords={White Other,African 
Caribbean and White,African,D,E}]]  
\addplot coordinates
{(White Other,1) (African Caribbean and White,1) (African,3) (D,2) (E,1.5)};
\addplot coordinates
{(White Other,1) (African Caribbean and White,1) (African,3) (D,2) (E,1.5)};
\addplot coordinates
{(White Other,1) (African Caribbean and White,1) (African,3) (D,2) (E,1.5)};
\end{axis}

\begin{axis}[ybar stacked,bar shift=-8pt,ymin=0,ymax=11,symbolic x coords=
{White Other,African Caribbean and White,African,D,E}]  
\addplot+[fill=blue!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5) 
(E,1)};
\addplot+[fill=red!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5) 
(E,1)};
\addplot+[fill=yellow!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5) 
(E,1)};
\end{axis}



\begin{axis}[ybar stacked,bar shift=-8pt,ymin=0,ymax=11,symbolic x coords=
{White Other,African Caribbean and White,African,D,E}]  
\addplot+[fill=blue!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5) 
(E,1)};
\addplot+[fill=red!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5) 
(E,1)};
\addplot+[fill=yellow!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5) 
(E,1)};

\end{axis}

\end{tikzpicture}

enter image description here

1 Answer 1

7

The problem is that you've used the same bar shift for two of the axis environments. You want e.g. -10pt for one axis, zero for the next, and 10pt for the last.

I would perhaps also add hide axis to the first two axis environments, so that the axis box, ticks and ticklabels are only drawn once.

output of code

\documentclass{article}    
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}[
  every axis/.style={ % add these settings to all the axis environments in the tikzpicture
    ybar stacked,
    ymin=0,ymax=11,
    x tick label style={rotate=45,anchor=east},
    symbolic x coords={
      White Other,
      African Caribbean and White,
      African,D,E
    },
  bar width=8pt
  },
]

% bar shift -10pt here
\begin{axis}[bar shift=-10pt,hide axis]
\addplot coordinates
{(White Other,1) (African Caribbean and White,1) (African,3) (D,2) (E,1.5)};
\addplot coordinates
{(White Other,1) (African Caribbean and White,1) (African,3) (D,2) (E,1.5)};
\addplot coordinates
{(White Other,1) (African Caribbean and White,1) (African,3) (D,2) (E,1.5)};
\end{axis}

% zero bar shift here    
\begin{axis}[hide axis]
\addplot+[fill=blue!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5) 
(E,1)};
\addplot+[fill=red!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5) 
(E,1)};
\addplot+[fill=yellow!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5) 
(E,1)};
\end{axis}

% and bar shift +10pt here
\begin{axis}[bar shift=10pt]
\addplot+[fill=blue!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5) 
(E,1)};
\addplot+[fill=red!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5) 
(E,1)};
\addplot+[fill=yellow!50!gray] coordinates
{(White Other,1.5) (African Caribbean and White,1.5) (African,3.5) (D,2.5) 
(E,1)};

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

You must log in to answer this question.

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