Forum: help
Monitor Forum | Start New ThreadRE: soil class in soil texture triangle [ Reply ] By: corentin clement on 2011-05-26 10:12 | [forum:4489] |
Thank very much it works very well with best Corentin |
RE: soil class in soil texture triangle [ Reply ] By: Julien Moeys on 2011-05-26 10:00 | [forum:4487] |
Yes, if you use the 'col' argument in cat2colFun(), then the number of colors passed to col must be the same as the number of categories in data[,4] (which was the secret meaning of the error message in fact). If you want to avoid that, either avoid using 'col' (and let the function chose the right number of colors for you... but they may be 'ugly') or use something like ### colArg <- hsv( seq( .1, .4, length.out = length(unique(data[,4])) ), .9, .9 ) colz <- cat2colFun( data[,4], col = colArg ) # etc. ### that will generate the right number of colors with best Julien |
RE: soil class in soil texture triangle [ Reply ] By: corentin clement on 2011-05-26 09:47 | [forum:4486] |
Thank you for answering so quikly, Your code give me exactly what I wanna do. If I can ask others questions (I am a begginer in using R) When I use the complicate formula with my data (29 rows and 4 collumns). I have an error message about the length of my table I suppose it is in the formula to calibrate colours (see below the message) colz <- cat2colFun( data[,4], col = hsv( seq( .1, .4, .1 ), .9, .9 ) ) # 'Calibrate' r dans cat2colFun(data[, 4], col = hsv(seq(0.1, 0.4, 0.1), 0.9, 0.9, : length(col) must be equal to length( unique(i) ) thank you very much Corentin |
RE: soil class in soil texture triangle [ Reply ] By: Julien Moeys on 2011-05-26 08:53 | [forum:4485] |
Hi Corentin There are no "build-in" functionality in TT.plot for plotting categorical variables (although it would be good to have some, I agree!). And z.names only handles continuous variables. But you can go around that problem, like with the standard plot() in R: Here is an example code of what you can do: ### R code ------------------------------------------------- data <- data.frame( "CLAY" = c(10,20,30,40), "SILT" = c(40,30,20,10), "SAND" = c(50,50,50,50), "FAO_CLASS" = c("A","B","C","D"), stringsAsFactors = TRUE ) # library( "soiltexture" ) ### The easy way: colz <- as.numeric( as.factor( data[,"FAO_CLASS"] ) ) TT.plot( class.sys = "FAO50.TT", tri.data = data, main = "Soil texture data", col = colz, cex = 2, pch = 21 ) # ### Alternative, no color TT.plot( class.sys = "FAO50.TT", tri.data = data, main = "Soil texture data", cex = 2, pch = colz ) # ### The complicated way, using a home made-function cat2colFun <- function( i, col=NULL, colFun = rainbow ){ u.i <- unique( i ) n.i <- length( u.i ) # if( is.null(col) ){ col <- colFun(n=n.i) }else{ if( length(col) != n.i ){ stop( "length(col) must be equal to length( unique(i) )" ) } # } # # fun.text <- paste( sep = "", "function( \n", " i \n", "){ # \n", " i.f <- factor( \n", " x = i, \n", " levels = ", paste(deparse(u.i),collapse=""), ", \n", " labels = ", paste(deparse(col),collapse=""), " \n", " ) # \n", " # \n", " return( as.character( i.f ) ) \n", "} # \n" ) # # return( eval( parse( text = fun.text ) ) ) } # colz <- cat2colFun( data[,4] ) # 'Calibrate' colz2 <- colz( data[,4] ) # Get the colors # NB: class(colz) is "function" TT.plot( class.sys = "FAO50.TT", tri.data = data, main = "Soil texture data", col = colz2, cex = 2, pch = 21 ) # # NB: colz() can be re-used, for bulding a legend for example, # or on a new dataset with the same FAO_CLASS's legend( x = "topright", legend = unique( data[,4] ), pch = 21, col = colz( unique( data[,4] ) ), pt.lwd = 2, pt.cex = 2 ) # ### Chose the colors colz <- cat2colFun( data[,4], col = hsv( seq( .1, .4, .1 ), .9, .9 ) ) # 'Calibrate' colz2 <- colz( data[,4] ) # Get the colors TT.plot( class.sys = "FAO50.TT", tri.data = data, main = "Soil texture data", col = colz2, cex = 2, pch = 21 ) # ### End of R code Please do not hesitate if you have other questions With best Julien |
soil class in soil texture triangle [ Reply ] By: corentin clement on 2011-05-26 07:35 | [forum:4484] |
HI I am using the R software to plot my soil texture I am using the classification of the FAO and my database is composed with four columns (see below): CLAY SILT SAND FAO CLASS 20 30 50 ACRISOL I Manage to plot all my soil texture but I have different class of soil (FAO class) and I would like to make the color plot different as the FAO class change (to see the range of texture inside the FAO classes) . I think I have to use the function z.name but with all informations I found on internet I don't manage to do it. My code is the following one : TT.plot( class.sys = "FAO50.TT", tri.data = data, main = "Soil texture data", col="limegreen",cex=0.5,pch=21,) Could you please help me or give me the formula or one code applied for others data. Hope you understand Regards C |