[英]R plot: Legend background size
我正在使用 R 制作 plot(特別是分散 plot),並注意到圖例的背景大小不必要地大(如下所示)
有什么辦法可以縮小面積嗎?
我試圖改變cex
,但它不僅減少了面積,還減少了字體大小。 我想保持字體大小,但減少圖例區域。
以防萬一,我附上了我使用過的圖例代碼:
legend(x="bottomleft", legend = c("Survived (0)", "Died (1)"),
col=c(1:length(levels(Noadr.for.plot2$Death))), pch = c(19, 17),
y.intersp = 0.4,
bg = "gray")
您可以執行以下操作:
attach(mtcars)
plot(wt, mpg, main="Scatterplot Example",
xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)
# op <- par(cex = 1)
text = c("Survived (0)", "Died (1)")
cex_val = 1
legend(x="bottomleft", legend = text,
pch = c(19, 17), cex=cex_val,
y.intersp = 1,
bg = "gray",
text.width = strwidth(text)[1]*cex_val)
圖例寬度的一部分由您使用的標簽的最長寬度確定,該寬度是通過strwidth
計算的,因此通過將其設置為文本寬度,您可以在保持字體大小的同時縮小框。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.