ms609 / Ternary

Create ternary plots in R
https://ms609.github.io/Ternary/
32 stars 3 forks source link

Feature suggestion: bypassing functions for countours #46

Closed cjcarlson closed 3 years ago

cjcarlson commented 3 years ago

Hi there! Big fan of what you've done here. I have some old code using ggtern I'm desperately hoping to salvage, because it does something I think is pretty neat - generates a ternary partial dependence plot for a machine learning model. Essentially, it's predicting over two of three total soil composition variables, so the third is just (100% - (a+b)), and the partials are calculated by a different function (not really easily called through Ternary). So I have a data frame with columns for a, b, c, and the partial value just ready to pass into something. Would it be possible to get Ternary to generate the color gradient and countours for those values directly? I've tried passing them into a function but it just returns NaN. Any suggestions for how to achieve this would be extremely appreciated! I'm happy to share an .RDS if that's helpful.

ms609 commented 3 years ago

The challenge is that the contouring / colouring approach needs to be able to return a value for each coordinate in ternary space – not just coordinates where a point exists and a partial has been calculated.

The approach would therefore be to interpolate values where no value of partials is available – which strikes me as something that ought to be straightforward to accomplish at a technical level, but which would take a little thought mathematically.

My initial thoughts at how a function might work would be:

yourData <- read.your.data()
InterpolatedValue <- function (a, b, c) {
  dists <- distance from (a, b, c) to each (a, b, c) coordinate in `yourData`

  WeightingFunction <- function (x) 1 / x # or something more intelligent?

  # Return:
  weighted.mean(yourData$partials, WeightingFunction(dists))
}

Perhaps you'll be able to get somewhere by this approach; if not, do e-mail on the .Rds and when time permits, I'll have a play around and try to incorporate some code into the package – it would be nice to be able to add the function to the package in either case.

ms609 commented 3 years ago

I've read up a little on interpolation. It turns out that inverse distance weighting is not a terrible proposition; I've suggested an implementation using this approach in a new vignette (see PR #47). A better solution would be to use kriging, and I should really learn how that works someday, but the moment might not be now...

cjcarlson commented 3 years ago

Thanks so much!