# background for pgfplots and other tikz constructs

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:

With the background commented in, I see

• 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}

category, mass, other
{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}

• Almost. The dollars/donors labels on top of the columns are still missing. Should this be reported as a bug? And thanks for adding my newsprint background. – Ethan Bolker Jul 5 '13 at 0:16
• @EthanBolker: Oops, completely missed that, sorry. The problem with that is that you're using the /pgfplots/on layer key in your scope without first having set /pgfplots/set layers (I'm surprised that there's no error message). However, if you do set set layers, an error occurs because of the clipping path. Two possible solutions: Either just move the paper node to the start of the tikzpicture (immediately after the clip), or use \begin{pgfonlayer}{background} instead of the scope. Do you have a particular reason for wanting the paper node in the axis? – Jake Jul 5 '13 at 0:37
• The second fix worked. The first hung with the message [Loading MPS to PDF converter (version 2006.09.02).] Accepting the answer as is - should it be edited? – Ethan Bolker Jul 5 '13 at 1:05
• @EthanBolker: I've edited the answer using the first suggestion (I find it a little bit more straightforward). Could you try whether that works for you? If it doesn't, I'll edit the answer to include the second approach. – Jake Jul 5 '13 at 1:10
• Works fine. Don't know what I did last night that didn't work ... – Ethan Bolker Jul 5 '13 at 12:45