# Plot weighted frequency matrix

I wish to create this plot preferably in pgfplots or tikz.

I have actually got a version plotted (with help from SO), but I have used R and exported the plot using tikzDevice. I can't post the tikz code because it is way over the limit for posting (so I'll give the R code instead).

ps <- ldply(0:35, function(i)data.frame(s=0:i, n=i))
plot.new()
plot.window(c(0,36), c(0,1))
apply(ps[ps$s<6 & ps$n - ps\$s < 30, ], 1, function(x){
s<-x[1]; n<-x[2];
lines(c(n, n+1, n, n+1), c(s/n, s/(n+1), s/n, (s+1)/(n+1)), type="o")})
axis(1)
axis(2)
lines(6:36, 6/(6:36), type="o")
# need to fill in the unconnected points on the upper frontier


Q. So looking to plot directly with tikz/pgfplots rather than via R and tikzdevice.

-

You can do this with three plots: One for the decreasing lines, one for the increasing ones (with dots), and one for the row of six dots at the top. To make the increasing lines stop at the last decreasing one, you can use a comparison operator that becomes 0 when the increasing line is higher than the last decreasing one, and divide by that: Unbounded coordinates are dropped by default.

\documentclass{article}
\usepackage{pgfplotstable}

\begin{document}
\begin{tikzpicture}[
declare function={f(\x,\n) = (1/(\x)*(\n));}
]
\begin{axis}[
restrict y to domain=0:1,
axis lines*=left,
ymax=1,ymin=0,
xmin=1, xmax=36,
xtick = {1,6,...,36},
ytick = {0,0.5,1},
grid=both,
mark size=1, mark=*
]

% Draw decreasing lines
\pgfplotsinvokeforeach{1,...,6}{
}

% Draw increasing lines, but only up to the level of the last decreasing line
% (that's what the "1 / ..." part does. 0.001 is for preventing precision errors)
\pgfplotsinvokeforeach{1,...,30}{
\addplot [domain=1:36, samples=36] {1-f(x,#1) * 1/( (1-(f(x,#1))) <= (f(x,6)+0.001)};
}

% Draw top markers