Inspired by this question where apparently the top answer is using an unsafe/erroneous way to add colors to a legend for a scatter plot.
Top answer suggests doing this:
data<-iris
plot(data$Sepal.Length, data$Sepal.Width, col=data$Species)
legend(7,4.3,unique(data$Species),col=1:length(data$Species),pch=1)
Comments suggest using levels()
instead of unique()
for controlling the text and colors in the call to legend()
, but are unclear on why it would help. I would need a better explanation to trust that code.
How can I write code that guarantees proper coloring?
A solution I've found is:
This code is a bit bulky, but easy to follow and explicitly constructs correct color labeling in the legend. The "trick" is to create the
colorcode
variable that serves as a translation table between levels of the factor (iris species in this case) and colors for the legend.