Reputation: 293
I'm in the process of creating a soil texture triangle based on 3 layers i.e clay, silt and sand. I get the error below when I run the code.
library("raster", "soiltexture")
clay <-raster('af_CLYPPT_T__M_sd1_250m.tif')
silt <-raster('af_SLTPPT_T__M_sd1_250m.tif')
sand <-raster('af_SNDPPT_T__M_sd1_250m.tif')
clay.df = as.data.frame(clay, xy = FALSE, na.rm = TRUE)
silt.df = as.data.frame(silt, xy = FALSE, na.rm = TRUE)
sand.df = as.data.frame(sand, xy = FALSE, na.rm = TRUE)
my.data <- data.frame("CLAY" = c(clay.df),"SILT" = c(silt.df),"SAND" = c(sand.df))
cols <- c("CLAY","SILT","SAND")
colnames(my.data) <- cols
TT.plot(
class.sys = "HYPRES.TT",
tri.data = my.data,
main = "Soil texture data"
)
Error in TT.data.test(tri.data = tri.data, css.names = css.names, text.sum = text.sum, : The sum of the 3 plotted variables should be around 100: check the data, or change 'text.tol' parameter.
How do I set all variables greater that 100 to be equal to 100?
Upvotes: 0
Views: 976
Reputation: 47146
To assure that the values add up to 100 you can do
my.data <- 100 * my.data / rowSums(my.data)
e.g.,
m <- cbind(1:10, 1:10, 1:10)
100 * m / rowSums(m)
The first part can be expressed more succinctly (3 lines in stead of 9):
s <- stack('af_CLYPPT_T__M_sd1_250m.tif', 'af_SLTPPT_T__M_sd1_250m.tif', 'af_SNDPPT_T__M_sd1_250m.tif')
names(s) <- c("CLAY","SILT","SAND")
my.data <- as.data.frame(s, na.rm=TRUE)
Upvotes: 1