如何在R中连续调整图例?

Man*_*ish 5 plot r legend

我有一个情节,我需要连续调整图例。我该怎么做?

plot(x,y)
legend(c("x","y"))
Run Code Online (Sandbox Code Playgroud)

我需要图例应该在一行

    ----- x                 --------- y 
Run Code Online (Sandbox Code Playgroud)

问候

Tho*_*mas 4

你想设置horiz=TRUElegend. horiz=FALSE以下是默认行为 ( ) 与 的比较horiz=TRUE

在此输入图像描述

该图基于文档中的第二个示例legend

layout(matrix(1:2,nrow=1))
# `horiz=FALSE` (default behavior)
plot(x, sin(x), type = "l", ylim = c(-1.2, 1.8), col = 3, lty = 2, main="horiz=FALSE (Default)")
points(x, cos(x), pch = 3, col = 4)
lines(x, tan(x), type = "b", lty = 1, pch = 4, col = 6)
legend(-1, 1.9, c("sin", "cos", "tan"), col = c(3, 4, 6),
       text.col = "green4", lty = c(2, -1, 1), pch = c(NA, 3, 4),
       merge = TRUE, bg = "gray90")
# `horiz=TRUE`
plot(x, sin(x), type = "l", ylim = c(-1.2, 1.8), col = 3, lty = 2, main="horiz=TRUE")
points(x, cos(x), pch = 3, col = 4)
lines(x, tan(x), type = "b", lty = 1, pch = 4, col = 6)
legend(-1, 1.9, c("sin", "cos", "tan"), col = c(3, 4, 6),
       text.col = "green4", lty = c(2, -1, 1), pch = c(NA, 3, 4),
       merge = TRUE, bg = "gray90", horiz=TRUE)
Run Code Online (Sandbox Code Playgroud)