将结果替换为图例标注的斜体

时间:2014-09-05 05:24:57

标签: r

我尝试为图表创建图例,显示组名称,然后显示斜体大小。我已经尝试过substitutebquote这样做,但没有成功:

# Some sample data
groupNames <- letters[1:5]
groupSizes <- seq(10, 50, length=5)

# Using bquote
legend.labels <-sapply(
  sapply(1:length(groupNames), function(i) {
    bquote(.(groupNames[i])~"("~italic(.(groupSizes[i]))~")")
  }), as.expression)
plot(1:5, colors=1:5)
legend("topleft", fill=1:5, legend=legend.labels)

# using substitute
legend.labels <-sapply(
  sapply(1:length(groupNames), function(i) {
    substitute(a~"("~italic(b)~")", list(a=groupNames[i], b=groupSizes[i]))
  }), as.expression)
plot(1:5, colors=1:5)
legend("topleft", fill=1:5, legend=legend.labels)

如何在绘图标签中斜体显示评估变量?

1 个答案:

答案 0 :(得分:2)

调整?legend帮助页面中的示例,也许您可​​以执行类似

的操作
plot(1:5, col=1:5)
temp <- legend("topleft", legend = groupNames,
               text.width = max(strwidth(paste(groupNames, groupSizes, " ()"))),
               fill=1:5, xjust = 0, yjust = 1)
text(temp$text$x +strwidth(groupNames) + strwidth(" "), temp$text$y,
    paste0("(", groupSizes, ")"), font=3, adj=c(0,.5))

enter image description here