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

时间:2017-02-20 13:05:08

标签: r plot customization legend legend-properties

我正在尝试绘制基础R中的图例,水平符号和下一行符号下方的相应标签。图例将绘制在边距中(不包括在示例数据中)。有没有办法使用图形参数来解决这个问题与legend()函数?否则我会尝试文本标签,但我更喜欢更易于管理的方法。

我有这个示例数据:

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

想要创造这样的东西:

legend example

谢谢!

(编辑:在文字中添加图片和额外信息)

1 个答案:

答案 0 :(得分:0)

您可以分别绘制点和文字。

类似的东西:

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

对于绘图区域外的绘图(即在边距上),您可以将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)