[R] Query on contour plots

Neotropical bat risk assessments neotrop|c@|@b@t@ @end|ng |rom gm@||@com
Tue Jun 2 14:33:36 CEST 2020

Hi all,

Many thanks for the efforts and suggestions.

This is getting closer to what is needed.  No legend showing the density 
values yet.
I was able to replicate a similar plot with the original data set.
However when I tried this with a different data set that has other Fc & 
Sc values  the plot does not work... just a blank PNG
Code from console below:

 > dim(bfs)
[1] 5638   17
 > names(bfs)
  [1] "Filename" "st"       "Dur"      "TBC"      "Fmax" "Fmin"     "Fmean"
  [8] "Tk"       "Fk"       "Qk"       "Tc"       "Fc" "Dc"       "S1"
[15] "Sc"       "Qual"     "Pmc"
 > library(plotrix)
 > # set the matrix limits a bit beyond the data ranges
 > fcsc_mat<-makeDensityMatrix(bfs$Fc,bfs$Sc,nx=25,ny=25,
+ zfun="sum",xlim=c(24,29),ylim=c(-20,10))
Range of density (>0) - Inf -Inf
Warning messages:
*1: In min(x) : no non-missing arguments to min; returning Inf**
**2: In max(x) : no non-missing arguments to max; returning -Inf*
 > png("bat_call_plot.png")
 > par(mar=c(6,4,4,2))
 > color2D.matplot(fcsc_mat,
+ main="Freqency by slope of bat calls",
+ extremes=c("yellow","red"),xlab="Frequency (kHz)",
+ ylab="Characteristic slope (octaves/s)",
+ border=NA,axes=FALSE)
Warning messages:
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf
 > axis(1,at=seq(5,95,10),round(seq(24.5,28.5,length.out=10),1))
 > axis(2,at=seq(5,95,10),round(seq(-20,10,length.out=10),1))
 > color.legend(0,-14,25,-10,legend=seq(0,10,length.out=5),
+ rect.col=color.scale(0:4,extremes=c("yellow","red")),align="rb")
 > text(12.5,-20,"Density (cell count)",xpd=TRUE)
 > dev.off()
null device

I will not need to add a function it iterate as I will not be running  
this as an iterative task at one time... I just need the code to be able 
to use different data sets that have the same fields.
The Sc values over the 200+ data sets will range from potentially large 
negative numbers to positive numbers depending on the slope of the 
calls, i.e. increasing frequencies or decreasing frequencies.
An example of these two parameters for a single species with descriptive 
N is valid number of call pulses, then 10%-90% bins of where the call 
pulses fall into.

Parameters 	N 	Min 	Max 	Mean 	St.Dev 	10% 	25% 	75% 	90%
Fc 	32802 	43.01 	50.00 	46.86 	1.31 	45.07 	45.98 	47.76 	48.63
Sc 	32802 	-309.78 	13.76 	-6.60 	10.98 	-10.31 	-7.50 	-3.91 	-2.81

I am very appreciative and thank you both for guiding the efforts.

> Very nice. I forgot that you didn't have the complete data set.
> png("as_bat_call.png")
> plot_ds (bfs[,c("Fc","Sc")], "plot 1", xlim = c (25, 30), ylim = c (-15, 10),
>          k1=1.25, k2=1.25)
> dev.off()
> Jim
> On Tue, Jun 2, 2020 at 6:24 PM Abby Spurdle <spurdle.a using gmail.com> wrote:
>> I'm putting this back on the list.
>>> So how would I set up the code to do this with the data type I have?
>>> I will need to replicate the same task > 200 times with other data sets.
>>> What I need to do is plot *Fc *against *Sc* with the third dimension being the *density* of the data points.
>> Using Jim's bat_call data:
>>      library (bivariate)
>>      plot_ds <- function (dataset, main="", xlim, ylim, ..., k1=1, k2=1)
>>      {   names <- names (dataset)
>>          fh <- kbvpdf (dataset [,1], dataset [,2], k1 * bw.nrd (dataset
>> [,1]), k2 * bw.nrd (dataset [,2]) )
>>          plot (fh, main=main, xlab = names [1], ylab = names [2],
>>              xlim=xlim, ylim=ylim,
>>              ncontours=2)
>>      }
>>      plot_ds (bat_call, "plot 1", k1=1.25, k2=1.25)
>> Note that I've used stats::bw.nrd.
>> The k1 and k2 values, simply scale the default bandwidth.
>> (In this case, I've increased the smoothness).
>> If you want to do it 200+ times:
>> (1) Create another function, to iterate over each data set.
>> (2) If you want to save the plots, you will need to add in a call to
>> pdf/png/etc and close the device, in each iteration.
>> (3) It may be desirable to have constant xlim/ylim values, ideally
>> based on the ranges of the combined data:
>>      plot_ds (bat_call, "plot 1", xlim = c (25, 30), ylim = c (-15, 10),
>>          k1=1.25, k2=1.25)
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.

Bruce W. Miller, PhD.
Neotropical bat risk assessments
Conservation Fellow - Wildlife Conservation Society

If we lose the bats, we may lose much of the tropical vegetation and the lungs of the planet

Using acoustic sampling to identify and map species distributions
and pioneering acoustic tools for ecology and conservation of bats for >25 years.

Key projects include providing free interactive identification keys and call fact sheets for the vocal signatures of New World Bats

	[[alternative HTML version deleted]]

More information about the R-help mailing list