Hi all, While exploring? packages for 3D plots that several folks suggested (Tnx all!) It seems what I really need is a contour plot.? This is not working int he Deducer GUI. This will be an aid to separating bats by their vocal signatures. What I need to do is plot *Fc *against *Sc* with the third dimension being the *density* of the data points in the Fc-Sc plot. Data format is like this abbreviated sample.? Fc is a frequency in kHz and Sc is the characteristic slope? (octaves per second) of each call pulse. Any suggestions, guidance greatly appreciated. Bruce Fc Sc 26.58 -5.95 27.03 -8.2 27.16 -2.07 26.19 -7.68 26.62 -3.99 26.85 -6.08 26.94 0 26.1 -5.74 26.62 -5.96 26.85 -4.05 26.98 -4.09 26.02 -5.69 26.53 -7.89 26.62 -2 26.8 -4.04 28.73 7 25.72 -2.97 26.14 -5.76 26.32 -3.89 26.4 0 26.32 5.88 [[alternative HTML version deleted]]
Hi Neo, It's a bit of a guess, but try this: bat_call<-read.table(text="Fc Sc 26.58 -5.95 27.03 -8.2 27.16 -2.07 26.19 -7.68 26.62 -3.99 26.85 -6.08 26.94 0 26.1 -5.74 26.62 -5.96 26.85 -4.05 26.98 -4.09 26.02 -5.69 26.53 -7.89 26.62 -2 26.8 -4.04 28.73 7 25.72 -2.97 26.14 -5.76 26.32 -3.89 26.4 0 26.32 5.88", header=TRUE) library(plotrix) color2D.matplot(makeDensityMatrix(bat_call$Fc,bat_call$Sc,nx=5,ny=5, zfun="sum",xlim=range(bat_call$Fc),ylim=range(bat_call$Sc)), main="Map of bat calls",extremes=c("blue","red"),xlab="Frequency", ylab="Characteristic slope",axes=FALSE) axis(1,at=seq(0.5,4.5,1),seq(26.3,28.3,0.5)) axis(2,at=seq(0.5,4.5,1),seq(4,-11.2,-3.5)) color.legend(-0.5,-0.65,1,-0.45,legend=seq(0,4,length.out=5), rect.col=color.scale(0:4,extremes=c("blue","red")),align="rb") text(0.25,-0.89,"Density",xpd=TRUE) Jim On Mon, Jun 1, 2020 at 3:16 AM Neotropical bat risk assessments <neotropical.bats at gmail.com> wrote:> > Hi all, > > While exploring packages for 3D plots that several folks suggested (Tnx > all!) > It seems what I really need is a contour plot. This is not working int > he Deducer GUI. > > This will be an aid to separating bats by their vocal signatures. > What I need to do is plot *Fc *against *Sc* with the third dimension > being the *density* of the data points in the Fc-Sc plot. > > Data format is like this abbreviated sample. Fc is a frequency in kHz > and Sc is the characteristic slope (octaves per second) of each call pulse. > > Any suggestions, guidance greatly appreciated. > Bruce > > Fc Sc > 26.58 -5.95 > 27.03 -8.2 > 27.16 -2.07 > 26.19 -7.68 > 26.62 -3.99 > 26.85 -6.08 > 26.94 0 > 26.1 -5.74 > 26.62 -5.96 > 26.85 -4.05 > 26.98 -4.09 > 26.02 -5.69 > 26.53 -7.89 > 26.62 -2 > 26.8 -4.04 > 28.73 7 > 25.72 -2.97 > 26.14 -5.76 > 26.32 -3.89 > 26.4 0 > 26.32 5.88 > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
Hi Bruce, With a much larger data set, you would see a smoother plot like your sample. I plotted frequency as the abcissa and slope as the ordinate. It looks as though your sample has it the other way round and the plot limits are extended beyond the range of the data. However, makeDensityMatrix and color2D.matplot could produce a plot like it. Jim On Mon, Jun 1, 2020 at 11:13 AM Neotropical bat risk assessments < neotropical.bats at gmail.com> wrote:> Tnx Jim > > Great help. > I need to read about package plotrix . > Hoping to achieve something like this sample on right. >[[alternative HTML version deleted]]
Hi, I'm probably biased. But my package, bivariate, contains a wrapper for KernSmooth::bkde2D, which can produce both 3D surface plots and (pretty) contour plots of bivariate kernel density estimates, conveniently. https://cran.r-project.org/web/packages/bivariate/vignettes/bivariate.pdf (pages 18 to 19) On Mon, Jun 1, 2020 at 5:16 AM Neotropical bat risk assessments <neotropical.bats at gmail.com> wrote:> > Hi all, > > While exploring packages for 3D plots that several folks suggested (Tnx > all!) > It seems what I really need is a contour plot. This is not working int > he Deducer GUI. > > This will be an aid to separating bats by their vocal signatures. > What I need to do is plot *Fc *against *Sc* with the third dimension > being the *density* of the data points in the Fc-Sc plot. > > Data format is like this abbreviated sample. Fc is a frequency in kHz > and Sc is the characteristic slope (octaves per second) of each call pulse. > > Any suggestions, guidance greatly appreciated. > Bruce > > Fc Sc > 26.58 -5.95 > 27.03 -8.2 > 27.16 -2.07 > 26.19 -7.68 > 26.62 -3.99 > 26.85 -6.08 > 26.94 0 > 26.1 -5.74 > 26.62 -5.96 > 26.85 -4.05 > 26.98 -4.09 > 26.02 -5.69 > 26.53 -7.89 > 26.62 -2 > 26.8 -4.04 > 28.73 7 > 25.72 -2.97 > 26.14 -5.76 > 26.32 -3.89 > 26.4 0 > 26.32 5.88 > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
Good morning Bruce & Abby, The fruit bats of Sydney have retreated to their camps so I can finally answer your last two queries. Attached is a plot of your data set on a 100 x 100 grid. This is how I did it: bfs<-read.csv("Procen_sample.csv") dim(bfs) names(bfs) library(plotrix) # set the matrix limits a bit beyond the data ranges fcsc_mat<-makeDensityMatrix(bfs$Fc,bfs$Sc,nx=100,ny=100, zfun="sum",xlim=c(24,29),ylim=c(-20,10)) png("bat_call.png") par(mar=c(6,4,4,2)) color2D.matplot(fcsc_mat, main="Freqency by chirp slope of bat calls", extremes=c("yellow","red"),xlab="Frequency (kHz)", ylab="Characteristic slope (octaves/s)", border=NA,axes=FALSE) 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() Abby's bivariate package looks like it will do some things that color2D.matplot won't. However, I haven't had time to install it and try it out, so I don't know whether it will be as easy to plug different calls onto the same grid. Also, there appears to be constraints on the frequency and slope in the calls and I don't know enough about them to say why. Further tweaking may lead to better solutions. Jim -------------- next part -------------- A non-text attachment was scrubbed... Name: bat_call.png Type: image/png Size: 18612 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200602/af0030c1/attachment.png>
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)
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 at 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 at 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.-------------- next part -------------- A non-text attachment was scrubbed... Name: as_bat_call.png Type: image/png Size: 26580 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200602/17c1cb64/attachment.png>