The way to do this is as you have suggested, using the 'point in polygon approach', I mean, when you do this visually, that is exactly what you are doing in your head. The problem with the accepted answer, is that whilst in this case (and simple cases) you might be able to build a decision tree, for a situation where there are many more categories, this process would become inextricably more complicated IMHO. Perhaps categories overlap, perhaps categories are only a single point, the process that I propose here, these artefacts would have no consequence to the result.
I have already covered HERE, the production of the USDA soil classification diagram, the result I attach below, based off the dataset provided in the ggtern package.

But what may not be so clear, is that the ggtern package has some functions that make this a little less cumbersome than it has to be. Specifically, there are some internal functions in the ggtern package (routinely used in the backend) to make the transformations necessary in order to evaluate the point in polygon truth table for each point against the reference categories.
Such approach is fairly straight forward with the ddply function from the plyr package, and, the point.in.polygon function, from the sp package.
First let us load the necessary packages, and load the USDA data from ggtern. Let us also create some sample data, testing this process for a point lying on a vertice, and a point lying in the centre of a classification region!.
library(ggtern)
library(sp)
library(plyr)
#The Main Data to lookup against
data(USDA)
#The sample Data (Try a point at a vertice, and a point in the middle...)
testData = rbind(data.frame(Clay=.4,Sand=.2,Silt=.4), #Vertice point
data.frame(Clay=1,Sand=1,Silt=1)/3) #Simple middle point
I then suggest to use the internal function, transform_tern_to_cart(...)
, to convert both datasets to cartesian coordinates.
#Do the transformation to cartesian
USDA[,c("x","y")] = transform_tern_to_cart(USDA$Clay,USDA$Sand,USDA$Silt)
testData[,c("x","y")] = transform_tern_to_cart(testData$Clay,testData$Sand,testData$Silt)
By using a combination of ddply(...)
and apply(...)
, we can then test each point in the test set, against each category in the reference set via use of the point.in.polygon(...)
function.
#Create a function to do the lookup
lookup <- function(data=testData,lookupdata=USDA,groupedby="Label"){
if(!groupedby %in% colnames(lookupdata))
stop("Groupedby value is not a column of the lookupdata")
#For each row in the data
outer = apply(data[,c("x","y")],1,function(row){
#for each groupedby in the lookupdata
inner = ddply(lookupdata,groupedby,function(df){
if(point.in.polygon(row[1],row[2],df$x,df$y) > 0) #Is in polygon?
return(df) #Return a valid dataframe
else
return(NULL) #Return nothing
})
#Extract the groupedby data from the table
inner = unique(inner[,which(colnames(inner) == groupedby)])
#Join together in csv string and return to 'outer'
return(paste(as.character(inner),collapse=","))
})
#Combine with the original data and return
return(cbind(data,Lookups=outer))
}
Which can then be called in the following manner:
#Execute
lookup()
You will notice that the first point satisfies four (4) categories, and the second, only one (1), as to be expected.