【问题标题】:How to use colors of my choice for plotting by groups in R?如何使用我选择的颜色在 R 中按组绘制?
【发布时间】:2015-07-11 01:22:08
【问题描述】:

我有一个列(名为组)作为分组信息(1 到 4 组,4 个组),我希望用它来为图中的组分配不同的颜色

x=data.frame(read.csv(file.choose()))
plot(x$A,x$B,col=as.factor(x$group))

这为分别标记为 1、2、3 ... 的组提供了黑色、红色、绿色等颜色。如果我想根据自己的需要分配颜色,我该如何修改代码。假设我希望标记为 1 的组以蓝色显示(例如)

【问题讨论】:

    标签: r plot colors


    【解决方案1】:

    像这样:

    plot(Sepal.Length ~ Sepal.Width, data = iris, 
         col = c("blue", "red", "green")[as.factor(Species)])
    legend("topright", pch = 1, col = c("blue", "red", "green"),
           legend = levels(as.factor(iris$Species)))
    

    【讨论】:

      猜你喜欢
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多