繁体   English   中英

R 中水平图例中的不同线长

[英]Different line length in horizontal legend in R

我想创建一个带有多行(具有不同长度)和每行下方的文本的水平图例。 我有以下代码:

library(RColorBrewer)

colorlevel <- brewer.pal(n = 5, name = 'Set1')

plot.new()
a <- legend("topleft",
            legend =c("", "", "", "", "", ""),
            lty = 1,
            lwd = 4,
            bty='o',
            seg.len= c(rep(1.5,5), 4),
            col = colorlevel,
            horiz = TRUE,
            trace=TRUE
)
text(a$text$x-0.05, a$text$y, c("A", "B", "C", "D", "E", "F"), pos=1)

这会产生以下图例:

具有不同线长的水平图例

但是,有一些我无法解决的问题:

  1. 最后一行(最大的,对应于 F)必须保持与所有其他行相同的分隔,但它是完全分隔的。
  2. 虽然我希望彩色线条之间的距离相同,但有没有办法调整这种分离?
  3. 图例的边框离文字太近等,可以调整吗?

提前致谢。

试试这个解决方案:

library(RColorBrewer)

colorlevel <- brewer.pal(n = 5, name = 'Set1')

plot.new()
a <- legend("topleft",
            legend =c("", "", "", "", "", ""),
            lty = 1,
            lwd = 3,
            bty='o',
            seg.len= rep(1.5,5),
            col = colorlevel,
            horiz = TRUE,
            trace=TRUE,
            cex = 1.3
)
#you can experiment more with "cex" parameter in legend and text for more accurate output
text(a$text$x-0.06, a$text$y, c("A", "B", "C", "D", "E", "F"), pos=1)

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM