gpt4 book ai didi

r - 如何在 R 中的条形图的 names.arg 中有多行和不同的字体大小?

转载 作者:行者123 更新时间:2023-12-04 14:24:33 24 4
gpt4 key购买 nike

我正在尝试创建一个条形图,其中每个条形都有一个 x 轴标签“Name [linebreak] N=123”,其中带有“N=123”的第二行以较小的字体显示。

考虑这个例子:

t <- table(
factor(c(1, 1, 1, 1, 1, 1, 2, 1, 2, 2)),
factor(c(1, 1, 3, 2, 2, 2, 2, 2, 3, 1))
)
counts <- colSums(t) # 3, 5, 2
colnames(t) <- c("A", "B", "C")
barplot(t)

该图看起来像这样(裁剪):

Result

我想将变量 counts 中每个级别 A、B、C 的计数添加到标签(以较小的字体),使其看起来像这样:

Desired result

有什么方法可以在 R 中实现吗?

最佳答案

barplot 返回“所有条形中点的坐标”(来自 ?barplot),您可以使用它向图中添加文本。

b <- barplot(t)

mtext(paste("N = ", counts), side=1, line=2, at=b)

# Use `mtext` to easily write to plot margins
# side=1 : bottom
# line=2 : counts out the way - so (I think) one is at the axis labels, so one more
# at: use the positions calculated by barplot

关于r - 如何在 R 中的条形图的 names.arg 中有多行和不同的字体大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48649068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com