我尝试为图表创建图例,显示组名称,然后显示斜体大小。我已经尝试过substitute
和bquote
这样做,但没有成功:
# 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)
如何在绘图标签中斜体显示评估变量?
答案 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))