繁体   English   中英

在 ggplot2/metR 中绘图时出现 nullGrob 错误

[英]nullGrob error on plotting in ggplot2 / metR

我正在绘制(使用ggplot2 )一系列 3D 数据作为 2D 表面 plot 与垂直轴的色标。 我还在绘制等高线和等高线标签(使用metR )。

我遇到了一些数据,这些数据使 plot 失败并出现错误Error in nullGrob(): could not find function "nullGrob"

我相信该错误是由于geom_rastergeom_text_contour之间的交互造成的; 在尝试不同geom的所有组合时,只有那些同时包含geom_rastergeom_text_contour的组合才会失败。

此外,如果我删除expand=expansion(mult = 0, add = 0)的限制,则不会发生错误。

这是什么错误,我怎样才能让它 go 消失?

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))

会话信息()

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     

正如@Tjebo 给出的,明确附加grid package 解决了问题; nullGrob()function 中的 package

问题在于我使用多图 function function 包括对grid的显式调用。 当我在玩我的一些情节并重新加载工作区时,我最终在加载多图之前玩了我的 plot,所以我没有加载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   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM