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

如何实现ggplot2分面图横向并排并消除柱状图间距?

问题解决:调整ggplot分面布局与柱状图间距

修改后的完整代码

# 加载数据(直接使用提供的结构化数据)
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("Tesla P100-SYCL", "Tesla V100-SYCL", "Tesla P100-CUDA", 
"Tesla V100-CUDA", "Tesla P100-SYCL", "Tesla V100-SYCL", "Tesla P100-CUDA", 
"Tesla V100-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))

# 加载ggplot2包
library(ggplot2)

# 绘制图表
myplot <- ggplot(data, aes(fill=condition, y=value, x=condition)) + 
    # 调整柱状图位置与宽度,消除间距
    geom_bar(position=position_dodge(width=1), stat="identity", width=1) +
    scale_fill_manual(values=c("#7b3294", "#c2a5cf", "#a6dba0", "#008837"))+
    ylab("Performance (ns/day)") +
    # 设置分面为横向并排
    facet_wrap(~specie, ncol=2, labeller = label_wrap_gen(width = 85), strip.position="bottom") +
    theme_bw() +
    theme(
        panel.grid = element_blank(),
        panel.spacing = unit(0, "mm"),
        legend.title=element_blank(),
        axis.title.x = element_blank(), 
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.title.y = element_text(angle = 90, hjust = 0.5),
        panel.background = element_blank()
    )

# 输出图表
myplot

核心修改说明

  • 分面横向排列:将facet_wrap参数从nrow=3改为ncol=2,让两个分面横向并排展示,保留strip.position="bottom"确保分面标签在底部显示。
  • 消除柱状图间隙:使用position_dodge(width=1)替代position="dodge",同时将geom_bar的width设为1,使同组的柱状图完全贴合,无间距。
  • 精简theme代码:合并重复的样式设置,避免冗余,保持代码简洁。

内容的提问来源于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 00:15:08