1

I would like to shift text of nodes out of axis area like in the first image. If this is difficult to realize, would be nice also the solution in the second image, in which the right margin of area is shifted in order to let enough space for the text of the node.

enter image description here

enter image description here

The code I used is this:

\begin{filecontents*}{WHCs.dat}
Sample  y   y_err
5-Pa-Pr 0.520   0.10
6-Pa-Pr 0.465   0.05
7-Pa-Pr 0.768   0.16
5-Pa-S  0.517   0.10
6-Pa-S  0.562   0.20
7-Pa-S  0.794   0.35
\end{filecontents*} 

    \begin{tikzpicture}
    \begin{axis}[
        ybar, ymin=0,
        bar width=15pt,
        x tick label style={rotate=45, anchor=east},
        major x tick style = transparent,
        ymajorgrids = true,
        xtick = data,
        axis on top,
        enlarge x limits=0.35,
        scaled y ticks = false,
        symbolic x coords={5-Pa-Pr, 6-Pa-Pr, 7-Pa-Pr, 5-Pa-S, 6-Pa-S, 7-Pa-S},
        ymajorgrids]
   \begin{scope}
    \fill[brown] (rel axis cs:0,0.355) rectangle (rel axis cs:1,0.365);
    \fill[green] (rel axis cs:0,0.495) rectangle (rel axis cs:1,0.505);
    \fill[red] (rel axis cs:0,0.555) rectangle (rel axis cs:1,0.565);
    \path 
    node[anchor=east] at (rel axis cs:1,0.36) {Sand}  
    node[anchor=east] at (rel axis cs:1,0.50) {Clay}  
    node[anchor=east] at (rel axis cs:1,0.56) {Silty loam};  
    \end{scope}      
        \addplot+[error bars/.cd, y dir=both,y explicit] table [x=Sample, y=y, y error=y_err] {WHCs.dat};
    \end{axis}
    \end{tikzpicture}
Stefano
  • 1,038

1 Answers1

1

Use clip=false to avoid clipping of content falling outside the axis. Then use anchor=west in place of anchor=east for those nodes.

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{WHCs.dat}
Sample  y   y_err
5-Pa-Pr 0.520   0.10
6-Pa-Pr 0.465   0.05
7-Pa-Pr 0.768   0.16
5-Pa-S  0.517   0.10
6-Pa-S  0.562   0.20
7-Pa-S  0.794   0.35
\end{filecontents*}
\usepackage{pgfplots}
\begin{document}
  \begin{tikzpicture}
    \begin{axis}[
        clip=false,
        ybar, ymin=0,
        bar width=15pt,
        x tick label style={rotate=45, anchor=east},
        major x tick style = transparent,
        ymajorgrids = true,
        xtick = data,
        axis on top,
        enlarge x limits=0.35,
        scaled y ticks = false,
        symbolic x coords={5-Pa-Pr, 6-Pa-Pr, 7-Pa-Pr, 5-Pa-S, 6-Pa-S, 7-Pa-S},
        ymajorgrids]
   \begin{scope}
    \fill[brown] (rel axis cs:0,0.355) rectangle (rel axis cs:1,0.365);
    \fill[green] (rel axis cs:0,0.495) rectangle (rel axis cs:1,0.505);
    \fill[red] (rel axis cs:0,0.555) rectangle (rel axis cs:1,0.565);
    \path
    node[anchor=west] at (rel axis cs:1,0.36) {Sand}
    node[anchor=west] at (rel axis cs:1,0.50) {Clay}
    node[anchor=west] at (rel axis cs:1,0.56) {Silty loam};
    \end{scope}
        \addplot+[error bars/.cd, y dir=both,y explicit] table [x=Sample, y=y, y error=y_err] {WHCs.dat};
    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here