2014-06-11 27 views
2

我試圖在單個圖例中設置多個文本大小。在單個圖例中設置多個文本大小

plot(1, 1) 
legend("topleft", 
     legend=c("fruit", "apples", "oranges", "vegetables", "cucumber", "peppers"), 
     cex=0.8, 
     pch=c(19, NA, NA, 19, NA, NA), 
     col=c("red", "white", "white", "green", "white", "white"), 
     pt.cex=1) 

legendallsamesize

我喜歡 「蘋果」, 「桔子」, 「黃瓜」 和 「小辣椒」 是一個更小的尺寸。

我可以通過以下方式設置點的大小不同:

plot(1, 1) 
legend("topleft", 
     legend=c("fruit", "apples", "oranges", "vegetables", "cucumber", "peppers"), 
     cex=0.8, 
     pch=c(19, NA, NA, 19, NA, NA), 
     col=c("red", "white", "white", "green", "white", "white"), 
     pt.cex=c(1, NA, NA, 0.8, NA, NA)) 

但是,如果我嘗試設置文字大小以類似的方式,我得到一個警告,並在兩次創造傳奇奇怪的方式。

plot(1, 1) 
legend("topleft", 
     legend=c("fruit", "apples", "oranges", "vegetables", "cucumber", "peppers"), 
     cex=c(1, 0.8, 0.8, 1, 0.8, 0.8), 
     pch=c(19, NA, NA, 19, NA, NA), 
     col=c("red", "white", "white", "green", "white", "white"), 
     pt.cex=c(1, NA, NA, 0.8, NA, NA)) 

badlegend

我敢肯定從不理解從CEX想要什麼樣的輸入傳說我的問題造成的。我也意識到我可能會撥打legend()兩次,並使用text()來插入我的文本,但這似乎很費時間和雜亂。

+1

由於'legend'是不好受用'cex',一個選項可能是改變字體重量:'text.font = c(2,3,3,2,3,3)'。 – MrFlick

回答

3

看起來,如果您在圖例調用中提供cex的多個值,它會爲每個(唯一的?)cex值繪製一個圖例。

正如@MrFlick所示,您可以使用不同的字體來創建層次結構。這是一個使用文本的解決方案。通過寫傳奇呼叫的輸出a,我們可以輕鬆地重新使用計算的位置的文本標籤和不同pointsizes只有1額外的行添加文字:

plot(1, 1) 

labs = c("fruit", "apples", "oranges", "vegetables", "cucumber", "peppers") 

# add legend with white (invisible text) and store text positions in 'a' 
a=legend("topleft", 
     legend=labs, 
     cex=1.0, 
     text.col='white', 
     pch=c(19), 
     col=c("red", "white", "white", "green", "white", "white")) 

# draw text labels at calculated positions 
text(a$text$x, a$text$y, lab=labs, cex=c(1, 0.8, 0.8, 1, 0.8, 0.8), pos=4, offset=c(0,0.1)) 
+0

+1學習'legend()'(比如'hist()','barplot()'等)具有如此有用的返回值。 –

+0

同意,@JoshO'Brien!這個解決方案正是我想要的。謝謝! – fredtal

+0

''legend''中的'cex'參數引用'title.cex'和'pt.cex'後者可以在文檔中找到,但title.cex似乎不是傳說的有效參數。此外,圖例源代碼中的cex似乎在處理圖例標題的代碼中被回收。這是缺少title.cex rcore錯誤還是我錯過了什麼? – rawr

相關問題