[Tex/LaTex] Changing colour of stacked bar graph – pgfplot,tikz

pgfplotstikz-pgf

I'm trying to create a stacked bar chart with different shades of grey/black for the bars. My code is as follows. This does change the colour of the bars. However the outline of the bars remains in the standard red/blue scheme and this is also what appears in the legend. How do I change the colour scheme altogether?
Thanks, Polly.

\begin{figure}[h]
\caption{Graph showing the proportion of children born into each partnership 
scenario by ethnic group (Table 5, Kiernan and Smith, 2003)}
\centering
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
legend style={at={(0.5,-0.35)},
  anchor=north,legend columns=-1},
  x axis line style = { opacity = 0 }, 
  y axis line style = { opacity = 0 },
  tickwidth = 0pt,
bar width=10pt,
 ylabel={\% of births},
symbolic x coords={White British, Mixed Ethnicity, Indian, Pakistani, 
Bangladeshi, African Caribbean, Other Black, Other},
xtick=data,
x tick label style={rotate=45,anchor=east},
]
\addplot+[ybar] plot coordinates {(White British,14.4) (Mixed Ethnicity,38) 
(Indian,7.3)  (Pakistani,7) (Bangladeshi,9.3)(African Caribbean,51.6) (Other 
Black,39.4) (Other,14.3)} [fill=gray!90];
\addplot+[ybar] plot coordinates {(White British,27.1) (Mixed 
Ethnicity,21.4) 
(Indian,1.9)  (Pakistani,1.1) (Bangladeshi,2.9)(African Caribbean,16.3) 
(Other Black,14.2) (Other,9.8)} [fill=gray!50];
\addplot+[ybar] plot coordinates {(White British,58.5) (Mixed 
Ethnicity,40.6) 
(Indian,90.0)  (Pakistani,91.9) (Bangladeshi,87.8)(African Caribbean,32) 
(Other Black,46.5) (Other,75.9)}  [fill=gray!10];
\legend{Single, Cohabiting, Married}
\end{axis}
\end{tikzpicture}
\end{figure}

Best Answer

Don't use

\addplot +[<options>] plot coordinates {...} [<other options>];

What you should use is

\addplot +[<all options>] coordinates {...};

(Which tutorial teaches users to do \addplot plot anyways?)

To set the colour of the border, use draw=<colour> in addition to fill=<colour>. For example \addplot [fill=gray!90,draw=black!70] coordinates. Because you have ybar stacked in the axis options, you don't have to specify ybar for each plot.

output of code

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{figure}[h]
\caption{Graph showing the proportion of children born into each partnership 
scenario by ethnic group (Table 5, Kiernan and Smith, 2003)}
\centering
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
legend style={at={(0.5,-0.35)},
  anchor=north,legend columns=-1},
  x axis line style = { opacity = 0 }, 
  y axis line style = { opacity = 0 },
  tickwidth = 0pt,
bar width=10pt,
 ylabel={\% of births},
symbolic x coords={White British, Mixed Ethnicity, Indian, Pakistani, 
Bangladeshi, African Caribbean, Other Black, Other},
xtick=data,
x tick label style={rotate=45,anchor=east},
]
\addplot [fill=gray!90,draw=black!70]  coordinates {(White British,14.4) (Mixed Ethnicity,38) 
(Indian,7.3)  (Pakistani,7) (Bangladeshi,9.3)(African Caribbean,51.6) (Other 
Black,39.4) (Other,14.3)} ;
\addplot [fill=gray!50,draw=gray!80]  coordinates {(White British,27.1) (Mixed 
Ethnicity,21.4) 
(Indian,1.9)  (Pakistani,1.1) (Bangladeshi,2.9)(African Caribbean,16.3) 
(Other Black,14.2) (Other,9.8)};
\addplot [fill=gray!10,draw=gray!40]  coordinates {(White British,58.5) (Mixed 
Ethnicity,40.6) 
(Indian,90.0)  (Pakistani,91.9) (Bangladeshi,87.8)(African Caribbean,32) 
(Other Black,46.5) (Other,75.9)} ;
\legend{Single, Cohabiting, Married}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

Alternatively, define a custom cycle list in the axis options, and don't set any options for each \addplot. I.e. add

cycle list={
  {fill=gray!90,draw=black!70},
  {fill=gray!50,draw=gray!80},
  {fill=gray!10,draw=gray!40}
  }

to the axis options, and use just

\addplot coordinates {...};

Output as above.

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{figure}[h]
\caption{Graph showing the proportion of children born into each partnership 
scenario by ethnic group (Table 5, Kiernan and Smith, 2003)}
\centering
\begin{tikzpicture}
\begin{axis}[
  ybar stacked,
  legend style={at={(0.5,-0.35)},
  anchor=north,legend columns=-1},
  x axis line style = { opacity = 0 }, 
  y axis line style = { opacity = 0 },
  tickwidth = 0pt,
  bar width=10pt,
  ylabel={\% of births},
  symbolic x coords={
    White British,
    Mixed Ethnicity,
    Indian,
    Pakistani, 
    Bangladeshi,
    African Caribbean,
    Other Black,
    Other
  },
  xtick=data,
  x tick label style={rotate=45,anchor=east},
  cycle list={
    {fill=gray!90,draw=black!70},
    {fill=gray!50,draw=gray!80},
    {fill=gray!10,draw=gray!40}
    }
]
\addplot  coordinates {(White British,14.4) (Mixed Ethnicity,38) 
(Indian,7.3)  (Pakistani,7) (Bangladeshi,9.3)(African Caribbean,51.6) (Other 
Black,39.4) (Other,14.3)};

\addplot  coordinates {(White British,27.1) (Mixed 
Ethnicity,21.4) 
(Indian,1.9)  (Pakistani,1.1) (Bangladeshi,2.9)(African Caribbean,16.3) 
(Other Black,14.2) (Other,9.8)};

\addplot  coordinates {(White British,58.5) (Mixed 
Ethnicity,40.6) 
(Indian,90.0)  (Pakistani,91.9) (Bangladeshi,87.8)(African Caribbean,32) 
(Other Black,46.5) (Other,75.9)} ;

\legend{Single, Cohabiting, Married}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}