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

如何将ggplot2中Y轴标签调整至中间并放大字体

解决方案:调整ggplot2 Y轴标题位置与字体大小

关键修改点

  • 将Y轴标题移至轴中间:在axis.title.y的element_text()中同时设置vjust=0.5和hjust=0.5,确保垂直旋转的标题沿Y轴居中
  • 放大标题字体:添加size参数,设置合适的数值(示例用size=12,可按需调整)

修改后的完整代码

# 加载必要包(确保已安装ggplot2、viridis、hrbrthemes)
library(ggplot2)
library(viridis)
library(hrbrthemes)

# 使用提供的数据集
data <- structure(list(specie = c("gmx mdrun -gpu_id 1 -ntomp 16 -s benchMEM.tpr -nsteps 10000", 
"gmx mdrun -gpu_id 1 -ntomp 16 -s benchMEM.tpr -nsteps 10000", 
"gmx mdrun -gpu_id 1 -ntomp 16 -s benchMEM.tpr -nsteps 10000", 
"gmx mdrun -gpu_id 1 -ntomp 16 -s benchMEM.tpr -nsteps 10000", 
"gmx mdrun -gpu_id 1 -ntomp 16 -s MD_15NM_WATER.tpr -nsteps 10000", 
"gmx mdrun -gpu_id 1 -ntomp 16 -s MD_15NM_WATER.tpr -nsteps 10000", 
"gmx mdrun -gpu_id 1 -ntomp 16 -s MD_15NM_WATER.tpr -nsteps 10000", 
"gmx mdrun -gpu_id 1 -ntomp 16 -s MD_15NM_WATER.tpr -nsteps 10000"
), condition = c("Extreme-SYCL", "Saber-SYCL", "Extreme-CUDA", 
"Saber-CUDA", "Extreme-SYCL", "Saber-SYCL", "Extreme-CUDA", "Saber-CUDA"
), value = c(75.8, 77.771, 63.297, 78.046, 34.666, 50.052, 32.07, 
59.815)), class = "data.frame", row.names = c(NA, -8L))

# 绘制图表
myplot <- ggplot(data, aes(fill=condition, y=value, x=condition)) + 
    geom_bar(position="dodge", stat="identity") +
    scale_fill_viridis(discrete = T, option = "E") +
    ylab("Performance (ns/day)") +
    facet_wrap(~specie) +
    theme_ipsum()

# 调整主题(关键修改部分)
myplot + theme(
    panel.grid.major = element_blank(), 
    panel.grid.minor = element_blank(), 
    legend.title=element_blank(), 
    panel.background = element_blank(), 
    axis.title.x = element_blank(), 
    axis.text.x=element_blank(), 
    axis.ticks.x=element_blank(), 
    axis.title.y = element_text(
        angle=90, 
        vjust = 0.5, 
        hjust = 0.5, 
        size = 12  # 放大字体,可根据需求调整数值
    )
)

说明

  • vjust=0.5 和 hjust=0.5 共同作用,确保Y轴标题在垂直方向上居中于Y轴
  • size 参数控制标题字体大小,数值越大字体越大,可根据图表尺寸灵活调整
  • theme_ipsum() 的默认样式可能会影响轴标题位置,明确指定这两个参数可以覆盖默认设置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 08:27:46