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

ggplot2如何横向排列geom_tile并将y轴标签移至图像顶部

所需修改点

  • 互换x、y轴的字段映射,将TEST设为x轴字段、DATA设为y轴字段,实现瓷砖横向排列,压缩纵向占用空间
  • 添加scale_x_discrete(position = "top")配置,将测试项标签移动到图表顶部
  • 合并重复的scale_fill_continuous参数,避免配置覆盖引发的警告
  • 同步调整geom_tile、geom_text的美学映射,以及坐标轴标签文本
  • 可放开宽高比注释配置,根据实际展示需求调整比例,进一步优化空间占用

修改后完整代码

p <- ggplot(data, aes(x= TEST, 
                      y= DATA, 
                      fill = VALUE))+ 
  geom_tile(colour = 'White', width = 1, height = 1) + 
  # 合并重复的fill配置
  scale_fill_continuous(na.value = 'salmon', high = "#4292C6", low = "#C6DBEF")+
  guides(fill=guide_legend(title='Tiles'))+
  # 将x轴(测试项)标签移到顶部
  scale_x_discrete(position = "top") +
  # 调整坐标轴标签对应互换后的轴
  xlab("Test") + ylab('Data') + labs(fill = 'AUROC') +
  geom_text(aes(label = paste0(round(VALUE, 4),'\n','[',CI,']')), size = 4.7, fontface = 'bold') + 
  ggtitle('Data vs Test') +
  theme_bw()+
  # 可放开下面的配置调整宽高比
  #theme(aspect.ratio = 1/5) +
  #coord_fixed(ratio = 0.1) +
  theme(axis.text=element_text(size=18),
        axis.title=element_text(size=24),
        plot.title = element_text(size=26)) + 
  theme(legend.key.size = unit(2,"line"), 
        legend.title = element_text(size=24),
        legend.text = element_text(size=16))
#legend.position = 'bottom', legend.direction="horizontal")
plot(p)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 05:24:01