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

ggplot2堆叠分面网格:移除Y轴负号并添加XY轴标签

ggplot分面堆叠图需求优化方案

这是《How Do I Adjust Scale of Each Facet in Ggplot Faceting?》的跟进问题,当前堆叠分面网格中,绿色、橙色代表RMSE指标(对比MB与TMB方法),蓝色、红色代表MAE指标(对比同一组方法),已调整缩放确保所有网格清晰可见。

需求列表

  • 移除Y轴刻度的负号
  • 将X轴标签设置为phi,Y轴标签设置为Value
  • 保留原图例位置
  • 不使用facet_wrap函数以节省打印空间

修改后的完整代码

library(ggplot2)
library(reshape2)
set.seed(199)
MB_RMSE_sd1 <-  runif(12, min = 0, max = 2)
TMB_RMSE_sd1 <- runif(12, min = 0, max = 2)
MB_RMSE_sd3 <-  runif(12, min = 2, max = 5)
TMB_RMSE_sd3 <- runif(12, min = 2, max = 5)
MB_RMSE_sd5 <- runif(12, min = 5, max = 10)
TMB_RMSE_sd5 <- runif(12, min = 5, max = 10)
MB_RMSE_sd10 <-  runif(12, min = 7, max = 16)
TMB_RMSE_sd10 <- runif(12, min = 7, max = 16)
MB_MAE_sd1 <-  runif(12, min = 0, max = 2)
TMB_MAE_sd1 <- runif(12, min = 0, max = 2)
MB_MAE_sd3 <-  runif(12, min = 2, max = 5)
TMB_MAE_sd3 <- runif(12, min = 2, max = 5)
MB_MAE_sd5 <- runif(12, min = 5, max = 10)
TMB_MAE_sd5 <- runif(12, min = 5, max = 10)
MB_MAE_sd10 <-  runif(12, min = 7, max = 16)
TMB_MAE_sd10 <- runif(12, min = 7, max = 16)

ID <- rep(rep(c("N10_AR0.8", "N10_AR0.9", "N10_AR0.95", "N15_AR0.8", "N15_AR0.9", "N15_AR0.95", "N20_AR0.8", "N20_AR0.9", "N20_AR0.95", "N25_AR0.8", "N25_AR0.9", "N25_AR0.95"), 2), 1)
df1 <- data.frame(ID, MB_RMSE_sd1, TMB_MAE_sd1, MB_RMSE_sd3, TMB_MAE_sd3, MB_RMSE_sd5, TMB_MAE_sd5, MB_RMSE_sd10, TMB_MAE_sd10)
reshapp1 <- reshape2::melt(df1, id = "ID")

NEWDAT <- data.frame(value = reshapp1$value, year = reshapp1$ID, n = rep(rep(c("10", "15", "20", "25"), each = 3), 16), Colour = rep(rep(c("RMSE_MB", "RMSE_TMB", "MAE_MB", "MAE_TMB"), each = 12), 4), sd = rep(rep(c(1, 3, 5, 10), each = 48), 1),  phi = rep(rep(c("0.8", "0.9", "0.95"), 16), 4))

NEWDAT$sd <- with(NEWDAT, factor(sd, levels = sd, labels = paste("sd =", sd)))
NEWDAT$year <- factor(NEWDAT$year, levels = NEWDAT$year[1:12])
NEWDAT$n <- with(NEWDAT, factor(n, levels = n, labels = paste("n = ", n)))

ggplot() +
  geom_bar(data=NEWDAT[NEWDAT$Colour %in% c("RMSE_MB", "RMSE_TMB"),], 
           aes(x = phi, y=value, fill=rev(Colour)), stat="identity") +
  geom_bar(data=NEWDAT[NEWDAT$Colour %in% c("MAE_MB", "MAE_TMB"),], 
           aes(x=phi, y=-value, fill=Colour), stat="identity") +
  geom_hline(yintercept=0, colour="grey40") +
  facet_grid(sd ~ n, scales = "free") +
  scale_fill_manual(breaks=c("MAE_MB", "MAE_TMB", "RMSE_MB", "RMSE_TMB"),
                    values=c("red","blue","orange","green")) +
  scale_y_continuous(
    expand = c(0.0, 0.00),
    # 移除Y轴负号
    labels = function(x) abs(x)
  ) + 
  guides(fill=guide_legend(reverse=TRUE)) +
  # 设置轴标签
  labs(fill="", x = "phi", y = "Value") + 
  theme_bw() +
  theme(
    axis.text.x=element_text(angle=-90, vjust=0.5),
    # 保留原图例位置(默认右侧)
    legend.position = "right"
  )

修改说明

  1. 移除Y轴刻度负号:在scale_y_continuous中添加labels = function(x) abs(x),将所有Y轴刻度值转为绝对值,消除负号。
  2. 设置轴标签:通过labs()函数直接指定x = "phi"和y = "Value",替换默认轴标签。
  3. 保留原图例位置:明确指定legend.position = "right",与原图例位置保持一致。
  4. 维持分面布局:继续使用facet_grid(sd ~ n, scales = "free"),不替换为facet_wrap,满足节省打印空间的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 21:01:04