'How to suppress warnings with ggrepel
ggrepel allows to avoid overlapping text labels by repeling labels too near from each other.
The algorithm depends on viewing window size, and a callback occurs when window size is changed.
If the algorithm doesn't manage to avoid overlaps for a given window size, warnings are issued :
Warning messages:
1: ggrepel: 178 unlabeled data points (too many overlaps). Consider increasing max.overlaps
I would like to suppress these warnings, independently of the value of max.overlaps, because they might come delayed and out of context.
The reason for the delay is probably a callback after first print or after changing viewing window width, see example below :
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
I tried suppressWarnings and withCallingHandlers to no avail:
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" )
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


