r - 在单个图例中设置多种文本大小

标签 r plot legend

我正在尝试在单个图例中设置多种文本大小。

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)

legendallsamesize

我希望“苹果”、“橙子”、“ 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))

badlegend

我很确定我的问题源于不理解 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/

相关文章:

在 R 中使用 mongolite 的正则表达式

r - R 中的 car::scatter3d - 更好地标记轴

android - android中的图例和字段集

MATLAB:为图例中的文本分配多种颜色

python - 图例中的分割标记和线条 - Matplotlib

r - R中同一张图上的频率和累积频率曲线

r - 在 R 中执行 2D 快速傅立叶变换

python - 将具有两列的 pandas DataFrame 转换为堆叠条形图

r - 在 R 图中结合 Unicode 进行排版

matlab - 在 matlab 中像 matplotlib imshow 一样在 imagesc\imshow 的像素之间进行平滑处理