ggplot graph rendered differently in knitted document compared to RStudio plot

36 views Asked by At

Consider the following two images. Both use the same code, but are rendered differently. The first is the image that appears in the Plots panel in RStudio, and the second one is the image that appears in the corresponding knitted HTML document.

Observe the difference in the legends for the 'Below 0.1' label. Why has knitr rendered the document differently and replaced the black colour with the grey background colour of the plot in this legend label? (I realise there are no values below 0.1 hence they are 'missing', but I specifically want the legend label to still appear even when such values are missing.)

Plot created in RStudio

Plot created in knitr

My code is at: https://github.com/a-s-russo/austemp In particular, see the weather_graphs.html file in the pipeline branch which produces the knitr image above. The RStudio image above can be reproduced by installing the package as per the instructions on the main branch page (or see first three code lines below). Both images are created using the following call to plot_temperatures():

> devtools::install_github("a-s-russo/austemp")
> library(austemp)
> data('Sydney')
> plot_temperatures(data = Sydney, season = 'winter')

(NB: ignore the image size differences. That's simply because of how I resized and saved them for the purpose of creating this question.)

Here is the session information for RStudio:

R version 4.3.2 (2023-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22631)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.utf8  LC_CTYPE=English_Australia.utf8    LC_MONETARY=English_Australia.utf8
[4] LC_NUMERIC=C                       LC_TIME=English_Australia.utf8    

time zone: Australia
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] austemp_0.1

loaded via a namespace (and not attached):
 [1] tidyr_1.3.1       utf8_1.2.3        generics_0.1.3    xml2_1.3.6        lattice_0.21-9    stringi_1.8.3     hms_1.1.3        
 [8] digest_0.6.34     magrittr_2.0.3    timechange_0.3.0  grid_4.3.2        padr_0.6.2        pkgload_1.3.4     fastmap_1.1.1    
[15] processx_3.8.3    pkgbuild_1.4.3    sessioninfo_1.2.2 urlchecker_1.0.1  ps_1.7.6          promises_1.2.1    rvest_1.0.3      
[22] httr_1.4.7        purrr_1.0.2       fansi_1.0.4       scales_1.3.0      cli_3.6.1         shiny_1.8.0       rlang_1.1.1      
[29] munsell_0.5.0     ellipsis_0.3.2    withr_3.0.0       remotes_2.4.2.1   cachem_1.0.8      devtools_2.4.5    tools_4.3.2      
[36] tzdb_0.4.0        memoise_2.0.1     dplyr_1.1.3       colorspace_2.1-0  ggplot2_3.4.4     httpuv_1.6.14     curl_5.2.0       
[43] vctrs_0.6.3       R6_2.5.1          mime_0.12         zoo_1.8-12        lubridate_1.9.3   lifecycle_1.0.4   stringr_1.5.1    
[50] fs_1.6.3          htmlwidgets_1.6.4 usethis_2.2.2     miniUI_0.1.1.1    pkgconfig_2.0.3   desc_1.4.3        callr_3.7.3      
[57] pillar_1.9.0      later_1.3.2       gtable_0.3.4      glue_1.6.2        profvis_0.3.8     Rcpp_1.0.12       tibble_3.2.1     
[64] tidyselect_1.2.0  rstudioapi_0.15.0 farver_2.1.1      xtable_1.8-4      htmltools_0.5.7   readr_2.1.5       compiler_4.3.2   

Here's the session info for knitr:

R version 4.3.2 (2023-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22631)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.utf8 LC_CTYPE=English_Australia.utf8
[3] LC_MONETARY=English_Australia.utf8 LC_NUMERIC=C
[5] LC_TIME=English_Australia.utf8

time zone: Australia
tzcode source: internal

attached base packages:
[1] stats graphics grDevices datasets utils methods base

other attached packages:
[1] austemp_0.1 future.callr_0.8.2 future_1.33.1 tarchetypes_0.8.0
[5] targets_1.6.0

loaded via a namespace (and not attached):
[1] gtable_0.3.4 xfun_0.42 bslib_0.6.1 ggplot2_3.5.0
[5] processx_3.8.3 lattice_0.21-9 callr_3.7.5 tzdb_0.4.0
[9] vctrs_0.6.5 tools_4.3.2 ps_1.7.6 generics_0.1.3
[13] base64url_1.4 parallel_4.3.2 tibble_3.2.1 fansi_1.0.6
[17] highr_0.10 pkgconfig_2.0.3 data.table_1.15.2 secretbase_0.3.0.1 
[21] lifecycle_1.0.4 farver_2.1.1 compiler_4.3.2 stringr_1.5.1
[25] munsell_0.5.0 codetools_0.2-19 sass_0.4.8 htmltools_0.5.7
[29] yaml_2.3.8 pillar_1.9.0 jquerylib_0.1.4 tidyr_1.3.1
[33] cachem_1.0.8 parallelly_1.37.1 padr_0.6.2 tidyselect_1.2.0
[37] rvest_1.0.4 digest_0.6.34 stringi_1.8.3 dplyr_1.1.4
[41] purrr_1.0.2 listenv_0.9.1 fastmap_1.1.1 grid_4.3.2
[45] colorspace_2.1-0 cli_3.6.2 magrittr_2.0.3 utf8_1.2.4
[49] readr_2.1.5 withr_3.0.0 scales_1.3.0 backports_1.4.1
[53] lubridate_1.9.3 timechange_0.3.0 rmarkdown_2.26 httr_1.4.7
[57] globals_0.16.3 igraph_2.0.3 zoo_1.8-12 hms_1.1.3
[61] evaluate_0.23 knitr_1.45 rlang_1.1.3 Rcpp_1.0.12
[65] glue_1.7.0 xml2_1.3.6 renv_1.0.3 jsonlite_1.8.8
[69] rstudioapi_0.15.0 R6_2.5.1 fs_1.6.3
0

There are 0 answers