Generating rainbow contour plots, coloured levels

I'm coming up to a thesis deadline and I'm noticing some issues with the colour-coding of my contour plots.

The plot is using 2-D coordinates , peaks are supposed to represent frequency of activity in a unit/bin/sector (higher peak = more visits to that spot). I thought the colour-coding system was working on a threshold (e.g., 50+ responses = yellow, 75+ responses = green, 100+ responses = blue), but this isn't the case. You can see that some blue peaks are lower than green ones, and a great deal of variation in the size of green peaks (some maps have no dark blue at all).

I've been working on adding a level function and defining "z", I've tried a few different methods on help pages but I can't seem to get anything to change, a lot of the formulas are giving me errors. Here's the code I'm using in RStudio:

X <-(big.s07.nfb.end[,1])
Y <-(big.s07.nfb.end[,2])
nbins <- 18
x.bin <- seq (floor(min(X,na.rm = TRUE)),ceiling(max(X,na.rm = TRUE)), length=nbins)
y.bin <- seq (floor(min(Y,na.rm = TRUE)),ceiling(max(Y,na.rm = TRUE)), length=nbins)
freq <- as.data.frame(table(findInterval(X, x.bin), findInterval(Y, y.bin)))
freq2D <- diag(nbins)*0
freq2D[cbind(freq[,1], freq[,2])] <- freq[,3]
par(mfrow=c(1,2))

palette(rainbow(max(freq2D)))
cols <- (freq2D[-1,-1]+freq2D[-1,-(nbins-1)] + freq2D[-(nbins-1), -(nbins-1)] +freq2D[-(nbins-1),-1])/4
persp(freq2D, col=cols)

I've tried using different levels functions but no avail. Here's the different methods I've already been playing with:

https://stat.ethz.ch/R-manual/R-patched/library/graphics/html/filled.contour.html

I can't paste the picture here, but if you type "help (levels)" in R studio you can see the other page I've been using to diagnose problems.

Any direction/answers would be very much appreciated! I'm very new to R and under the gun!

Thanks!

Attempted answers are probably getting stuck here for want of representative data. See the FAQ: How to do a minimal reproducible example reprex for beginners.

What is sought is something that can be done, perhaps more easily, with other tools, for example {ggplot2} and {rayshader} in combination.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.