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

标签 r

我正在尝试创建一个条形图,其中每个条形都有一个 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/

相关文章:

r - Kmeans 以 group_by 为中心并汇总管道

r - 尽管使用 r 中的 as.POSIXct 跨越午夜,日期并没有改变

r - 使用 Roxygen2 模板标签

r - override.aes 与列表参数的命名向量

r - 如何在ggplot2中使用纬度和箭头绘制风向

旋转 Markdown 的表格 pdf 输出

r - 如何在 R 中使用 `tryCatch` 正确记录警告和错误?

r - 在 rmarkdown::html_document() .Rmd 中使用 highlight.js 主题

r - 按条件从长格式数据集中提取特定行/采用长格式数据进行生存分析

r - 如何检查树是分类树还是回归树