1

I have made this stacked bar chart but I can't get my x-axis label to be shown even though I have added them in the codes. Also, can anyone tell me how can I add labels to my stacked bar chart like in the picture ? Thanks!

enter image description here

\documentclass[10pt,twoside,a4paper,fleqn]{report}
\usepackage{xcolor,pgfplots}
\begin{document}
\begin{figure}[ht!]
\centering
\begin{tikzpicture}
\begin{axis}[
    xbar stacked,
    legend style={
    legend columns=4,
        at={(xticklabel cs:0.5)},
        anchor=north,
        draw=none
    },
    xlabel= Percentage,
    ylabel= Environmental indicators,
    ytick=data,
    xtick=data,
    axis y line*=none,
    axis x line*=bottom,
    tick label style={font=\footnotesize},
    legend style={font=\footnotesize},
    label style={font=\footnotesize},
    xtick={0,10,20,30,40,50,60,70,80,90,100},
    width=.9\textwidth,
    bar width=6mm,
    yticklabels={GWP, ODP, POCP, AP, EP(T), EP(FW), EP(M), ADP, CED},
    xmin=0,
    xmax=100,
    area legend,
    y=8mm,
    enlarge y limits={abs=0.625},
]
\addplot
[Construction,fill=Construction] 
coordinates
{(7.9,0) (8.8,1) (20.2,2) (28.7,3) (20.5,4) (44.7,5) (17.8,6) (46.6,7) (3.9,8)};
\addplot
[Operational,fill=Operational] 
coordinates
{(89.7,0) (89.5,1) (76.3,2) (64.5,3) (76.3,4) (50.1,5) (70.5,6) (37.9,7) (95.4,8)};
\addplot
[Maintenance,fill=Maintenance] 
coordinates
{(1.0,0) (1.5,1) (2.8,2) (6.4,3) (2.9,4) (4.8,5) (3.1,6)(15.3,7) (0.6,8)};
\addplot
[Disposal,fill=Disposal] 
coordinates
{(1.3,0) (0.2,1) (0.7,2) (0.3,3) (0.3,4) (0.5,5) (8.6,6)(0.1,7) (0.1,8)};

\legend{Construction, Operational, Replacement, Disposal}
\end{axis}  
\end{tikzpicture}
\caption{Life cycle phase contribution analysis per impact category.\\Categories from bottom to the top are Global Warming Potential (\ac{GWP}), Ozone Depletion Potential (\ac{ODP}), Photochemical Ozone Creation Potential (\ac{POCP}), Acidification Potential (\ac{AP}), Terrestrial Eutrophication Potential \ac{EP(T)}, \ac{EP(FW)}, \ac{ADP} and \ac{CED}.}
\label{conventional}
\end{figure}
\end{document}
  • Thanks for providing a MWE. But, please edit your code so that it produces the erroneous result that you describe. As it is, it is not compilable. For instance you have \[ where it should just be a [ for optional parameters. – Peter Grill Nov 20 '18 at 20:51
  • Thanks for the reply. I hope it's correct now. – thesilencer Nov 20 '18 at 21:49
  • Did you try compiling it? – Peter Grill Nov 20 '18 at 22:21
  • I'm not quite sure what's the mistake here because I extracted the table from my thesis and it's not exactly the whole thing. Did i miss any package that you can kindly enlighten me on? – thesilencer Nov 20 '18 at 23:26
  • I get errors when I compile this.Please put this in a separate document and make sure it compiles. For instance, the Construction style is not defined, neither is the Construction color. – Peter Grill Nov 20 '18 at 23:34
1

It is somewhat hard to write an answer because this requires some fair amount of avoidable extra work. I ended up defining the missing colors somehow, and inventing a command \ac. Now to your questions:

  1. You cannot see the xlabel since you put the legend on top. I moved the legend down.
  2. You can add numbers with percentages using nodes near coords. I made a guess which numbers you might be after, and of course this guess is most likely wrong, but perhaps will allow you to modify things in such a way that you get what you want.

Result

\documentclass[10pt,twoside,a4paper,fleqn]{report}
\usepackage{xcolor,pgfplots}
\pgfplotsset{compat=1.16}
\definecolor{Construction}{RGB}{184,36,33}
\definecolor{Operational}{RGB}{233,171,100}
\definecolor{Maintenance}{RGB}{124,174,255}
\definecolor{Disposal}{RGB}{185,220,165}
\newcommand{\ac}[1]{\textcolor{red}{#1}}
\begin{document}
\begin{figure}[ht!]
\centering
\begin{tikzpicture}
\begin{axis}[clip=true,
    xbar stacked,
    legend style={
    legend columns=4,
        at={(xticklabel cs:0.5)},yshift=-5mm,
        anchor=north,
        draw=none
    },
    ytick=data,
    xtick=data,
    axis y line*=none,
    axis x line*=bottom,
    xlabel= Percentage,
    ylabel= Environmental indicators,
    tick label style={font=\footnotesize},
    legend style={font=\footnotesize},
    label style={font=\footnotesize},
    xtick={0,10,20,30,40,50,60,70,80,90,100},
    width=.9\textwidth,
    bar width=6mm,
    yticklabels={GWP, ODP, POCP, AP, EP(T), EP(FW), EP(M), ADP, CED},
    xmin=0,
    xmax=100,
    area legend,
    y=8mm,
    enlarge y limits={abs=0.625},
    visualization depends on=x \as \rawx,
    nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
    every node near coord/.style={xshift=\rawx*1.5pt+0.5cm,text=white},
]
\addplot
[Construction,fill=Construction] 
coordinates
{(7.9,0) (8.8,1) (20.2,2) (28.7,3) (20.5,4) (44.7,5) (17.8,6) (46.6,7) (3.9,8)};
\addplot
[Operational,fill=Operational,nodes near coords={}] 
coordinates
{(89.7,0) (89.5,1) (76.3,2) (64.5,3) (76.3,4) (50.1,5) (70.5,6) (37.9,7) (95.4,8)};
\addplot
[Maintenance,fill=Maintenance,nodes near coords={}] 
coordinates
{(1.0,0) (1.5,1) (2.8,2) (6.4,3) (2.9,4) (4.8,5) (3.1,6)(15.3,7) (0.6,8)};
\addplot
[Disposal,fill=Disposal,nodes near coords={}] 
coordinates
{(1.3,0) (0.2,1) (0.7,2) (0.3,3) (0.3,4) (0.5,5) (8.6,6)(0.1,7) (0.1,8)};
\legend{Construction, Operational, Replacement, Disposal}
\end{axis}  
\end{tikzpicture}
\caption{Life cycle phase contribution analysis per impact category.
Categories
from bottom to the top are Global Warming Potential (\ac{GWP}), Ozone Depletion
Potential (\ac{ODP}), Photochemical Ozone Creation Potential (\ac{POCP}),
Acidification Potential (\ac{AP}), Terrestrial Eutrophication Potential
\ac{EP(T)}, \ac{EP(FW)}, \ac{ADP} and \ac{CED}.}
\label{conventional}
\end{figure}
\end{document}

enter image description here

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.