function PVal = k2_global_plot(loc,n1,sims,D,s)

%K2_GLOBAL compares two point populations, and plots p-values for 
%clustering of pop1 relative to pop1 + pop2

%NOTE 1: By convention, P-Values at each distance h are heredefined to be  
%        the probability of getting a frequency count greater than the given
%        value IF pop1 were a random draw from pop1 + pop2. So low P-Values 
%        correspond to significant CLUSTERING of pop1 vs pop2.

%NOTE 2: Ties are treated neutrally, adding .5 rather 1 to rankings.


%Written by: TONY E. SMITH, 2/3/01

%Programs Called: k2_glob

% INPUTS:
%     (i)   loc  = combined location file [loc(i)=(Xi, Yi),i=1..n]
%                  where n = n1 + n2. It is assumed that points for
%                  pop1 are in the first n1 rows.]
%     (ii)  n1   = size of pop1.
%     (iii) sims = number of relabelings
%     (iv)  D    = vector of distance values at which to measure clustering.

%     (v)   s    = optional specification of random seed. Default seed = 23311.
%                  s = 1 if random seed is to be used.
%                  s = 5_digit positive integer to be used as seed 



% OUTPUTS:  PVal = Vector of P-Values at each distance.

% SCREEN OUTPUT: Plot of P-Values at each distance.

% SET Seed Value for Random Number generator


if nargin == 4
   
    s = 23311; %forest seed (default)   
    
   %s =      ; %myrtles seed
   
elseif s == 1
   
   s = round(100000*rand(1))
   
else
   
   s = s;
   
end


% CALL K2_GLOB

PVal = k2_global(loc,n1,sims,D,s);

% Compute Dmin

%  Dmin = Minimum distance between points in the given point pattern.
%         (This is useful for interpreting the resulting plot.)

L = loc(1:n1,:);

Dist = dist_vec(L);

Dmin = min(Dist)

% PLOT RESULTS

plot(D,PVal,'k.','MarkerSize',20);

hold on;

plot(D,PVal,'k-','LineWidth',2);

hold on;

SIG = .05*ones(length(D),1);

plot(D,SIG,'r--');

plot(D,1-SIG,'r--');

xlabel('Radius','Fontsize',12, 'FontWeight', 'bold');

ylabel('clustered                    P-Value                   dispersed',...
   'Fontsize',12, 'FontWeight', 'bold');



