I'm making plots like the one generated with the following code:

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")

Is there a way, with just the R graphics package, to place a black circle before Var 1 and a grey circle before Var 2 in the y axis label, to avoid having to insert a legend? Or alternatively, a way to use different text colours (black for Var 1 and grey for Var 2) in the y axis? I tried using col.lab = c("black","grey"), but it says Error in plot.window(...) : graphical parameter "col.lab" has the wrong length.

Many thanks in advance,

Márcia

有帮助吗?

解决方案

I'm not sure how to add the point to the label, but an easy way to labe with color can be done in the following way:

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")

enter image description here

其他提示

Would something like this work for you? It's a bit busy on the left axis, but I think it shows what you are asking about.

> 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()

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top