# How to draw an extra line over a pgfplot hexbin plot that uses pgfdeclareplotmark

I quite like hexagonal plots, and using Jake's code I managed to avoid falling back to R to render it.

Now, I do have a questions, how can I add a line plot that draws over it instead of under it?

MWE:

R code to generate bins:

# Load the data
library(maps)
data(world.cities)

library(hexbin)

# Generate hexbins, with the aspect ratio of the plot matching that of the data.
hbin<-hexbin(x=world.cities$long,y=world.cities$lat,xbins=100,shape=diff(range(world.cities$lat))/diff(range(world.cities$long)))

# Write the datafile. hcell2xy extracts the centroids of the hexagons
write.table(data.frame(hcell2xy(hbin),slot(hbin,"count")),row.name=F,file="testdata.csv")


tex code:

\documentclass{standalone}
\usepackage{siunitx}
\usepackage{pgfplots, pgfplotstable}

\sisetup{round-mode=places,round-precision=0}

\pgfdeclareplotmark{hexagon}
{%
\pgfpathmoveto{\pgfqpoint{0pt}{1.1547\pgfplotmarksize}}
\pgfpathlineto{\pgfqpointpolar{150}{1.1547\pgfplotmarksize}}
\pgfpathlineto{\pgfqpointpolar{210}{1.1547\pgfplotmarksize}}
\pgfpathlineto{\pgfqpointpolar{270}{1.1547\pgfplotmarksize}}
\pgfpathlineto{\pgfqpointpolar{330}{1.1547\pgfplotmarksize}}
\pgfpathlineto{\pgfqpointpolar{30}{1.1547\pgfplotmarksize}}
\pgfpathclose
\pgfusepathqfill
}

\pgfplotsset{
colormap={grayred}{color(0cm)=(black!10); color(1cm)=(red!75!black)}
}

\begin{document}
\begin{tikzpicture}[]
\begin{axis}[
enlargelimits=false,
colorbar, colormap name=grayred,
scale only axis,width=10cm,unit vector ratio*=1 1 1,
enlarge x limits={abs=2},enlarge y limits={abs=2},
xlabel=Longitude, ylabel=Latitude, xticklabel={\SI{\tick}{\degree}},yticklabel={\SI{\tick}{\degree}},
]
scatter, scatter/use mapped color={draw=mapped color, fill=mapped color},
scatter src=explicit,
only marks,
mark=hexagon,mark size=\pgfkeysvalueof{/pgfplots/width}/100/2
] table [meta index=2] {testdata.csv};

\end{axis}
\end{tikzpicture}

\end{document}


Question: how to get the blue line plot to draw over the custom plotmarks?

A much simpler way to not draw the markers on top of everything else is to simply use clip mode=individual.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% ---------------------------------------------------------------------
% to *not* draw the markers on a separate layer on top of the lines
clip mode=individual,
% ---------------------------------------------------------------------
]
red,
samples=10,
only marks,
mark=*,
]{.5*x};
blue,
very thick,
]{0.5*x};
\end{axis}
\end{tikzpicture}
\end{document}


You could use set layers for the axis environment and on layer=axis foreground for the blue line.

Here is a simple example (I can not use the R code):

\documentclass{standalone}
\pgfplotsset{compat=1.14}% <- add this, current version is 1.14

\pgfdeclareplotmark{hexagon}
{%
\pgfpathmoveto{\pgfqpoint{0pt}{1.1547\pgfplotmarksize}}
\pgfpathlineto{\pgfqpointpolar{150}{1.1547\pgfplotmarksize}}
\pgfpathlineto{\pgfqpointpolar{210}{1.1547\pgfplotmarksize}}
\pgfpathlineto{\pgfqpointpolar{270}{1.1547\pgfplotmarksize}}
\pgfpathlineto{\pgfqpointpolar{330}{1.1547\pgfplotmarksize}}
\pgfpathlineto{\pgfqpointpolar{30}{1.1547\pgfplotmarksize}}
\pgfpathclose
\pgfusepathqfill
}

\begin{document}
\begin{tikzpicture}[]
\begin{axis}[
width=10cm
]
red,
samples=10,
only marks,
mark=hexagon,
mark size=\pgfkeysvalueof{/pgfplots/width}/100
]{.5*x};