ggrepel
ggrepel copied to clipboard
excessive legend size causes "Viewport has zero dimension(s)" error
Summary
When the legend is too large and ggrepel is used, the following obtuse error text is produced:
Error in grid.Call(C_convert, x, as.integer(whatfrom), as.integer(whatto), : Viewport has zero dimension(s)
Minimal code example
Here is the minimum amount of code needed to demonstrate the issue:
library(ggplot2)
library(ggrepel)
library(ggplot2movies)
ggplot(movies[1:300,], aes(y = year, x = length, color = factor(title))) +
geom_point() +
geom_text_repel(aes(label = title))
Suggestions
Improve the wording of the error message to indicate that the legend is too large.
A legend this large is useless, and hiding it fixes the problem.
library(ggplot2movies)
ggplot(movies[1:300,], aes(y = year, x = length, color = factor(title))) +
geom_point() +
geom_text_repel(aes(label = title)) +
theme(legend.position="none")
Version information
Here is the output from sessionInfo()
in my R session:
R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.2
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggrepel_0.8.0 ggplot2_3.0.0 ggplot2movies_0.0.1 lmerTest_3.0-1 lme4_1.1-17
[6] Matrix_1.2-14
loaded via a namespace (and not attached):
[1] Rcpp_0.12.17 compiler_3.5.0 pillar_1.2.3 nloptr_1.0.4 plyr_1.8.4
[6] bindr_0.1.1 tools_3.5.0 digest_0.6.15 tibble_1.4.2 gtable_0.2.0
[11] nlme_3.1-137 lattice_0.20-35 pkgconfig_2.0.1 rlang_0.2.1 yaml_2.1.19
[16] mvtnorm_1.0-8 bindrcpp_0.2.2 coda_0.19-1 withr_2.1.2 dplyr_0.7.5
[21] lsmeans_2.27-62 tidyselect_0.2.4 grid_3.5.0 glue_1.2.0 R6_2.2.2
[26] survival_2.41-3 multcomp_1.4-8 TH.data_1.0-8 minqa_1.2.4 purrr_0.2.5
[31] magrittr_1.5 scales_0.5.0 codetools_0.2-15 MASS_7.3-49 splines_3.5.0
[36] assertthat_0.2.0 xtable_1.8-2 colorspace_1.3-2 numDeriv_2016.8-1 labeling_0.3
[41] sandwich_2.4-0 estimability_1.3 lazyeval_0.2.1 munsell_0.5.0 zoo_1.8-2
Thanks for reporting this issue!
This is a duplicate of #32. I think we can fix the issue, but it is low priority right now.
I had the same issue
I still have the same issue
@bounlu @Sibojang9 Did you try theme(legend.position = "none")
?
This issue went away for me one day for unknown reason.
I am also having this issue.
@bounlu @Sibojang9 Did you try
theme(legend.position = "none")
?
no I didn't try
I'm also having this issue. It's fine if the legend is removed, but I'm trying to keep the legend. It seems to be an issue with the legend itself, not the "excessive size." Changing the size of the legend, moving the legend around, does not solve the issue.
I tried how this is handled by geom_text()
:
library(ggplot2)
library(ggrepel)
library(ggplot2movies)
ggplot(movies[1:300,], aes(y = year, x = length, color = factor(title), label = title)) +
geom_point() +
geom_text()
This does not trigger an error, but instead slowly plots outside the viewport. I agree that this seem like a low priority problem. There is little use for a discrete color scale with so many levels as they cannot be distinguished. geom_text()
does not trigger any error, but instead outputs garbage... that illustrates the problem. (I guess clipping is done when the plot is rendered as zooming affects how much of the legend fits in the viewport.)
As @slowkow and @kslays suggest adding + theme(legend.position = "none")
solves the problem.
A problem can be suppressing the legend for a single aesthetics. Earlier adding show.legend = FALSE
to geom calls would suppress the legend, but this has changed in recent versions of 'ggplot2'. One needs to control the legend visibility through the scale, which is a more logical approach.
Adding + scale_color_discrete(guide = "none")
hides the guide or legend only for this scale.
To solve this issue try to adjust the width of the plot area in the R studio ! Hope that helps.
I had this issue if I combined a plot with patchwork with or without dropping the legend and with an oversized plot area. It worked fine if I combined the plots with cowplot. Not sure if that observation will help but I thought it was odd and might be related to this issue
@RichardJActon Would you consider sharing a reprex?
It's from a fairly complex bit of code that it will take me a bit to distil a reprex from - I'll post something if/when I get the time
@RichardJActon I had the same problem using patchwork
. The suggestion from @mrizk007 worked for me. Thanks @mrizk007!
To solve this issue try to adjust the width of the plot area in the R studio ! Hope that helps.
Folks experiencing this in RMarkdown can use the chunk options fig.width and fig.height to combat it
Exporting the figure using tiff() and dev.off() solved the problem for me. I guess ggsave() probably also work. Still did not work if I just wanted to plot it in RStudio with the legend. Using + theme(legend.position = "none")
allowed me to see the graph part in RStudio.
Adjusting the width of the plot area in the R studio served as a half-fix, but looking for something better.
theme(legend.position = "none")
will supresss all legends´. Otherwise, supress the legend in a call to the corresponding scale and pass a suitable argument to parameter guide
or use guides()
to remove completly or change the size of a legend: p + guides(colour = "colorbar",size = "none")
see help for ggplot2::guides()
.
Same problem here.