r - 如何使用 ggrepel 抑制警告
问题描述
ggrepel
允许通过将标签相互排斥得太近来避免重叠的文本标签。
该算法取决于查看窗口大小,当窗口大小改变时会发生回调。
如果算法无法避免给定窗口大小的重叠,则会发出警告:
Warning messages:
1: ggrepel: 178 unlabeled data points (too many overlaps). Consider increasing max.overlaps
我想压制这些警告,与 的值无关max.overlaps
,因为它们可能会延迟并脱离上下文。
延迟的原因可能是第一次打印或更改查看窗口宽度后的回调,请参见下面的示例:
library(ggplot2)
library(ggrepel)
N <- 50
data <- data.frame(x=1:N,y=rep(1,N),label = paste0("Text",1:N))
ggplot(data)+
geom_point(aes(x=x,y=y))+
geom_text_repel(aes(x=x,y=y,label=label),hjust=0, vjust=0, box.padding = 0.5, max.overlaps = 20)
# If the viewing window width is OK, no warning!
# Execute anything in console
1
#[1] 1
# This is OK!
# Now reduce window width with mouse
# No warning yet
# Execute anything in console
1
# Warning messages:
# 1: ggrepel: 41 unlabeled data points (too many overlaps). Consider increasing max.overlaps
# 2: ggrepel: 41 unlabeled data points (too many overlaps). Consider increasing max.overlaps
# 3: ggrepel: 41 unlabeled data points (too many overlaps). Consider increasing max.overlaps
# 4: ggrepel: 47 unlabeled data points (too many overlaps). Consider increasing max.overlaps
# 5: ggrepel: 47 unlabeled data points (too many overlaps). Consider increasing max.overlaps
# 6: ggrepel: 47 unlabeled data points (too many overlaps). Consider increasing max.overlaps
# 7: ggrepel: 47 unlabeled data points (too many overlaps). Consider increasing max.overlaps
# 8: ggrepel: 47 unlabeled data points (too many overlaps). Consider increasing max.overlaps
# 9: ggrepel: 47 unlabeled data points (too many overlaps). Consider increasing max.overlaps
#[1] 1
我试过suppressWarnings
但withCallingHandlers
无济于事:
withCallingHandlers(
suppressWarnings(ggplot(data)+
geom_point(aes(x=x,y=y))+
geom_text_repel(aes(x=x,y=y,label=label),hjust=0, vjust=0, box.padding = 0.5, max.overlaps = 20)
)
, warning=function(w) {
if( any( grepl( "ggrepel", w) ) ) invokeRestart( "muffleWarning" )
})
解决方案
推荐阅读
- c++ - 通过域名而不是使用 C/++ 套接字的 IP 地址访问 Web 服务器
- python - 向条形图添加更多数据
- python - 如何遍历 pandas 中的行并获取特定但不同的列名
- swift - 快速将 2 个视图与自定义颜色混合?
- hyperledger-fabric - 将排序者添加到超级账本结构中的现有通道
- php - HTML 表单 - 使用 PHP 帖子输入隐藏的输入类型不起作用
- java - Java字符串将存储的值重置为null
- javascript - 使用 npm store.js 包在 cookie 上设置过期日期
- python - Discord.py 赋予角色
- python - 累计计算列表中每个元素的出现次数