I want to visualize the feature space in my training areas using scatterplots. I use a nested classification schema, so my class values are not incrementing but sorted in categories like 100 (mainclass), 110 (subclass)... I have color values already specified for each class by the GIS I'm using to digitalize the training areas, so I want to have them in the scatterplot.

I already tried to update the palette with my color vector but the results are not satisfying at all. After some experimentation I noticed that for incrementing class values the result would be satisfying.

Here is a representative example:

b1 <- c(1:50)
b2 <- runif(b1, b1-10, b1+10)
val1 <- c(rep(1,10), rep(2,10), rep(3,10), rep(4,10), rep(5,10))
val2 <- c(rep(110,10), rep(120,10), rep(200,10), rep(300,10), rep(800,10))
df <- data.frame(b1, b2, val1, val2)


plot(df$b1, df$b2, col= df$val1, pch = 20)
plot(df$b1, df$b2, col= df$val2, pch = 20)

palette(c("#FF0000", "#FB5DF6", "#AF3C96", "#857EC7", "#E8D1D1"))
plot(df$b1, df$b2, col= df$val1, pch = 20)
plot(df$b1, df$b2, col= df$val2, pch = 20)

which will result in:


The plot at bottomleft would be satisfying. How can I match my palette to the not incrementing categories on the bottomright image column?


You need some sort of lookup from the coding ("100","101" etc) to colours. An easy way to do this would be to convert the coding to a character value, then convert that to a "factor". Factors have an underlying numerical storage value from 1 to N, and a visible text representation.

If you do:

> df$f_val2 = as.factor(as.character(df$val2))

then plot:

> plot(df$b1, df$b2, col= df$f_val2, pch = 20)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.