我正在尝试在单个图例中设置多种文本大小。
plot(1, 1)
legend("topleft",
legend=c("fruit", "apples", "oranges", "vegetables", "cucumber", "peppers"),
cex=0.8,
pch=c(19, NA, NA, 19, NA, NA),
col=c("red", "white", "white", "green", "white", "white"),
pt.cex=1)
我希望“苹果”、“橙子”、“ cucumber ”和“辣椒”尺寸较小。
我可以通过以下方式设置不同大小的点:
plot(1, 1)
legend("topleft",
legend=c("fruit", "apples", "oranges", "vegetables", "cucumber", "peppers"),
cex=0.8,
pch=c(19, NA, NA, 19, NA, NA),
col=c("red", "white", "white", "green", "white", "white"),
pt.cex=c(1, NA, NA, 0.8, NA, NA))
但是,如果我尝试以类似的方式设置文本大小,我会收到警告,并且它会以奇怪的方式创建图例两次。
plot(1, 1)
legend("topleft",
legend=c("fruit", "apples", "oranges", "vegetables", "cucumber", "peppers"),
cex=c(1, 0.8, 0.8, 1, 0.8, 0.8),
pch=c(19, NA, NA, 19, NA, NA),
col=c("red", "white", "white", "green", "white", "white"),
pt.cex=c(1, NA, NA, 0.8, NA, NA))
我很确定我的问题源于不理解 cex 想要什么样的输入图例。我还意识到我可能可以调用 legend()
两次,并使用 text()
插入我的文本,但这似乎可能既耗时又困惑。
最佳答案
看起来,如果您在图例调用中提供 cex
的多个值,它会为每个(唯一?)cex 值绘制一个图例。
正如 @MrFlick 所建议的,您可以使用不同的字体来创建层次结构。这是使用文本的解决方案。通过将图例调用的输出写入 a
,我们可以轻松地重复使用计算出的文本标签位置,并添加不同磅值的文本,只需额外一行:
plot(1, 1)
labs = c("fruit", "apples", "oranges", "vegetables", "cucumber", "peppers")
# add legend with white (invisible text) and store text positions in 'a'
a=legend("topleft",
legend=labs,
cex=1.0,
text.col='white',
pch=c(19),
col=c("red", "white", "white", "green", "white", "white"))
# draw text labels at calculated positions
text(a$text$x, a$text$y, lab=labs, cex=c(1, 0.8, 0.8, 1, 0.8, 0.8), pos=4, offset=c(0,0.1))
关于r - 在单个图例中设置多种文本大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24170834/