You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R ggpubr/ggplot2绘图如何将单个因子水平Bacillus设为斜体

R ggplot2 单个分类标签斜体设置方案

不需要修改原有数据预处理的因子转换逻辑,仅需在绘图代码中新增x轴离散刻度的自定义图层,即可单独将Bacillus标签渲染为斜体,其余标签保持原有正体格式。

核心修改代码

在ggboxplot()图层后叠加x轴刻度配置:

scale_x_discrete(labels = c("Fung1", "Fung2", "Fung3", expression(italic("Bacillus"))))

该代码通过R原生表达式语法,单独对第四个分类标签应用斜体样式,不会改动其他绘图逻辑。

修正后完整可运行绘图代码

(原代码中ggboxplot()结尾缺失图层连接符+,已同步补全,否则后续图层无法正常叠加)

ggboxplot(IF_Eff_2, x = "Fung",
          y = "RST", 
          fill = "Fung",
          facet.by = "Year",
          ylab = "Sucrose yield (g/kg)",
          xlab = NULL) +
  # 新增:单独设置Bacillus为斜体
  scale_x_discrete(labels = c("Fung1", "Fung2", "Fung3", expression(italic("Bacillus")))) +
  theme_classic() + 
  theme(legend.position = "none",
        text=element_text(family="Calibri", size=30),
        axis.text.x = element_text(angle = 45, vjust = 0.5),
        axis.title.x = element_blank(),
        plot.title = element_text(hjust = 0.5)) +
  border() +
  geom_hline(yintercept = mean(IF_Eff_2$RST), linetype = 2) +
  theme(plot.background = element_rect(color="black",fill=NA,size=1.5)) +
  stat_summary(fun=mean, geom="point", shape=8, size=5, color="black", fill="black")

运行后x轴上的Bacillus标签就会显示为斜体,其余所有绘图元素、统计逻辑和原有代码的输出效果完全一致。

内容的提问来源于stack exchange,提问作者Austin Lien

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.01 21:30:55