[Tex/LaTex] background for pgfplots and other tikz constructs

backgroundspgfplotstikz-pgf

The answer to ybar stacked interferes with tikz background raises further questions I solved some of them. With the background commented out the MWE below produces this image:

enter image description here

With the background commented in, I see

enter image description here

  • The background is behind the plot, but covers my carefully placed labels.
  • The background image must be positioned behind and slightly larger than all the content (including the separately placed title and legend). Fixed: making the background a node did the trick.
  • The ragged edge should apply to the full background image. Fixed

Finally, as much of the placement as possible should be computed rather than done by trial and error. I've isolated most of the sizes and positions in macros, but they are still all hard coded.

I realize that this is more than one question, but they seem to me to be naturally grouped. Other people like me who need an answer to one of them may need them all.

The MWE (not fully minimal since it has my real data).

\documentclass[border=5mm]{standalone}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable}
\usepackage{mwe}

\usetikzlibrary{calc,decorations.pathmorphing}

%Use the new axis label placement features
\pgfplotsset{compat=1.8}


\usepackage{helvet}
\usepackage[eulergreek]{sansmath}

\begin{document}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main} 

\pgfplotstableread[col sep=comma]{
category, mass, other
{dollars\\\$3.14m}, 31, 69
{donors\\56k}, 21, 79
}\warren

\newcommand{\plotsize}{width=6cm, height=5cm}
\newcommand{\boxsize}{(6,5)}
\newcommand{\boxspot}{(-2,-2)}
\newcommand{\titlepos}{(2.0,-1.5)} 
\newcommand{\legendpos}{(0.17, -0.02)} %fraction of plotsize

\pgfplotsset{every axis legend/.append 
  style={at={\legendpos},anchor=north west,fill=none}
}

\tikzset{
pencildraw/.style={ %
    decorate,
    decoration={random steps,segment length=6pt,amplitude=3pt}
    } %
}

\begin{tikzpicture}[font=\sffamily\sansmath]
\path[clip,pencildraw] \boxspot rectangle \boxsize;
\begin{axis}[ 
    clip=false,
    ybar stacked,
    ymin=0,
    ymax=100,
    bar width=35pt,
    enlarge x limits={abs=20pt},
    \plotsize,
    hide y axis,
    axis x line*=top,
    axis line style={opacity=0},
    xtick style={opacity=0},
    xtick=data,
    xticklabel style={align=center},
    xticklabel pos=upper,
    xticklabels from table={\warren}{category},
    nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
    nodes near coords align={anchor=east,xshift=-5.5mm,yshift=-3mm},
    point meta=explicit,
    ]
  \legend{Massachusetts, out of state};
  \addplot [fill=blue] table [x expr =\coordindex, y=mass, meta=mass] {\warren};
  \addplot [fill=yellow] table [x expr =\coordindex, y=other,
    meta=other]{\warren};    
\pgfplotsextra{\begin{scope}[on layer=axis background]
   \node{   {\includegraphics[scale=1.5]{example-image-A}}};
\end{scope}}
\end{axis} 
\node at \titlepos {\large Elizabeth Warren's Fundraising};
\end{tikzpicture}
\end{document}

Best Answer

The problem is caused by the combination of clip=false and ybar stacked: For some reason, this makes the nodes near coords disappear (looks like a bug to me).

You can avoid this problem by placing the paper node first, outside the axis. That way, everything will automatically be drawn on top of the paper background, without any need for using layers:

\documentclass[border=5mm]{standalone}
\usepackage{tikz,pgfplots}
\usepackage{pgfplotstable}

\usetikzlibrary{calc,decorations.pathmorphing}

%Use the new axis label placement features
\pgfplotsset{compat=1.8}


\usepackage{helvet}
\usepackage[eulergreek]{sansmath}

\begin{document}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main} 

\pgfplotstableread[col sep=comma]{
category, mass, other
{dollars\\\$3.14m}, 31, 69
{donors\\56\,000}, 21, 79
}\warren

\newcommand{\plotsize}{width=6cm, height=5cm}
\newcommand{\boxsize}{(6,5)}
\newcommand{\boxspot}{(-2,-2)}
\newcommand{\titlepos}{(2.0,-1.5)} 
\newcommand{\legendpos}{(0.17, -0.02)} %fraction of plotsize

\pgfplotsset{every axis legend/.append 
  style={at={\legendpos},anchor=north west,fill=none}
}

\tikzset{
pencildraw/.style={ %
    decorate,
    decoration={random steps,segment length=6pt,amplitude=3pt}
    } %
}

\begin{tikzpicture}[font=\sffamily\sansmath]
\path[clip,pencildraw] \boxspot rectangle \boxsize;
\node{
    \includegraphics[scale=1.5]{newsprint}
};
\begin{axis}[ 
    ybar stacked,
    ymin=0,
    ymax=100,
    bar width=35pt,
    enlarge x limits={abs=20pt},
    \plotsize,
    hide y axis,
    axis x line*=top,
    axis line style={opacity=0},
    xtick style={opacity=0},
    xtick=data,
    xticklabel style={align=center, on layer=axis foreground},
    xticklabel pos=upper,
    xticklabels from table={\warren}{category},
    nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
    nodes near coords align={anchor=east,xshift=-5.5mm,yshift=-3mm},
    point meta=explicit,
    ]
  \legend{Massachusetts, out of state};
  \addplot [fill=blue] table [x expr =\coordindex, y=mass, meta=mass] {\warren};
  \addplot [fill=yellow] table [x expr =\coordindex, y=other,
    meta=other]{\warren};    
\end{axis} 
\node at \titlepos {\large Elizabeth Warren's Fundraising};
\end{tikzpicture}
\end{document}