如何在R的ggbarstats中调整标签与样本量图的字体大小
解决ggbarstats调整样本量、百分比字体大小的方案
方法1:用内置参数控制标注文本
ggbarstats自带控制说明类文本(含样本量n)和副标题(含百分比汇总)的参数,直接设置字体大小即可:
library(ggstatsplot) ggbarstats( data = 你的数据集, x = 分类变量, # 调整样本量所在说明文字的字体大小 caption.args = list(size = 12), # 调整副标题中百分比文本的字体大小 subtitle.args = list(size = 10) )
方法2:覆盖柱子上的百分比标签样式
如果要调整柱子上方/内部的百分比数字大小,通过ggplot.component修改geom_text参数:
ggbarstats( data = 你的数据集, x = 分类变量, show.legend = FALSE ) + ggplot2::geom_text( ggplot2::aes(label = paste0(percent, "%")), size = 5, # 这里设置百分比字体大小 position = ggplot2::position_stack(vjust = 0.5) )
方法3:自定义主题统一调整文本
通过自定义ggplot主题,批量控制包括样本量、百分比在内的所有文本大小:
custom_theme <- ggplot2::theme_classic() + ggplot2::theme( plot.caption = ggplot2::element_text(size = 12), # 样本量n的位置 plot.subtitle = ggplot2::element_text(size = 10), # 百分比汇总文本 axis.text = ggplot2::element_text(size = 9) ) ggbarstats( data = 你的数据集, x = 分类变量, ggtheme = custom_theme )
内容的提问来源于stack exchange,提问作者li ranran
相关产品推荐
相关产品推荐

