【问题标题】:move y axis labels closer to heatmap (ggplot2) [duplicate]将y轴标签移近热图(ggplot2)[重复]
【发布时间】:2021-12-17 03:31:26
【问题描述】:

我正在尝试将我的 y 轴刻度线移近我的热图。

这里是热图的代码:

ggplot(p_typ_meta, aes(SampleType_Basic, Phylum, fill= Counts)) +geom_tile()+
scale_fill_gradient2(low="blue", mid="white",high="red",midpoint=.15)+theme(axis.title.x = element_text(size=13),axis.title.y = element_text(size=13),axis.text = element_text(size=11),axis.text.y=element_text(margin = margin(0,0)),legend.title.align=0.5, legend.title = element_text(size=13),legend.text = element_text(size=11),plot.title = element_text(hjust=0.5, size=15)) +labs(x="Sample Type", y="Microbial Phyla", title="Microbial Phyla & Sample Type",fill="Relative Abundance")+theme_classic()

我尝试了以下方法:

theme(axis.text.y=element_text(hjust=0.5))
theme(axis.text.y=element_text(margin = margin(0,0)))
theme(axis.ticks.margin=unit(0,'cm'))
theme(axis.text.y = element_text(margin = margin(r = 0)))
scale_y_discrete(expand = c(0, 0))
scale_y_continuous(expand=c(0, 0))

我的代码中是否缺少某些内容?或者这就是热图在 ggplot2 中的工作方式?

我附上了我制作的热图,红色箭头显示了我想要移动 y 轴标签的位置。感谢您的洞察力!

【问题讨论】:

  • 这能回答你的问题吗? Remove space between plotted data and the axes
  • 不幸的是,这似乎只适用于连续的 y 或 x 轴。我尝试了这种方法,以及 scale_y_discrete(expand = c(0, 0)) - 这些都不起作用。
  • expand 的参数将expansion 作为包装器;请参阅任何规模的文档。对于任何 x 或 y 比例,它都应该与类型无关。但是,您不小心将 y 比例分配了两次,而不是 x 一次和 y 一次,这一事实使得这是一个错字。出于这个原因,我投票关闭

标签: r ggplot2 heatmap


【解决方案1】:

使用scale_x_discrete(expand = c(0,0)) 可以解决问题。由于您想减少 x 轴上的空间,因此您需要使用此比例而不是 y 轴的比例。

【讨论】:

  • 这成功了!!感谢您发现我的愚蠢错误,我很感激。
猜你喜欢
  • 2021-01-22
  • 2022-01-18
  • 1970-01-01
  • 2015-07-27
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多