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

ggplot多分组条形图分面标题显示异常与分割线添加问题

解决ggplot2多分组条形图的两个问题

问题1:分面标题无法适配灰色标题框

原因是Type列文本过长超出标题框宽度,通过以下方式解决:

  • 用stringr::str_wrap()将长文本按指定宽度自动折行
  • 调整分面标题字体大小,优化显示适配性

问题2:添加左右分面组的垂直分割线

使用patchwork包将图按列拆分后拼接,中间用窄空白区块作为垂直分割线,实现精确布局控制。


完整修改代码

library(reshape2)
library(ggplot2)
library(stringr)
library(patchwork)

# 加载数据集
df <- structure(list(ID = 1:8, Type = c("gmx mdrun -ntmpi 8 -ntomp 1 -s benchPEP.tpr -nsteps 10000 -resethway",
"gmx mdrun -ntmpi 8 -ntomp 1 -s benchPEP.tpr -nsteps 10000 -resethway",
"gmx mdrun -ntmpi 8 -s benchPEP.tpr -nsteps 4000 -resetstep 3000",
"gmx mdrun -ntmpi 8 -s benchPEP.tpr -nsteps 4000 -resetstep 3000",
"gmx mdrun -ntmpi 8 -s benchPEP.tpr -nsteps -1 -maxh 1.0 -resethway",
"gmx mdrun -ntmpi 8 -s benchPEP.tpr -nsteps -1 -maxh 1.0 -resethway",
"gmx mdrun -ntmpi 8 -ntomp 1 -s benchPEP.tpr -nsteps -1 -maxh 1.0 -resethway -noconfout",
"gmx mdrun -ntmpi 8 -ntomp 1 -s benchPEP.tpr -nsteps -1 -maxh 1.0 -resethway -noconfout"
), Annee = c("SYCL", "CUDA", "SYCL", "CUDA", "SYCL", "CUDA",
"SYCL", "CUDA"), Domain.decomp. = c("2. 1", "2", "2. 1", "2. 1",
"2.1", "2", "2. 1", "2"), DD.com..load = c(0, 0, 0, 0, 3.7, 3,
0, 0), Neighbor.search = c("3.7", "3. 1", "3.7", "3.9", "0. 1",
"O. 1", "3.5", "3. 1"), Launch.PP.GPU.ops. = c("0. 1", "0", "0.2",
"0", "1 .6", "1 . 5", "0.2", "0. 1"), Comm..coord. = c("1 .6",
"1 .0", "1 .5", "1 .3", "1 .5", "1 .3", "1 . 5", "1 .6"), Force = c("1 . 5",
"1 .2", "1 .4", "1 .2", "1 .3", "1 . 1", "1 .5", "1 .2"), Wait...Comm..F = c("1 .3",
"1 .7", "1 .2", "1 .0", "66.7", "68.8", "1 .2", "1 .2"), PIE.mesh = c("65.6",
"70.9", "61 .0", "61 .4", "0", "0", "67.6", "69.2"), Wait.Bonded.GPU = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L), wait.GPU.NB.nonloc. = c(0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L), Wait.GPU.NB.local = c(0, 0, 0, 0, 7.4,
5.7, 0, 0), NB.X.F.buffer.ops. = c("7.3", "4.4", "6. 7", "5",
"0. 1", "0. 1", "7.2", "5.5"), Write.traje = c("0.3", "0.3",
"1 .2", "1 .3", "6.4", "6. 1", "O. 1", "0. 1"), Update = c(6.3,
4.3, 5.7, 4.9, 8.2, 9.5, 6.2, 5.6), Constraints = c("8.9", "9.7",
"1 1 .6", "13.3", "0.3", "0.4", "8. 1", "9.5"), Comm..energies = c("0.9",
"0.9", "3.3", "3.9", "8.4", "8. 5", "0.3", "0.4"), PIE.redist..X.F = c("8. 1",
"8.7", "7.9", "7.4", "29.9", "30.1", "8. 1", "8. 1"), PIE.spread = c("29.7",
"30.6", "27.2", "29.6", "20.3", "20.2", "30. 1", "30.4"), PIE.gather = c("19.9",
"21 .3", "18.7", "19", "6.4", "8.4", "20", "20.6"), PIE.3D.FFT = c("6",
"8.6", "5.7", "4.3", "1 .0", "1 .1", "7.6", "8.4"), PIE.3D.FFT.comm. = c("1 .2",
"1 .0", "0.9", "0.7", "1 .2", "0. 5", "1 .0", "1 .1"), PIE.solve.Elec = c(0.7,
0.5, 0.6, 0.3, 0.7, 0.5, 0.7, 0.5)), class = "data.frame", row.names = c(NA,
-8L))

# 处理Type列文本,自动换行
df$Type <- str_wrap(df$Type, width = 40)

# 转换为长格式
df.long <- melt(df, id.vars=c("ID","Type","Annee"))
df.long$value <- gsub(" ", "", df.long$value)
df.long$value <- sub("O", "0", df.long$value)
df.long$value <- as.numeric(df.long$value)

# 拆分左右分面组
left_types <- unique(df$Type)[1:2]
right_types <- unique(df$Type)[3:4]

# 绘制左侧子图
p_left <- ggplot(df.long[df.long$Type %in% left_types,], aes(variable, value, fill=as.factor(Annee)))+
  geom_bar(position="dodge", stat="identity")+
  ylab("Simulation Progress (%)") +
  facet_wrap(~Type, nrow=2) +
  theme(panel.grid.major = element_blank(), 
        legend.title=element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        axis.title.x = element_blank(), 
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        axis.line = element_line(colour = "black"),
        strip.text = element_text(size = 9))

# 绘制右侧子图(隐藏重复的y轴元素)
p_right <- ggplot(df.long[df.long$Type %in% right_types,], aes(variable, value, fill=as.factor(Annee)))+
  geom_bar(position="dodge", stat="identity")+
  ylab("") +
  facet_wrap(~Type, nrow=2) +
  theme(panel.grid.major = element_blank(), 
        legend.title=element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        axis.title.x = element_blank(), 
        axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        axis.line = element_line(colour = "black"),
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        strip.text = element_text(size = 9))

# 拼接图并添加垂直分割线
p_left + plot_spacer(width = 0.01) + p_right +
  plot_layout(ncol=3, widths = c(1, 0.01, 1)) +
  theme(plot.background = element_rect(colour = "black", size=0.5))

内容的提问来源于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 16:07:03