Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Please consider the following MWE.

There are three things I would like to do:

  1. Individual colouring For the last element ("not rejected") I would like to fill the bar with another color (e.g. orange)
  2. Nicer x-axis label The x-axis label "not rejected" ist too long for which reason I chose to rotate all x-axis labels via x tick label style = {rotate=90}. However, it does not look very nice. So maybe some has an idea. (What just came up my mind: I could replace "not rejected" with an "*" and include a note below the figure. No idea how that can be implemented.)
  3. Bar labels The current y-values represent the absolute occurrence of each element. However, I would like to add the relative occurrence of each element on top of its bar. The information is provided in the last column of the table EvalAbgelehntePP.dat.

bar chart

MWE

\documentclass{article}
\usepackage{filecontents}
\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable} 

\begin{filecontents}{EvalAbgelehntePP.dat}
Wert    PP  HaeufigkeitAbs  HaeufigkeitRel
1   4   682 61.49684400360685
2   5   630 56.80793507664562
3   7   457 41.2082957619477
4   9   414 37.33092876465284
5   1   403 36.339044183949504
6   8   394 35.527502254283135
7   6   360 32.46167718665464
8   3   268 24.16591523895401
9   2   254 22.90351668169522
10  10  207 18.66546438232642
11  {not rejected}  226 20.378719567177637
\end{filecontents}

\begin{document}

\begin{tikzpicture} 
\begin{axis}[ 
    ybar,
    xtick={1,...,11},
    xticklabels from table = {EvalAbgelehntePP.dat}{PP},
    x tick label style = {rotate=90},
    xtick align=inside,
    xlabel={production programms},
    every axis x label/.style={at={(ticklabel cs:0.5)},anchor=near ticklabel},
    ylabel=occurrence,
    every axis y label/.style={at={(ticklabel cs:0.5)},rotate=90,anchor=near ticklabel}
] 


\addplot[ybar,fill=blue] table [ 
    x=Wert, 
    y=HaeufigkeitAbs,
] {EvalAbgelehntePP.dat} ;

\end{axis} 
\end{tikzpicture} 

\end{document}
share|improve this question
    
Found the following in the pgf-manual but how do I tell LaTeX to get the nodes data from the last column of EvalAbgelehntePP.dat ? nodes near coords, nodes near coords align={vertical}, –  John Nov 1 '12 at 18:24

1 Answer 1

up vote 6 down vote accepted

You can adapt the approach from Is it possible to change the color of a single bar when the bar plot is based on symbolic values? for colouring a single bar in a different colour. I'm not sure what the best approach is for the single long label (what is PP?). What I did here is just rotate the long label, while leaving the others unrotated.

For adding the relative values, you have to tell PGFPlots that you're providing a meta value, by setting the key point meta=explicit, and then using meta=<column name> in the table options:

\documentclass{article}
\usepackage{filecontents}
\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable} 

\pgfplotsset{
    discard if/.style 2 args={
        x filter/.code={
            \ifdim\thisrow{#1} pt=#2pt
                \def\pgfmathresult{inf}
            \fi
        }
    },
    discard if not/.style 2 args={
        x filter/.code={
            \ifdim\thisrow{#1} pt=#2pt
            \else
                \def\pgfmathresult{inf}
            \fi
        }
    }
}

\begin{filecontents}{EvalAbgelehntePP.dat}
Wert    PP  HaeufigkeitAbs  HaeufigkeitRel
1   4   682 61.49684400360685
2   5   630 56.80793507664562
3   7   457 41.2082957619477
4   9   414 37.33092876465284
5   1   403 36.339044183949504
6   8   394 35.527502254283135
7   6   360 32.46167718665464
8   3   268 24.16591523895401
9   2   254 22.90351668169522
10  10  207 18.66546438232642
11  {\rotatebox{90}{not rejected}}  226 20.378719567177637
\end{filecontents}

\begin{document}

\begin{tikzpicture} 
\begin{axis}[ 
    ybar,
    xtick={1,...,11},
    xticklabels from table = {EvalAbgelehntePP.dat}{PP},
    xtick align=inside,
    xlabel={production programms},
    every axis x label/.style={at={(ticklabel cs:0.5)},anchor=near ticklabel},
    ylabel=occurrence,
    every axis y label/.style={at={(ticklabel cs:0.5)},rotate=90,anchor=near ticklabel}
] 


\addplot[ybar, bar shift=0pt, fill=blue,
    discard if={Wert}{11},
    nodes near coords=\pgfmathprintnumber{\pgfplotspointmeta}\%,
    every node near coord/.style={
        font=\scriptsize,
        /pgf/number format/precision=0
    },
    point meta=explicit] table [ 
    x=Wert, 
    y=HaeufigkeitAbs,
    meta=HaeufigkeitRel
] {EvalAbgelehntePP.dat} ;

\addplot[ybar, bar shift=0pt, fill=orange,
    discard if not={Wert}{11},
    nodes near coords=\pgfmathprintnumber{\pgfplotspointmeta}\%,
    every node near coord/.style={
        font=\scriptsize,
        /pgf/number format/precision=0
    },
    point meta=explicit] table [ 
    x=Wert, 
    y=HaeufigkeitAbs,
    meta=HaeufigkeitRel
] {EvalAbgelehntePP.dat} ;

\end{axis} 
\end{tikzpicture} 

\end{document}
share|improve this answer
    
I saw the colour-approach but did not know how to adapt it to my problem. Many thanks! –  John Nov 2 '12 at 1:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.