R: Legend color according to factor levels

女生的网名这么多〃 提交于 2019-12-24 08:13:22

问题


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?


回答1:


A solution I've found is:

data <- iris
# Create a translation table that couple species to color
colorcode = data.frame(
  cbind(colorsMy = c("red", "green", "blue"), species = levels(data$Species)),
  stringsAsFactors = F)
# Make vector with colors for the different points in the scatter
iriscolors = sapply(data$Species,  # Species to colors translation acc to colorcode
                    function(x) colorcode$colorsMy[colorcode$species == x])
# Plot the scatter using the color vector constructed according the colorcode
plot(data$Sepal.Length, data$Sepal.Width, col = iriscolors, pch = 19)
# Since iriscolors according to colorcode, I can use colorcode for the legend
legend("bottomright", legend = colorcode$species, fill = colorcode$colorsMy)

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.



来源:https://stackoverflow.com/questions/55186549/r-legend-color-according-to-factor-levels

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!