【问题标题】:plot axis labels with multiple colours绘制具有多种颜色的轴标签
【发布时间】:2014-04-02 16:40:15
【问题描述】:

我正在制作类似于使用以下代码生成的图:

var1 <- sort(runif(10, 0, 1), decreasing = TRUE)
var2 <- sort(runif(10, 0, 1))
plot(var1, pch = 20, ylab = c("Var 1", "Var 2"))
points(var2, pch = 20, col = "grey")

有没有办法,仅使用 R 图形包,在 y 轴标签中在 Var 1 之前放置一个黑色圆圈,在 Var 2 之前放置一个灰色圆圈,以避免插入图例?或者,一种在 y 轴上使用不同文本颜色(Var 1 为黑色,Var 2 为灰色)的方法?我尝试使用col.lab = c("black","grey"),但它显示Error in plot.window(...) : graphical parameter "col.lab" has the wrong length。

非常感谢,

玛西亚

【问题讨论】:

    标签: r graphics plot axis-labels


    【解决方案1】:

    我不确定如何将点添加到标签,但可以通过以下方式轻松地使用颜色进行标记:

    var1 <- sort(runif(10, 0, 1), decreasing = TRUE)
    var2 <- sort(runif(10, 0, 1))
    plot(var1, pch = 20, ylab = "")
    points(var2, pch = 20, col = "grey")
    mtext("Var 1", side=2, line=2)
    mtext("Var 2", side=2, line=3, col="grey")
    

    【讨论】:

      【解决方案2】:

      这样的东西对你有用吗?左轴有点忙,但我认为它显示了您的问题。

      > var1 <- sort(runif(10, 0, 1), decreasing = TRUE)
      > var2 <- sort(runif(10, 0, 1))
      > plot(var1, ylim = range(c(var1, var2)), pch = 20, ylab = "", axes = FALSE)
      > points(var2, pch = 20, col = "grey")
      > labs <- round(sort(c(var1, var2)), 1)
      > axis(1)
      > axis(2, at = sort(c(var1, var2)), labels = labs)
      > sapply(var1, function(x) points(-0.1, x, pch = 20))
      > sapply(var2, function(x) points(-0.1, x, pch = 20, col = "grey"))
      > box()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-06
        • 1970-01-01
        • 2021-12-24
        • 1970-01-01
        • 1970-01-01
        • 2018-05-05
        相关资源
        最近更新 更多