简体   繁体   English

R plot:图例背景大小

[英]R plot: Legend background size

I am making a plot (scatter plot, specifically) using R, and noticed that the background size of the legend is unnecessarily big (as shown below):我正在使用 R 制作 plot(特别是分散 plot),并注意到图例的背景大小不必要地大(如下所示)

在此处输入图像描述

Is there any way that I can reduce the area?有什么办法可以缩小面积吗?

I tried to change cex , but it reduced not only the area but also the font size.我试图改变cex ,但它不仅减少了面积,还减少了字体大小。 I want to keep the font size, but reduce the legend area.我想保持字体大小,但减少图例区域。

Just in case, I attached the code of the legend that I have used:以防万一,我附上了我使用过的图例代码:

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")

[UPDATED] Here, the revised plot. [更新] 在这里,修改后的 plot。 I was able to modify the text width, but not the height (as shown below):我能够修改文本宽度,但不能修改高度(如下所示): 在此处输入图像描述

You can do the following:您可以执行以下操作:

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)

Part of the legend width is determined by the longest width of the labels you use, which is calculated via strwidth so by setting it to the text width you can reduce the box while keeping the font size.图例宽度的一部分由您使用的标签的最长宽度确定,该宽度是通过strwidth计算的,因此通过将其设置为文本宽度,您可以在保持字体大小的同时缩小框。

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

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