[Tex/LaTex] Fill area between curves

pgfplots

I need to reproduce the following plot:
CAP Curve

I tried the following code using pgfplots:

\documentclass[8pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\begin{document}
  \begin{frame}
  \begin{tikzpicture}
  \begin{axis}[domain=0:1,
   width=10cm,
  minor tick num=3,
   height=8cm,
  ymin=0,
  ymax=1,
  xmin=0,
  xmax=1,
  xticklabel=\empty,
  yticklabel=\empty,
  samples=50,
  smooth,
  xlabel=$r_{k}$,
  ylabel=$s_{k}$,
  extra x ticks={0,1},
  extra y ticks={1},
  extra x tick style={grid=major,thick},
  extra y tick style={grid=major,thick},
  extra x tick labels={$0$,$1$},
  extra y tick labels={$1$},
  legend pos=north east]


\addplot[fill=green,draw=none,domain=0:1,pattern=north east lines,pattern      color=yellow,stack plots=y]{x^0.25-x} \closedcycle;
\addplot[fill=green,draw=none,domain=0:1,stack plots=y]{1-x}\closedcycle;

\addplot[id=naive,color=black,domain=0:1,samples=50] {x};
\addplot[id=mod,color=black,domain=0:1,samples=50]{x^0.25};
\addplot[id=perf,color=black,domain=0:1,samples=50]{1};


\node (a) at (axis cs:0.4,0.6)  {$a_{R}$};
\node (b) at (axis cs:0.1,0.8)  {$a_{P}$};
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}

I don't get the shapes right. The filled areas are inverted and do not apply on the designated region. Followed many similar questions and tried different things and none worked for me so far.

Best Answer

The question didn't contain the functions or coordinates needed to produce the graphs showed in the image, so I used some dummy coordinates, but the idea is the same. Two customizable filling patterns were also defined:

\documentclass[8pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}

% New customizable pattern
\tikzset{
  hatch distance/.store in=\hatchdistance,
  hatch distance=8pt,
  hatch thickness/.store in=\hatchthickness,
  hatch thickness=5pt,
  hatch color/.store in=\hatchcolor,
  hatch color=gray!20
}

\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{thick vlines}
  {\pgfpointorigin}{\pgfqpoint{\hatchthickness}{100pt}}{\pgfqpoint{\hatchdistance}{100pt}}%
  {
  \pgfsetlinewidth{\hatchthickness}
  \pgfpathmoveto{\pgfqpoint{0.5pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{0.5pt}{100pt}}
  \pgfusepath{stroke}
  }
\pgfdeclarepatterninherentlycolored[\hatchcolor]{crosshatch dots color}
{\pgfpointorigin}{\pgfpoint{8pt}{8pt}}
{\pgfpoint{8pt}{8pt}}
{
  \pgfsetfillcolor{\hatchcolor}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{8pt}{8pt}}
  \pgfusepath{fill}
  \pgfsetfillcolor{\hatchcolor}
  \pgfpathcircle{\pgfpoint{2pt}{1.75pt}}{20pt}
  \pgfpathcircle{\pgfpoint{6pt}{5.75pt}}{20pt}
  \pgfusepath{fill}
  \pgfsetfillcolor{pgf@darklightsteelblue!70}
  \pgfpathcircle{\pgfpoint{2pt}{2.25pt}}{0.4pt}
  \pgfpathcircle{\pgfpoint{6pt}{6.25pt}}{0.4pt}
  \pgfusepath{fill}
}

\begin{document}

\begin{frame}
  \begin{tikzpicture}
  \begin{axis}[domain=0:1,
   width=10cm,
  minor tick num=3,
   height=8cm,
  ymin=0,
  ymax=1.2,
  xmin=0,
  xmax=1,
  xticklabel=\empty,
  yticklabel=\empty,
  samples=50,
  smooth,
  xlabel=$r_{k}$,
  ylabel=$s_{k}$,
  extra x ticks={0,1},
  extra y ticks={1},
  extra x tick style={grid=major,thick},
  extra y tick style={grid=major,thick},
  extra x tick labels={$0$,$1$},
  extra y tick labels={$1$},
  legend pos=north east]

\draw[pattern=crosshatch dots color]
  (axis cs:0,0) -- (axis cs:0.12,1) -- (axis cs:1,1) -- (axis cs:0,0);
\addplot+[
  id=mod,
  color=black,
  domain=0:1,
  samples=50,
  pattern=thick vlines,
  hatch thickness=3pt
] coordinates {(0,0) (0.2,0.45) (0.5,0.8) (0.75,0.92) (1,1)};
\addplot[id=naive,color=black,domain=0:1,samples=50] {x} ;

\node (a) at (axis cs:0.2,0.8)  {$a_{P}$};
\node[draw,fill=gray!30] (b) at (axis cs:0.45,0.6)  {$a_{R}$};

\begin{scope}[
  every pin edge/.style={<-,>=latex},
  pin distance=1.5cm
]
\node[pin=-30:{Rating model},] at (axis cs:0.5,0.8) {};
\node[pin=-30:{Random model},] at (axis cs:0.3,0.3) {};
\node[pin=15:{Perfect model},] at (axis cs:0.4,1.01) {};
\end{scope}
\end{axis}
\end{tikzpicture}
\end{frame}

\end{document}

enter image description here

And using x^0.45 to produce the curved path:

\documentclass[8pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

% New customizable pattern
\tikzset{
  hatch distance/.store in=\hatchdistance,
  hatch distance=8pt,
  hatch thickness/.store in=\hatchthickness,
  hatch thickness=5pt,
  hatch color/.store in=\hatchcolor,
  hatch color=gray!20
}

\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{thick vlines}
  {\pgfpointorigin}{\pgfqpoint{\hatchthickness}{100pt}}{\pgfqpoint{\hatchdistance}{100pt}}%
  {
  \pgfsetlinewidth{\hatchthickness}
  \pgfpathmoveto{\pgfqpoint{0.5pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{0.5pt}{100pt}}
  \pgfusepath{stroke}
  }
\pgfdeclarepatterninherentlycolored[\hatchcolor]{crosshatch dots color}
{\pgfpointorigin}{\pgfpoint{8pt}{8pt}}
{\pgfpoint{8pt}{8pt}}
{
  \pgfsetfillcolor{\hatchcolor}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{8pt}{8pt}}
  \pgfusepath{fill}
  \pgfsetfillcolor{\hatchcolor}
  \pgfpathcircle{\pgfpoint{2pt}{1.75pt}}{20pt}
  \pgfpathcircle{\pgfpoint{6pt}{5.75pt}}{20pt}
  \pgfusepath{fill}
  \pgfsetfillcolor{pgf@darklightsteelblue!70}
  \pgfpathcircle{\pgfpoint{2pt}{2.25pt}}{0.4pt}
  \pgfpathcircle{\pgfpoint{6pt}{6.25pt}}{0.4pt}
  \pgfusepath{fill}
}

\begin{document}

\begin{frame}
  \begin{tikzpicture}
  \begin{axis}[domain=0:1,
   width=10cm,
  minor tick num=3,
   height=8cm,
  ymin=0,
  ymax=1.2,
  xmin=0,
  xmax=1,
  xticklabel=\empty,
  yticklabel=\empty,
  samples=50,
  smooth,
  xlabel=$r_{k}$,
  ylabel=$s_{k}$,
  extra x ticks={0,1},
  extra y ticks={1},
  extra x tick style={grid=major,thick},
  extra y tick style={grid=major,thick},
  extra x tick labels={$0$,$1$},
  extra y tick labels={$1$},
  legend pos=north east]

\draw[pattern=crosshatch dots color]
  (axis cs:0,0) -- (axis cs:0.05,1) -- (axis cs:1,1) -- (axis cs:0,0);
\addplot[
  id=mod,
  color=black,
  domain=0:1,
  samples=100,
  pattern=thick vlines,
  hatch thickness=3pt
] {(x^(0.45))};
\addplot[id=naive,color=black,domain=0:1,samples=50] {x} ;

\node (a) at (axis cs:0.2,0.8)  {$a_{P}$};
\node[draw,fill=gray!30] (b) at (axis cs:0.45,0.6)  {$a_{R}$};

\begin{scope}[
  every pin edge/.style={<-,>=latex},
  pin distance=1.5cm
]
\node[pin=-30:{Rating model},] at (axis cs:0.5,0.8) {};
\node[pin=-30:{Random model},] at (axis cs:0.3,0.3) {};
\node[pin=15:{Perfect model},] at (axis cs:0.4,1.01) {};
\end{scope}
\end{axis}
\end{tikzpicture}
\end{frame}

\end{document}

enter image description here

In general, the following code can be used to fill the area between the graph of two functions f and g:

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

\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  domain=-2:9,
  samples=60,
  stack plots=y
]
% draw graph for the first function f
\addplot+[mark=none] {(x-3)^2};
% draw graph of max(g-f, 0) and stack
\addplot+[mark=none,fill=green!40,draw=red] {max(x+2-((x-3)^2),0)} \closedcycle;
% draw graph of min(g-f, 0) and stack
\addplot+[mark=none,fill=none,draw=red] {min(x+2-((x-3)^2),0)};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here