I have been trying to create a legend for an R plot with the rainbow option but I am facing some difficulties.
I plot
plot(test$a,test$b, col = rainbow(length(test$s))[rank(test$s)])
with the colour assigned according to test$s. The problem is that test$s is equal for many values of the data frame test so if than I write
legend('topright',legend=test.sub$s,col=rainbow(length(test.sub$s))
[rank(test.sub$s)])
I get in the legend all duplicates of test$s but the colours are correct. Since I don't want the duplicates I wrote
legend('topright',legend=unique(test.sub$s),col=rainbow(length(test.sub$s))
[rank(test.sub$s])
but then all the colours are messed up!
Thanks in advance
您的问题是, unique(test.sub$s)
的长度与rainbow(length(test.sub$s))[rank(test.sub$s]
。我的解决方案是:
col=rainbow(length(test.sub$s))[rank(test.sub$s)[!duplicated(test.sub$s)]]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.