我正在使用 ggplot2 geom_tile 构建热图,并向每个单元格添加一些文本,如下所示:
我正在使用的代码如下。但随着热图中图 block 数量的变化,我需要调整文本大小。有没有办法获取图 block 大小并使用它来设置文本大小?或者自动缩放文本?或者甚至构建文本图像并将其缩放为图 block 上的叠加层?
text.size.corr = 0.9
text.vjust.corr = -0.6
text.size.misc = 0.4
text.vjust.misc = 0.8
text.lineheight.misc = 0.8
ggheatmap =
ggplot(res2.flat,
aes(column, row, fill=cor)
)+
geom_tile(color = "white") +
geom_text( aes( column, row, label=cor ), # add Corr value
color = "black",
size = text.size.corr,
vjust= text.vjust.corr
) +
geom_text( aes( column, row, label=glue("{row}:{column}\np={formatPvalue(p)}\nn={n}") ), # p value
color = "black",
size = text.size.misc,
vjust= text.vjust.misc,
lineheight = text.lineheight.misc
) +
最佳答案
有ggfittext
:
library(tidyverse)
library(ggfittext)
data <- tribble(
~x, ~y, ~value, ~p, ~n,
1, 1, 0.78, 0.05, 132,
1, 2, 0.67, 0.04, 421,
2, 1, 0.72, 0.01, 400,
2, 2, 0.23, 0.01, 300
)
data %>%
ggplot(aes(x, y)) +
geom_tile(aes(fill = value)) +
geom_fit_text(aes(label = paste0(p, "\n", n)), size = 1e3)
由 reprex package 于 2021 年 12 月 10 日创建(v2.0.1)
关于r - 处理 geom_text 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70305422/