我使用以下 R 命令为我的图创建图例:
legend("topright",
legend = c("", "T = 500", "T = 1000", "T = 2000",
expression(lambda[1]), format(eigenvalues[1, 1], digits = 3),
format(eigenvalues[2, 1], digits = 3), format(eigenvalues[3, 1], digits = 3),
expression(lambda[2]), format(eigenvalues[1, 2], digits = 3),
format(eigenvalues[2, 2], digits = 3), format(eigenvalues[3, 2], digits = 3)),
lty = c(0, 1, 1, 1, rep(0, 8)), # Segments only for T rows
col = c("", "blue", "red", "gray30", rep("", 8)),
seg.len = c(0, 1.5, 1.5, 1.5, rep(0, 8)), # 0 seg.len for lambdas
ncol = 3, bty = "o", title = "")
现在您可以看到问题是列之间的空间太大,这导致图例有时会覆盖绘图。如何减少列间距?
您可以使用 legend() 的 text.width 参数在图例中传递列宽向量。为每一列的 text.width 指定一个值。我制作了一个简单的图并添加了您的图例,该图例有三列。比较下面的两个图。第一个使用默认的 text.width,第二个使用我传入的值。宽度以 x 轴为单位。
eigenvalues <- matrix(c(1:6), nrow = 3)
plot(1:3, 1:3)
legend("topright" ,
legend = c("", "T = 500", "T = 1000", "T = 2000",
expression(lambda[1]), format(eigenvalues[1, 1], digits = 3),
format(eigenvalues[2, 1], digits = 3), format(eigenvalues[3, 1], digits = 3),
expression(lambda[2]), format(eigenvalues[1, 2], digits = 3),
format(eigenvalues[2, 2], digits = 3), format(eigenvalues[3, 2], digits = 3)),
lty = c(0, 1, 1, 1, rep(0, 8)), # Segments only for T rows
col = c("", "blue", "red", "gray30", rep("", 8)),
seg.len = c(0, 1.5, 1.5, 1.5, rep(0, 8)), # 0 seg.len for lambdas
ncol = 3, bty = "o", title = "")
Xs <- c(0.25,0.1,0.1) #vector of column widths in the legend
plot(1:3, 1:3)
legend("topright" ,
legend = c("", "T = 500", "T = 1000", "T = 2000",
expression(lambda[1]), format(eigenvalues[1, 1], digits = 3),
format(eigenvalues[2, 1], digits = 3), format(eigenvalues[3, 1], digits = 3),
expression(lambda[2]), format(eigenvalues[1, 2], digits = 3),
format(eigenvalues[2, 2], digits = 3), format(eigenvalues[3, 2], digits = 3)),
lty = c(0, 1, 1, 1, rep(0, 8)), # Segments only for T rows
col = c("", "blue", "red", "gray30", rep("", 8)),
seg.len = c(0, 1.5, 1.5, 1.5, rep(0, 8)), # 0 seg.len for lambdas
ncol = 3, bty = "o", title = "",
text.width = Xs)
创建于 2024-11-09,使用 reprex v2.1.1