I am plotting (using ggplot2
) a series of 3D data as a 2D surface plot with a colour scale for the vertical axis. I'm also plotting contours and contour labels (using metR
).
I've come across some data that makes the plot fail with the error Error in nullGrob() : could not find function "nullGrob"
.
I believe the error is due to an interaction between geom_raster
and geom_text_contour
; when trying all combinations of the different geom
's, only those that contain both geom_raster
and geom_text_contour
fail.
Additionally, if I remove the restriction of expand=expansion(mult = 0, add = 0)
, the error doesn't occur.
What is this error, and how can I make it go away?
library(ggplot2)
library(metR)
library(readr)
df <- read_tsv("https://pastebin.com/raw/dJ7gM496")
ggplot(df, aes(x = x, y = y, z = z, fill = z)) +
geom_raster(interpolate=TRUE) +
geom_contour2(color = "black", alpha=0.5) +
geom_text_contour(aes(z = z)) +
scale_x_continuous(expand=expansion(mult = 0, add = 0)) +
scale_y_continuous(expand=expansion(mult = 0, add = 0))
sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)
Matrix products: default
locale:
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252 LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
[5] LC_TIME=English_Australia.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] readr_1.3.1 metR_0.6.0 ggplot2_3.3.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.4 pillar_1.4.3 compiler_3.6.3 plyr_1.8.6 tools_3.6.3 digest_0.6.25 lubridate_1.7.4
[8] memoise_1.1.0 lifecycle_0.2.0 tibble_3.0.0 gtable_0.3.0 checkmate_2.0.0 pkgconfig_2.0.3 rlang_0.4.5
[15] cli_2.0.2 rstudioapi_0.11 curl_4.3 withr_2.1.2 dplyr_0.8.5 stringr_1.4.0 vctrs_0.2.4
[22] hms_0.5.3 grid_3.6.3 tidyselect_1.0.0 glue_1.3.2 data.table_1.12.8 R6_2.4.1 fansi_0.4.1
[29] purrr_0.3.3 farver_2.0.3 magrittr_1.5 backports_1.1.5 scales_1.1.0 ellipsis_0.3.0 assertthat_0.2.1
[36] colorspace_1.4-1 labeling_0.3 stringi_1.4.6 munsell_0.5.0 crayon_1.3.4
As given by @Tjebo, explicitly attaching the grid
package solved the problem; nullGrob()
is a function in that package.
The issue was in my use of the multiplot function. The function includes an explicit call to grid
. When I was playing with some of my plots and reloading workspaces, I ended up playing with my plot before loading multiplot, and so I didn't have grid
loaded.
The sessionInfo()
after including library(grid)
is
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)
Matrix products: default
locale:
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252 LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
[5] LC_TIME=English_Australia.1252
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] readr_1.3.1 metR_0.6.0 ggplot2_3.3.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.4 pillar_1.4.3 compiler_3.6.3 plyr_1.8.6 tools_3.6.3 digest_0.6.25 lubridate_1.7.4
[8] memoise_1.1.0 lifecycle_0.2.0 tibble_3.0.0 gtable_0.3.0 checkmate_2.0.0 pkgconfig_2.0.3 rlang_0.4.5
[15] cli_2.0.2 rstudioapi_0.11 curl_4.3 withr_2.1.2 dplyr_0.8.5 stringr_1.4.0 vctrs_0.2.4
[22] hms_0.5.3 tidyselect_1.0.0 glue_1.3.2 data.table_1.12.8 R6_2.4.1 fansi_0.4.1 purrr_0.3.3
[29] farver_2.0.3 magrittr_1.5 backports_1.1.5 scales_1.1.0 ellipsis_0.3.0 assertthat_0.2.1 colorspace_1.4-1
[36] labeling_0.3 stringi_1.4.6 munsell_0.5.0 crayon_1.3.4