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

移除网格线并扩展绘图后ggplot2仍存在左侧空白问题求助

解决ggplot2热力图x轴左侧空白问题

尝试多种方法后,仍无法让ggplot2绘制的tile网格填满整个x轴,x轴左侧始终存在空白区域(示意图见下方),以下是解决方法:

可复现数据

df=structure(list(pid = structure(10:1, levels = c("10", "9", "8", 
"7", "6", "5", "4", "3", "2", "1"), class = "factor"), type = c("drug", 
"drug", "drug", "drug", "drug", "drug", "drug", "drug", "drug", 
"drug"), value = c("SD", "PD", "PD", "SD", "PR", "PD", "PR", 
"PR", "CR", "CR")), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

原代码

ggplot(df, aes(x = type, y = as.factor(pid), fill = value)) +
  geom_tile(color = "white") +
  theme_minimal() +
  theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_blank(),
        panel.grid = element_blank(),
        panel.border = element_blank()
  ) +
  ylab("") + 
  xlab("") + 
  theme( legend.key = element_blank(), legend.position = "none",
         axis.text.y = element_blank(),
         axis.text.x = element_text(angle = 45, size= 11 , hjust=1 ),
         axis.title.x = element_text(size=12),
         axis.title.y     = element_text(size=15), 
         legend.text      =element_text(size=12),
         plot.title = element_text(size = 15, face = "bold"),
         plot.margin = unit(c(0, 0, 0, 0), "pt"),
         axis.ticks.y = element_blank(),
         axis.line.y = element_blank()
  ) +
  scale_x_discrete(expand = c(0, 0) ) +  # Remove x-axis padding
  scale_y_discrete(expand = c(0, 0))  # Remove y-axis padding

问题示意图

左侧空白示意图

解决方案

问题根源是geom_tile的默认宽度(0.9):当x轴只有单个离散类别时,这个宽度会让tile无法填满x轴的全部范围,从而出现空白。只需给geom_tile添加width = 1参数,配合已有的scale_x_discrete(expand = c(0, 0))就能消除空白。

修改后的完整代码:

ggplot(df, aes(x = type, y = as.factor(pid), fill = value)) +
  geom_tile(color = "white", width = 1) +  # 设置width=1
  theme_minimal() +
  theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_blank(),
        panel.grid = element_blank(),
        panel.border = element_blank()
  ) +
  ylab("") + 
  xlab("") + 
  theme( legend.key = element_blank(), legend.position = "none",
         axis.text.y = element_blank(),
         axis.text.x = element_text(angle = 45, size= 11 , hjust=1 ),
         axis.title.x = element_text(size=12),
         axis.title.y     = element_text(size=15), 
         legend.text      =element_text(size=12),
         plot.title = element_text(size = 15, face = "bold"),
         plot.margin = unit(c(0, 0, 0, 0), "pt"),
         axis.ticks.y = element_blank(),
         axis.line.y = element_blank()
  ) +
  scale_x_discrete(expand = c(0, 0) ) +
  scale_y_discrete(expand = c(0, 0))

也可以用coord_cartesian(expand = FALSE)替代scale_x_discrete(expand = c(0, 0)),效果相同,但核心是设置geom_tile(width = 1)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 17:34:51