我(使用ggplot2)将一系列3D数据绘制为2D曲面图,并以垂直轴为色标。我还绘制了等高线和等高线标签(使用metR)。
我遇到了一些数据,这些数据导致绘图失败,并显示错误Error in nullGrob() : could not find function "nullGrob"。
我认为这个错误是由于geom_raster和geom_text_contour之间的交互造成的;当尝试不同geom的所有组合时,只有同时包含geom_raster和geom_text_contour的组合才会失败。
另外,如果我取消了对expand=expansion(mult = 0, add = 0)的限制,错误就不会发生。
这个错误是什么,我如何才能让它消失?
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 发布于 2020-04-06 09:12:02
正如@Tjebo所给出的,显式附加grid包解决了问题;nullGrob()是一个function in that package。
问题出在我使用multiplot function的时候。该函数包含对grid的显式调用。当我玩我的一些绘图并重新加载工作区时,我在加载multiplot之前玩完了我的绘图,所以我没有加载grid。
包含library(grid)后的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] 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 https://stackoverflow.com/questions/61044458
复制相似问题