# how to create an histogram with a string label using pgfplots?

Dear i try to generate an histogram / barplot

result.csv:

overlap,hit,organism
100,274,Unknowns
100,228,Coccomyxa subellipsoidea
100,11,Eukaryota
-500,153,Unknowns
-500,145,Coccomyxa subellipsoidea
-500,7,Eukaryota
-1000,118,Unknowns
-1000,108,Coccomyxa subellipsoidea
-1000,4,cellular organisms
-2000,74,Coccomyxa subellipsoide
-2000,72,Unknowns
-2000,4,Viridiplantae
-4000,51,Unknowns
-4000,42,Coccomyxa subellipsoidea
-4000,2,Eukaryota
-8000,26,Coccomyxa subellipsoidea
-8000,25,Unknowns
-8000,1,Eukaryota
-16000,14,Coccomyxa subellipsoidea
-16000,13,Unknowns
-16000,1,cellular organisms


As you can see we can group data in 7 category using overlap :

100, -500, -1000, -2000, -4000, -8000, -16000

\documentclass[table]{beamer}

\usepackage[american]{babel}
\usepackage{etex}
\usepackage{pgfplots}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{textcomp}
\usepackage{tikz}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\pgfplotsset{
table/col sep = comma
}

\begin{document}

\begin{frame}
\begin{tikzpicture}
\centering
\begin{axis}[
visualization depends on={value \thisrow{organism} \as \labela},
visualization depends on={value \thisrow{hit} \as \labelb},
ybar,
height                = 6cm,
width                 = 10cm,
enlarge y limits      = false,
axis lines*           = left,
ymin                  = 0,
ymax                  = 300,
xmin                  = -17000,
xmax                  = 600,
legend style          = {
at              = {(0.5,-0.2)},
anchor          = north,
legend columns  = -1
},
xtick                 = data,
scaled y ticks        = false,
y tick label style    = {/pgf/number format/fixed,/pgf/number format/1000 sep = \thinspace},
xticklabels from table= \table{overlap},
nodes near coords={\labela/\pgfmathprintnumber\labelb},
every node near coord/.append style={ anchor=mid west, rotate=80 }
]
\foreach \i in {1,2,...,21}{
\addplot table[ x index=\i, y index=1 ] \table;
}
\end{axis}
\end{tikzpicture}
\end{frame}

\end{document}


But i fail to plot correctly, current code give an error:

! Package PGF Math Error: Could not parse input 'Unknowns' as a floating point
number, sorry. The unreadable part was near 'Unknowns'.


-
This happens because you're trying to use the value of the counter \i to choose which column to use for the x values. I'm not exactly sure what you're trying to achieve: Do you want to add one plot for each row of your data? That won't work as intended, because you'll either get huge gaps between your bars, or they will all lie on top of each other (depending on where you put the ybar keyword). Could you explain in a bit more detail what kind of output you're going for (maybe draw a mockup?), and could you remove all the bits of code that aren't relevant for the problem? –  Jake Jan 23 '13 at 14:34
in first thanks for your explantaion i understand that \i is column number and not the line number. Yes i want to plot ybar and give a color for each overlap category. –  bioinfornatics Jan 23 '13 at 15:23

To use a string as the label for a bar plot, you can use point meta=explicit symbolic and specify which column to use as the label for each plot by setting meta=<column name> in the table options of the plots.

In this case, I would reorganise the data table to group the organisms and hit counts by the overlap:

\documentclass{article}

\usepackage{pgfplots, xstring}

\usepackage{filecontents}

\begin{filecontents}{dataedited.csv}
overlap,hit1,organism1,hit2,organism2,hit3,organism3
100,274,Unknowns,228,Coccomyxa subellipsoidea,11,Eukaryota
-500,153,Unknowns,145,Coccomyxa subellipsoidea,7,Eukaryota
-1000,118,Unknowns,108,Coccomyxa subellipsoidea,4,cellular organisms
-2000,74,Coccomyxa subellipsoide,72,Unknowns,4,Viridiplantae
-4000,51,Unknowns,42,Coccomyxa subellipsoidea,2,Eukaryota
-8000,26,Coccomyxa subellipsoidea,25,Unknowns,1,Eukaryota
-16000,14,Coccomyxa subellipsoidea,13,Unknowns,1,cellular organisms
\end{filecontents}

\begin{document}
\begin{tikzpicture}
\centering
\begin{axis}[
xbar, bar width=7pt,
height                = 11cm,
width                 = 10cm,
enlarge x limits      = false,
axis lines*           = left,
legend style          = {
at              = {(0.5,-0.2)},
anchor          = north,
legend columns  = -1
},
xmin = 0,
ytick                 = data,
scaled x ticks        = false,
yticklabels from table= \table{overlap},
point meta=explicit symbolic,
nodes near coords={
\IfStrEq{\pgfplotspointmeta}{Coccomyxa subellipsoidea}{\textcolor{red}{\pgfplotspointmeta}}{\pgfplotspointmeta}
},
every node near coord/.append style={anchor=mid west, yshift=-0.5ex, black!75}
]
\addplot [fill=gray] table [col sep=comma, y expr=-\coordindex, x=hit3, meta=organism3] {dataedited.csv};
\addplot [fill=gray!75] table [col sep=comma, y expr=-\coordindex, x=hit2, meta=organism2] {dataedited.csv};
\addplot [fill=gray!50] table [col sep=comma, y expr=-\coordindex, x=hit1, meta=organism1] {dataedited.csv};
\end{axis}
\end{tikzpicture}
\end{document}

-
thanks a lot you save me :-) –  bioinfornatics Jan 23 '13 at 16:11