9

I can't find an answer online, please help me. The following plot shows the percentage of 3 groups. The red line is the average (and needs to stay at 13.8).

How can I move ONLY the node "2.9" a little bit up? (here it is not really problematic, but I have other graphs where the number and the red line are overlapping - looks terrible!)....

Thank you very very much!

enter image description here

\documentclass[pdftex,11pt,a4paper]{scrreprt}
\usepackage{geometry}
\geometry{a4paper,left=28mm,right=28mm, top=25mm, bottom=25mm} 
\usepackage{acronym}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,arrows}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{amsmath,amssymb,amstext}
\usepackage{graphicx}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}


\begin{document}
\pgfplotsset{every axis/.append style={
    font=\footnotesize,
    line width=0.5pt,
    bar width=7pt,
    tick style={line width=0.4pt}}}

\begin{figure}[h!]
\begin{tikzpicture}
\begin{axis}[
    xlabel=Test,
    ymin=0, ymax=100,
    ylabel=percentage,
    enlargelimits=0.15,
    ybar,
    symbolic x coords={1,2,3},
    xtick=data,
    width=12cm,
    bar width=10pt];

\addplot [black, fill=black,nodes near coords]
coordinates {(1, 55.6) (2,18) (3,2.9)};

\addplot[red,mark=none, sharp plot, line width=0.6pt]
coordinates {(1,13.8) (3,13.8)};

\end{axis}

\end{tikzpicture}
\end{figure}
\end{document}
3
  • 1
    Please post a true MWE... there are certainly some packages not needed for this problem. And try to avoid an empty line between each line of code.
    – user31729
    Commented Jul 9, 2014 at 17:56
  • 1
    @Jake: Ok, sorry, I was distracted ;-) I remove my previous comment
    – user31729
    Commented Jul 9, 2014 at 18:23
  • @Kate, people will be more likely to answer if a true MWE is posted. As Christian notes, you have many packages/options that only serve to complicate the issue. Here's a potential MWE (linebreaks lost in comment, but you get the idea): \documentclass{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.10} \begin{document} \begin{tikzpicture} \begin{axis}[ybar,xtick=data]; \addplot[nodes near coords] coordinates {(1, 10) (2, 5)}; \end{axis} \end{tikzpicture} \end{document} With this MWE, you could ask a more general question: How to adjust the position of the label node on a single bar? Commented Jul 13, 2014 at 12:23

3 Answers 3

6
+25

You can move a single node near coord by using a conditional yshift that checks the current coordinate index, which is stored in the macro \coordindex:

\documentclass{article}
\usepackage{pgfplots}


\begin{document}
\pgfplotsset{every axis/.append style={
    font=\footnotesize,
    line width=0.5pt,
    bar width=7pt,
    tick style={line width=0.4pt}}}

\begin{tikzpicture}
\begin{axis}[
    xlabel=Test,
    ymin=0, ymax=100,
    ylabel=percentage,
    enlargelimits=0.15,
    ybar,
    symbolic x coords={1,2,3},
    xtick=data,
    width=12cm,
    bar width=10pt];

\addplot [black, fill=black,
    nodes near coords,
    every node near coord/.append style={
        yshift={\ifnum\coordindex=1 1ex\else 0pt\fi}
    }
]
coordinates {(1, 55.6) (2,11) (3,2.9)};

\addplot[red,mark=none, sharp plot, line width=0.6pt]
coordinates {(1,13.8) (3,13.8)};

\end{axis}

\end{tikzpicture}
\end{document} 
4

If I understood your question properly then your problem is that the average plot sometimes overlaps the values on top of the bars. I have came up with a solution, but you will have to omit the nodes near coords option from your bar plot, and manually add the nodes after drawing the average plot.

The solution I'm posting has different values than yours to demonstrate the two options I have included. See the two middle bars, first I have added 1.5 ex height to the node, the second I have added a faded background so the crossing red line won't be so disturbing. (Note that if you use the faded background solution you'll have to include the fadings TikZ library.)

Also I have lengthened the average plot line to extend to the edge of the bars.

\documentclass[pdftex,11pt,a4paper]{scrreprt}

\usepackage{geometry}
\geometry{a4paper, left=28mm, right=28mm, top=25mm, bottom=25mm}

\usepackage{acronym}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows, fadings}
% fading from: http://tex.stackexchange.com/q/82004/8844
\tikzfading[name=fade out,
    inner color=transparent!0,
    outer color=transparent!100]

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}

\usepackage{amsmath,amssymb,amstext}

\usepackage{graphicx}

\usepackage{pgfplots}
\pgfplotsset{compat=1.9}

\begin{document}
\pgfplotsset{every axis/.append style={%
    font=\footnotesize,%
    line width=0.5pt,%
    bar width=7pt,%
    tick style={line width=0.4pt}}}

\begin{figure}[h!]
\begin{tikzpicture}
    \begin{axis}[%
        xlabel=Test,%
        ymin=0,%
        ymax=100,%
        ylabel=percentage,%
        enlarge x limits=0.15,%
        enlarge y limits={value=0.05,upper},%
        ybar,%
        symbolic x coords={1,2,3,4},%
        xtick=data,%
        width=12cm,%
        bar width=10pt];%

        \addplot [black,%
            fill=black] coordinates {%
                (1, 75) (2, 50) (3, 50) (4, 35)
            };

        \addplot[red,%
            mark=none,%
            sharp plot,%
            line width=0.6pt] coordinates {
                (1, 52.5) (4, 52.5)
            };
        % omit the following two lines if you dont want the average plot
        % lines extended to the edge of the bars
        \draw[red, line width=0.6pt] (axis cs:1, 52.5) -- ++(-0.5 * \pgfplotbarwidth, 0);
        \draw[red, line width=0.6pt] (axis cs:4, 52.5) -- ++(0.5 * \pgfplotbarwidth, 0);

        % add values to the bars manually
        \node[above] at (axis cs:1, 75) {75};
        \node[above=1.5ex] at (axis cs:2, 50) {50};
        \node[above,
            fill=white,%
            path fading=fade out,%
            text opacity=1.0,%
            inner xsep=2em] at (axis cs:3, 50) {50};
        \node[above] at (axis cs:4, 35) {35};
    \end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
3

This is a solution -- simply draw the the 3rd ybar independently and add node near coords key with raisebox command into the option.

Note: To avoid shift of xticks, ybar key should move to individual addplot.

nodes near coords=\raisebox{0.3cm}{\pgfmathprintnumber\pgfplotspointmeta}

enter image description here

Code

\documentclass[pdftex,11pt,a4paper]{scrreprt}
\usepackage{geometry}
\geometry{a4paper,left=28mm,right=28mm, top=25mm, bottom=25mm} 
\usepackage{acronym}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,arrows}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{amsmath,amssymb,amstext}
\usepackage{graphicx}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}


\begin{document}
\pgfplotsset{every axis/.append style={
    font=\footnotesize,
    line width=0.5pt,
    bar width=7pt,
    tick style={line width=0.4pt}}}

\begin{figure}[h!]
\begin{tikzpicture}
\begin{axis}[
    xlabel=Test,
    ymin=0, ymax=100,
    ylabel=percentage,
    enlargelimits=0.15,
    %ybar,
    symbolic x coords={1,2,3},
    xtick=data, 
    extra x ticks={3},
    width=12cm,
    bar width=10pt];

\addplot [black, ybar, fill=black,nodes near coords]  % <--- remove (3,2.9)
coordinates {(1,55.6) (2,18)};

\addplot [black, ybar, fill=black,nodes near coords=\raisebox{0.3cm}{\pgfmathprintnumber\pgfplotspointmeta}]      
coordinates {(3,2.9)};                          % <--- to here and then raise the label where 0.3cm can be changed.

\addplot[red,mark=none, sharp plot, line width=0.6pt]
coordinates {(1,13.8) (3,13.8)};

\end{axis}

\end{tikzpicture}
\end{figure}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .