簡體   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