如何使图例中的标签在R中正确对齐? [英] How to make labels in the legend align right in R?

查看:493
本文介绍了如何使图例中的标签在R中正确对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如图所示,如何使这些标签正确对齐?

As this picture shows, how can I make these labels align right?

我在R中使用图例.

推荐答案

来自?legend

## right-justifying a set of labels: thanks to Uwe Ligges
x <- 1:5; y1 <- 1/x; y2 <- 2/x
plot(rep(x, 2), c(y1, y2), type = "n", xlab = "x", ylab = "y")
lines(x, y1); lines(x, y2, lty = 2)
temp <- legend("topright", legend = c(" ", " "),
           text.width = strwidth("1,000,000"),
           lty = 1:2, xjust = 1, yjust = 1,
           title = "Line Types")
text(temp$rect$left + temp$rect$w, temp$text$y,
     c("1,000", "1,000,000"), pos = 2)

我找不到自动将图例与右侧线条对齐的方法.但是您可以使用text位置和选项bty="n"删除边框来产生图例.

I can't find a way to automatically align the legend with the lines on the right. But you may be able to produce a legend using text placement and option bty="n" to remove the border.

## similar to above
x <- 1:5; y1 <- 1/x; y2 <- 2/x
plot(rep(x, 2), c(y1, y2), type = "n", xlab = "x", ylab = "y")
lines(x, y1); lines(x, y2, lty = 2)
temp <- legend("topright", legend = c(" ", " "),
           text.width = strwidth("1,000,000"),
           lty = 1:2, xjust = 1, yjust = 1, bty="n")

text(4, 2,"lbl1")
text(3.9, 1.92,"mylbl2")

这篇关于如何使图例中的标签在R中正确对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆