2

I'm struggling to get the labelling correct on a complex stacked and grouped bar chart. I want to label each group of bars with an ethnic group, and within each group of bars, I want to label the three individual bars 1st, 2nd and 3rd. Any suggestions on how to do this would be welcome.

\begin{figure}[h]
 \caption{Graph showing the percentage of births in different partnership 
scenarios to women, by generation and ethnic group, 1986-2014}
\centering
\begin{tikzpicture}[
  every axis/.style={
    ybar stacked,
    ymin=0,ymax=100,
    x tick label style={rotate=45,anchor=east},
  x axis line style = { opacity = 0 }, 
  y axis line style = { opacity = 0 },
  tickwidth = 0pt,
  width=17cm,
  height=8cm,
    symbolic x coords={
     African,
      African and White,
      African Caribbean,
      African Caribbean and White,
      Indian,
      Pakistani,
      Bangladeshi,
      Asian and White,
        },
  bar width=6pt
  },
]

\begin{axis}[bar shift=-12pt, xticklabels={}]
\addplot coordinates
{(African,64) (African and White,64) (African Caribbean,48) (African 
Caribbean and White,47) (Indian, 98) (Pakistani, 98) (Bangladeshi, 99) 
(Asian and White, 84)};
\addplot coordinates
{(African,12) (African and White,19) (African Caribbean,33) (African 
Caribbean and White,22) (Indian,1) (Pakistani, 1) (Bangladeshi, 0) (Asian 
and White, 11)};
\addplot coordinates
{(African,24) (African and White,17) (African Caribbean,33) (African 
Caribbean and White,31) (Indian, 1) (Pakistani, 1) (Bangladeshi, 1) (Asian 
and White, 5)};
\end{axis}

\begin{axis}
\addplot+[fill=blue!50!gray] coordinates
{(African,53) (African and White,28) (African Caribbean,38) (African 
Caribbean and White,34) (Indian, 92) (Pakistani, 93) (Bangladeshi, 91) 
(Asian and White, 69)};
\addplot+[fill=red!50!gray] coordinates
{(African,18) (African and White,19) (African Caribbean,19) (African 
Caribbean and White,28) (Indian,4) (Pakistani, 3) (Bangladeshi, 5) (Asian 
and White, 20)};
\addplot+[fill=yellow!50!gray] coordinates
{(African,30) (African and White,53) (African Caribbean,43) (African 
Caribbean and White,38) (Indian, 4) (Pakistani, 4) (Bangladeshi, 4) (Asian 
and White, 11)};
\end{axis}

\begin{axis}[bar shift=12pt,xticklabels={}]
\addplot+[fill=blue!50!gray] coordinates
{(African,42) (African and White,25) (African Caribbean,33) (African 
Caribbean and White,29) (Indian,88) (Pakistani, 85) (Asian and White, 50)};
\addplot+[fill=red!50!gray] coordinates
{(African,19) (African and White,27) (African Caribbean,20) (African 
Caribbean and White,27) (Indian, 5) (Pakistani, 6) (Asian and White, 26)};
\addplot+[fill=yellow!50!gray] coordinates
{(African,39) (African and White,48) (African Caribbean,47) (African 
Caribbean and White,44) (Indian, 7) (Pakistani, 9) (Asian and White, 25)};

\end{axis}
\end{tikzpicture}
\end{figure}

Graph

1

To get a single label per group, set xtick distance=1. For the labels below the bars, you could add a plot with zero-height bars, and use nodes near coords={some text} to add the label, along with every node near coord/.style={below} to move the label below the bar.

output of code

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[h]
 \caption{Graph showing the percentage of births in different partnership 
scenarios to women, by generation and ethnic group, 1986-2014}
\centering
\begin{tikzpicture}[
  every axis/.style={
    ybar stacked,
    ymin=0,ymax=100,
    x tick label style={
       % add some negative yshift to move ticklabels down
       yshift=-4mm,
       rotate=45,
       anchor=east},
  x axis line style = { opacity = 0 }, 
  y axis line style = { opacity = 0 },
  tickwidth = 0pt,
  width=15cm,
  height=8cm,
  % symbolic coords have numerical distance of 1
  % so with the following line you get a tick at every symbolic coord
  xtick distance=1,
  symbolic x coords={
     African,
      African and White,
      African Caribbean,
      African Caribbean and White,
      Indian,
      Pakistani,
      Bangladeshi,
      Asian and White,
        },
  bar width=6pt
  },
]

\pgfplotsset{
  % define a new style used for the plot used to add labels
  % 2 args means it takes two mandatory arguments, so must be used as
  % labelplot={first arg}{second arg}
  labelplot/.style 2 args={
     % forget plot means it doesn't affect cycle lists or legends
     forget plot,
     % #1 is first argument, the text used in the nodes near coords
     nodes near coords=#1,
     % #2 is second argument, a length that should be the same as the bar shift for the axis
     every node near coord/.style={below,font=\tiny,xshift=#2}
  }
}
\begin{axis}[bar shift=-12pt, xticklabels={}]
\addplot [labelplot={1st}{-12pt}] coordinates
{(African,0) (African and White,0) (African Caribbean,0) (African 
Caribbean and White,0) (Indian, 0) (Pakistani, 0) (Bangladeshi, 0) 
(Asian and White, 0)};

\addplot coordinates
{(African,64) (African and White,64) (African Caribbean,48) (African 
Caribbean and White,47) (Indian, 98) (Pakistani, 98) (Bangladeshi, 99) 
(Asian and White, 84)};
\addplot coordinates
{(African,12) (African and White,19) (African Caribbean,33) (African 
Caribbean and White,22) (Indian,1) (Pakistani, 1) (Bangladeshi, 0) (Asian 
and White, 11)};
\addplot coordinates
{(African,24) (African and White,17) (African Caribbean,33) (African 
Caribbean and White,31) (Indian, 1) (Pakistani, 1) (Bangladeshi, 1) (Asian 
and White, 5)};
\end{axis}

\begin{axis}
\addplot [labelplot={2nd}{0pt}] coordinates
{(African,0) (African and White,0) (African Caribbean,0) (African 
Caribbean and White,0) (Indian, 0) (Pakistani, 0) (Bangladeshi, 0) 
(Asian and White, 0)};

\addplot+[fill=blue!50!gray] coordinates
{(African,53) (African and White,28) (African Caribbean,38) (African 
Caribbean and White,34) (Indian, 92) (Pakistani, 93) (Bangladeshi, 91) 
(Asian and White, 69)};
\addplot+[fill=red!50!gray] coordinates
{(African,18) (African and White,19) (African Caribbean,19) (African 
Caribbean and White,28) (Indian,4) (Pakistani, 3) (Bangladeshi, 5) (Asian 
and White, 20)};
\addplot+[fill=yellow!50!gray] coordinates
{(African,30) (African and White,53) (African Caribbean,43) (African 
Caribbean and White,38) (Indian, 4) (Pakistani, 4) (Bangladeshi, 4) (Asian 
and White, 11)};
\end{axis}

\begin{axis}[bar shift=12pt,xticklabels={}]
\addplot [labelplot={3rd}{12pt}] coordinates
{(African,0) (African and White,0) (African Caribbean,0) (African 
Caribbean and White,0) (Indian, 0) (Pakistani, 0) (Bangladeshi, 0) 
(Asian and White, 0)};

\addplot+[fill=blue!50!gray] coordinates
{(African,42) (African and White,25) (African Caribbean,33) (African 
Caribbean and White,29) (Indian,88) (Pakistani, 85) (Asian and White, 50)};
\addplot+[fill=red!50!gray] coordinates
{(African,19) (African and White,27) (African Caribbean,20) (African 
Caribbean and White,27) (Indian, 5) (Pakistani, 6) (Asian and White, 26)};
\addplot+[fill=yellow!50!gray] coordinates
{(African,39) (African and White,48) (African Caribbean,47) (African 
Caribbean and White,44) (Indian, 7) (Pakistani, 9) (Asian and White, 25)};

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

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.