简体   繁体   English

如何在R中的不同行上绘制图例符号和标签?

[英]How can I plot legend symbols and labels on a different row in R?

I'm trying to plot a legend in base R with the symbols horizontally and the corresponding labels underneath the symbols on the next row. 我正在尝试在基数R中绘制图例,水平放置符号,并在下一行的符号下方放置相应的标签。 The legend will be plotted in the margins (not included in example data). 图例将在页边距中绘制(示例数据中未包括)。 Is there a way to use graphical parameters to solve this with the legend() function? 有没有一种方法可以使用图形参数通过legend()函数解决此问题? Otherwise I will try the text labels, but I prefer a more manageable approach. 否则,我将尝试使用文本标签,但我更喜欢一种更易于管理的方法。

I have this example data: 我有以下示例数据:

plot(c(1,2,3,4,5), c(1,2,3,4,5), xlim=c(0,5), ylim=c(0,5),  main = "", xlab = "", ylab = "")

legendEntries <- c(0.05, 0.1, 0.15, 0.2, 0.25) # which values in legend
legendSizes   <- sqrt( legendEntries / pi ) * 10 # calculate pch size 
legend(1, 2, title="", horiz = T,  legend=legendEntries, col="black", pch=rep(21,5), 
   pt.bg = "#ff166c", pt.cex = legendSizes, bty = "n")

And want to create something like this: 并想要创建这样的东西:

图例

Thanks! 谢谢!

Paul 保罗

(edit: added picture in text and extra info) (编辑:以文字和其他信息添加图片)

You can plot separately points and text. 您可以分别绘制点和文本。

Something like: 就像是:

# Make the basic plot
    plot(c(1,2,3,4,5), c(1,2,3,4,5), xlim=c(0,5), ylim=c(0,5),  main = "", xlab = "", ylab = "")
    # set up the legend entries and sizes
    legendEntries <- c(0.05, 0.1, 0.15, 0.2, 0.25) # which values in legend
    legendSizes   <- sqrt( legendEntries / pi ) * 10 # calculate pch size 

# plot the legend points
    points(y = rep(1, 5), x = seq(3,4, 0.25), pch = 21, cex = sqrt( legendEntries / pi ) * 10,
           bg = "#ff166c")
# plot the text
    text(y = rep(0.7, 5), x = seq(3,4, 0.25),
         labels = legendEntries)

For Plotting outside of the plot region (ie on the margins), you can use the xpd parameter as xpd = TRUE : 对于在绘图区域之外(即在边距上)进行绘图,可以将xpd参数用作xpd = TRUE

plot(c(1,2,3,4,5), c(1,2,3,4,5), xlim=c(0,5), ylim=c(0,5),  main = "", xlab = "", ylab = "")

legendEntries <- c(0.05, 0.1, 0.15, 0.2, 0.25) # which values in legend
legendSizes   <- sqrt( legendEntries / pi ) * 10 # calculate pch size 

points(y = rep(-0.8, 5), x = seq(1,2, 0.25), pch = 21, cex = sqrt( legendEntries / pi ) * 10,
       bg = "#ff166c", xpd = TRUE)
text(y = rep(-1, 5), x = seq(1,2, 0.25),
     labels = legendEntries, xpd = TRUE)

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

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