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

ggplot分面绘图需求:每个分面显示X轴且保持Y轴间距均匀

学术论文分面图制作问题:每个分面显示相同X轴+保持Y轴间距一致

需求:制作用于学术论文的分面图,要求每个分面显示相同的X轴,同时各分面的Y轴指标间距保持一致。

已尝试方案及问题:

  • facet_grid:可实现理想的Y轴间距,但无法为每个分面显示X轴
  • facet_wrap:能为每个分面显示X轴,但会破坏Y轴间距,且各分面X轴刻度不一致
  • lemon包的facet_rep_grid:运行报错,错误提示为Error in axisgrob$height$arg1 : $ operator is invalid for atomic vectors
  • patchwork:同样会破坏所需的Y轴间距

可复现代码

install.packages("lemon")
library(lemon)
data("mtcars")

df <- mtcars %>%
  rownames_to_column(var = "car")

mt <- ggplot(df, aes(mpg, car, colour = factor(cyl))) +
  theme(strip.placement = "outside")+
  geom_point()

# 理想的Y轴间距,但无法为每个分面显示X轴
mt + 
  facet_grid(vars(cyl), switch = "y", scales = "free", space = "free") 

# Y轴间距错误,每个分面显示X轴但刻度不同
mt + 
  facet_wrap(~cyl,  scales = "free", ncol=1, switch = "y") 

# 运行报错
mt +
  facet_rep_grid(vars(cyl), scales = "free"
                 , switch = "both", space = "free")

效果图

facet_grid版本

facet_grid效果图

facet_wrap版本

facet_wrap效果图


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 00:10:24