[Tex/LaTex] Horizontal bargraph pgfplot

bar chartpgfplotstikz-pgf

I'd like to make two graphs using TiKz pgfplots or otherwise using this table below. But get the error

! Package pgfplots Error: Sorry, the input coordinate `Bloodstream
Infection (B SI' has not been defined with 'symbolic y
coords={Bloodstream Infection (BSI),S urgical site infections
(SSI),Ventilator-associated pneumonia (VAP),Urinary tra ct infection
(UTI),Others}… Maybe it has been misspelled?.

Have I not correctly defined the names in y coords?

**Type of infection Mean cost   Stdev   St dev Min  Stdev Max   %**
Bloodstream Infection (BSI) 36441   37078   2883    207256  14
Surgical site infections (SSI)  25546   39875   1783    134602  17
Ventilator-associated pneumonia (VAP)   9969    2920    7904    12034   14
Urinary tract infection (UTI)   1006    503 650 1361    33
Others  1000    1000    800 5000    22

Type of infection vs Cost
Type of infection vs %

 \documentclass{article}
\usepackage{tikz} 
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

 \begin{tikzpicture}
\begin{axis}[ 
xbar, xmin=0,
xlabel={Percentage \%},
symbolic y coords={{Bloodstream Infection (BSI)},{Surgical site infections (SSI)},{Ventilator-associated pneumonia (VAP)},{Urinary tract infection (UTI)},{Others}},
ytick=data,
nodes near coords, nodes near coords align={horizontal},
ytick=data,
]
\addplot coordinates {(14,{Bloodstream Infection (BSI)}) (17,{Surgical site infections (SSI)}) (14,{Ventilator-associated pneumonia (VAP)}) (33,{Urinary tract infection (UTI)})(22, {Others})};
\end{axis}
\end{tikzpicture} %width=6cm,height=7.59cm

\end{document}

Best Answer

When symbolic coordinates are used, the pattern given by symbolic <x or y> coords should match exactly the pattern given in the coordinates. Hence the whitespace before {Others} should be removed.

Moreover, if there are characters such as parentheses or commas in the symbolic coordinate itself that would cause confusion during coordinate parsing of (<some text with ( or comma> , <some text with ( or comma>) they should be put withing brace pairs such that the parser will take them as a group of chars instead of finding a comma and start parsing the next coordinate.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}
\begin{tikzpicture}
\begin{axis}[ 
xbar, xmin=0,
xlabel={Percentage \%},
symbolic y coords={%
    {Bloodstream Infection (BSI)},
    {Surgical site infections (SSI)},
    {Ventilator-associated pneumonia (VAP)},
    {Urinary tract infection (UTI)},
    Others},
ytick=data,
nodes near coords, 
nodes near coords align={horizontal},
ytick=data,
]
\addplot coordinates {
    (14,{Bloodstream Infection (BSI)}) 
    (17,{Surgical site infections (SSI)}) 
    (14,{Ventilator-associated pneumonia (VAP)}) 
    (33,{Urinary tract infection (UTI)})
    (22,Others)};
\end{axis}
\end{tikzpicture} %width=6cm,height=7.59cm
\end{document}

enter image description here

Related Question