Ggplot2: Cross-Platform Reproducibility (MacOS vs Win)

Hi Guys,

My colleague ran the same code (in Win) below for the same dataset but encounter different plot output images from me (in MacOS), see attached. We tried scale_x_discrete to set x-labels so the output x labels are the same but didn’t work. Not sure what we’re doing wrong. We run-out of ideas in how to fix this problem in the Windows. We are open to suggestions.

Thank you

deck_nat_4_cuts <- ggplot(data = dio_nat_4_cut, 
                          aes(x =year, y = percent, 
                              fill = factor(Var2, levels = c("10% or More Above", 
                                                             "0 - 9% Above", 
                                                             "0 - 9% Below", 
                                                             "10% or More Below")))) + 
  geom_bar(stat = 'identity')+
  geom_text(position = "stack", aes(x=year, y=percent, label=show_pct), vjust=1.2, colour="white", size=4, fontface="bold")+ 
  report_theme_cpg + 
  scale_fill_manual(values = c(cpg_com_ppt_blue, cpg_com_ppt_green, cpg_com_ppt_purple, cpg_com_ppt_orange)) +
  labs(y="Percent") +	
  theme(axis.title.x = element_blank(), legend.position = "bottom") +
  guides(fill = guide_legend(reverse = FALSE, nrow = 2))

Report Theme:

report_theme_cpg <- theme(
  axis.ticks = element_blank(),
  panel.grid = element_line(colour="white"),
  text = element_text(family="Arial"),
  legend.text=element_text(size = 13,  face = "bold", family='Arial', colour="black"),
  axis.text=element_text(size = 12,  face = "bold", family='Arial', colour="black"),
  axis.title=element_text(size = 12, face = "bold", family='Arial'),
  panel.grid.major=element_blank(),
  legend.title=element_blank() 
)

Windows Output( Wrong Placement of Labels)

Session info ---------------------------------------------------------------
 setting  value                       
 version  R version 3.6.2 (2019-12-12)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       RStudio                     
 language (EN)                        
 collate  English_United States.1252  
 ctype    English_United States.1252  
 tz       America/New_York            
 date     2021-02-22                  

- Packages -------------------------------------------------------------------
 package     * version date       lib source                          
 assertthat    0.2.1   2019-03-21 [1] CRAN (R 3.6.2)                  
 backports     1.1.7   2020-05-13 [1] CRAN (R 3.6.3)                  
 callr         3.4.3   2020-03-28 [1] CRAN (R 3.6.3)                  
 checkmate     2.0.0   2020-02-06 [1] CRAN (R 3.6.3)                  
 cli           2.0.2   2020-02-28 [1] CRAN (R 3.6.3)                  
 codetools     0.2-16  2018-12-24 [1] CRAN (R 3.6.2)                  
 colorspace    1.4-1   2019-03-18 [1] CRAN (R 3.6.1)                  
 crayon        1.3.4   2017-09-16 [1] CRAN (R 3.6.2)                  
 curl          4.3     2019-12-02 [1] CRAN (R 3.6.2)                  
 desc          1.2.0   2018-05-01 [1] CRAN (R 3.6.2)                  
 devtools    * 2.3.0   2020-04-10 [1] CRAN (R 3.6.3)                  
 digest        0.6.25  2020-02-23 [1] CRAN (R 3.6.3)                  
 DomoR         0.1     2020-09-21 [1] Github (domoinc-r/DomoR@0d0141c)
 dplyr       * 1.0.0   2020-05-29 [1] CRAN (R 3.6.3)                  
 ellipsis      0.3.1   2020-05-15 [1] CRAN (R 3.6.3)                  
 evaluate      0.14    2019-05-28 [1] CRAN (R 3.6.2)                  
 extrafont   * 0.17    2014-12-08 [1] CRAN (R 3.6.2)                  
 extrafontdb   1.0     2012-06-11 [1] CRAN (R 3.6.0)                  
 fansi         0.4.1   2020-01-08 [1] CRAN (R 3.6.2)                  
 farver        2.0.3   2020-01-16 [1] CRAN (R 3.6.2)                  
 fs            1.4.2   2020-06-30 [1] CRAN (R 3.6.3)                  
 generics      0.0.2   2018-11-29 [1] CRAN (R 3.6.2)                  
 ggplot2     * 3.3.2   2020-06-19 [1] CRAN (R 3.6.3)                  
 glue          1.4.1   2020-05-13 [1] CRAN (R 3.6.3)                  
 gridExtra   * 2.3     2017-09-09 [1] CRAN (R 3.6.2)                  
 gtable        0.3.0   2019-03-25 [1] CRAN (R 3.6.2)                  
 hms           0.5.3   2020-01-08 [1] CRAN (R 3.6.2)                  
 htmlTable     2.0.1   2020-07-05 [1] CRAN (R 3.6.3)                  
 htmltools     0.5.0   2020-06-16 [1] CRAN (R 3.6.3)                  
 htmlwidgets   1.5.1   2019-10-08 [1] CRAN (R 3.6.2)                  
 httr          1.4.1   2019-08-05 [1] CRAN (R 3.6.2)                  
 knitr         1.29    2020-06-23 [1] CRAN (R 3.6.3)                  
 labeling      0.3     2014-08-23 [1] CRAN (R 3.6.0)                  
 lifecycle     0.2.0   2020-03-06 [1] CRAN (R 3.6.3)                  
 magrittr      1.5     2014-11-22 [1] CRAN (R 3.6.2)                  
 memoise       1.1.0   2017-04-21 [1] CRAN (R 3.6.2)                  
 munsell       0.5.0   2018-06-12 [1] CRAN (R 3.6.2)                  
 pillar        1.4.6   2020-07-10 [1] CRAN (R 3.6.3)                  
 pkgbuild      1.1.0   2020-07-13 [1] CRAN (R 3.6.2)                  
 pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 3.6.2)                  
 pkgload       1.1.0   2020-05-29 [1] CRAN (R 3.6.3)                  
 plyr          1.8.6   2020-03-03 [1] CRAN (R 3.6.3)                  
 prettyunits   1.1.1   2020-01-24 [1] CRAN (R 3.6.3)                  
 processx    * 3.4.3   2020-07-05 [1] CRAN (R 3.6.3)                  
 pryr          0.1.4   2018-02-18 [1] CRAN (R 3.6.2)                  
 ps            1.3.3   2020-05-08 [1] CRAN (R 3.6.3)                  
 purrr         0.3.4   2020-04-17 [1] CRAN (R 3.6.3)                  
 R6            2.4.1   2019-11-12 [1] CRAN (R 3.6.2)                  
 Rcpp          1.0.5   2020-07-06 [1] CRAN (R 3.6.3)                  
 readr         1.3.1   2018-12-21 [1] CRAN (R 3.6.2)                  
 remotes       2.1.1   2020-02-15 [1] CRAN (R 3.6.3)                  
 reshape2    * 1.4.4   2020-04-09 [1] CRAN (R 3.6.3)                  
 rlang         0.4.6   2020-05-02 [1] CRAN (R 3.6.3)                  
 rmarkdown     2.3     2020-06-18 [1] CRAN (R 3.6.3)                  
 rprojroot     1.3-2   2018-01-03 [1] CRAN (R 3.6.2)                  
 rstudioapi    0.11    2020-02-07 [1] CRAN (R 3.6.3)                  
 Rttf2pt1      1.3.8   2020-01-10 [1] CRAN (R 3.6.2)                  
 scales      * 1.1.1   2020-05-11 [1] CRAN (R 3.6.3)                  
 sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 3.6.2)                  
 stringi       1.4.6   2020-02-17 [1] CRAN (R 3.6.2)                  
 stringr     * 1.4.0   2019-02-10 [1] CRAN (R 3.6.2)                  
 testthat      2.3.2   2020-03-02 [1] CRAN (R 3.6.3)                  
 tibble        3.0.2   2020-07-07 [1] CRAN (R 3.6.3)                  
 tidyselect    1.1.0   2020-05-11 [1] CRAN (R 3.6.3)                  
 usethis     * 1.6.1   2020-04-29 [1] CRAN (R 3.6.3)                  
 vctrs         0.3.1   2020-06-05 [1] CRAN (R 3.6.3)                  
 withr         2.2.0   2020-04-20 [1] CRAN (R 3.6.3)                  
 xfun          0.15    2020-06-21 [1] CRAN (R 3.6.3)                  
 yaml          2.2.1   2020-02-01 [1] CRAN (R 3.6.2)                  

MacOS Output(Correct Placement of Labels)

R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Mojave 10.14.6
Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/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
[4] utils     datasets  methods
[7] base
other attached packages:
 [1] showtext_0.9-2
 [2] showtextdb_3.0
 [3] sysfonts_0.8.3
 [4] tm_0.7-8
 [5] NLP_0.2-1
 [6] extrafont_0.17
 [7] gridExtra_2.3
 [8] devtools_2.3.2
 [9] usethis_1.6.3
[10] processx_3.4.4
[11] reshape2_1.4.4
[12] lubridate_1.7.9.2
[13] forcats_0.5.0
[14] stringr_1.4.0
[15] purrr_0.3.4
[16] readr_1.4.0
[17] tidyr_1.1.2
[18] tibble_3.0.4
[19] tidyverse_1.3.0
[20] ggplot2_3.3.2
[21] scales_1.1.1
[22] dplyr_1.0.2
loaded via a namespace (and not attached):
 [1] httr_1.4.2
 [2] pkgload_1.1.0
 [3] jsonlite_1.7.1
 [4] modelr_0.1.8
 [5] assertthat_0.2.1
 [6] cellranger_1.1.0
 [7] slam_0.1-48
 [8] remotes_2.2.0
 [9] sessioninfo_1.1.1
[10] Rttf2pt1_1.3.8
[11] pillar_1.4.7
[12] backports_1.2.0
[13] glue_1.4.2
[14] extrafontdb_1.0
[15] digest_0.6.27
[16] pryr_0.1.4
[17] rvest_0.3.6
[18] colorspace_2.0-0
[19] plyr_1.8.6
[20] pkgconfig_2.0.3
[21] broom_0.7.2
[22] haven_2.3.1
[23] DomoR_0.1
[24] generics_0.1.0
[25] farver_2.0.3
[26] ellipsis_0.3.1
[27] withr_2.3.0
[28] cli_2.2.0
[29] magrittr_2.0.1
[30] crayon_1.3.4
[31] readxl_1.3.1
[32] memoise_1.1.0
[33] ps_1.4.0
[34] fs_1.5.0
[35] fansi_0.4.1
[36] xml2_1.3.2
[37] pkgbuild_1.1.0
[38] tools_4.0.3
[39] prettyunits_1.1.1
[40] hms_0.5.3
[41] lifecycle_0.2.0
[42] munsell_0.5.0
[43] reprex_0.3.0
[44] callr_3.5.1
[45] compiler_4.0.3
[46] rlang_0.4.8
[47] grid_4.0.3
[48] rstudioapi_0.13
[49] labeling_0.4.2
[50] testthat_3.0.0
[51] gtable_0.3.0
[52] codetools_0.2-16
[53] DBI_1.1.0
[54] curl_4.3
[55] R6_2.5.0
[56] utf8_1.1.4
[57] rprojroot_2.0.2
[58] desc_1.2.0
[59] stringi_1.5.3
[60] parallel_4.0.3
[61] Rcpp_1.0.5
[62] vctrs_0.3.5
[63] dbplyr_2.0.0
[64] tidyselect_1.1.0

It's hard to be certain without seeing how the data are coded in each instance, but the Windows plot looks like year is coded as numeric (ggplot automatically set the axis labels to 3-year intervals, starting with 2010, and these tickmarks don't happen to overlap with any of the year values in your data) and the MacOS plot looks like year is coded as either character or factor (that is, year is categorical). If so, this is unlikely to have anything to do with Windows vs. MacOS. However, it could result from each of you running slightly different code to clean and process your data before running the plot code.

You're also running different major versions of R (3.x.x vs. 4.x.x). I can't think of a way this would matter here, but that's another possibility if all of your code (including the code that comes before the plot code) is exactly the same in both cases.

Can you and your colleague run str(dio_nat_4_cut) just before running the plot code and check whether year is coded differently or the same on each of your systems?

Also, note that numeric coding preserves the true distances (in time) between bars, while character or factor coding spaces bars equally, regardless of the actual distance in time between them. Below are some examples of how a plot looks with different codings of the x-axis variable.

library(tidyverse)
theme_set(theme_bw())

d = data.frame(year=c(2009,2012,2015,2018,2020),
               value=c(1:5))

# year coded as numeric
d %>% 
  ggplot(aes(year, value)) + geom_col()

# year coded as numeric plus annual x-axis ticks
d %>% 
  ggplot(aes(year, value)) + geom_col() + 
  scale_x_continuous(breaks=min(d$year):max(d$year))

# year coded as factor
d %>% 
  mutate(year = factor(year)) %>% 
  ggplot(aes(year, value)) + geom_col()

  
# year coded as factor but with all intervening years included. 
# To do this, we need to (1) code year as a factor with all intervening
#  years included in the factor levels, and (2) add drop=FALSE to 
#  scale_x_discrete, which results in all levels being included in the x-axis
d %>% 
  mutate(year = factor(year, levels=min(year):max(year))) %>% 
  ggplot(aes(year, value)) + geom_col() + scale_x_discrete(drop=FALSE)

Created on 2021-02-23 by the reprex package (v1.0.0)

2 Likes

Hi @joels,

Thank you!! :grinning: You were right. The problem was just that . R's Windows and R's Mac uploading variables different. It is always the most simple problem that is hard to find.

Thanks again,
CityExplorer

2 Likes

Hello @joels,
As my colleague said, thanks so much for your help. Cannot believe we didn't think to check the class of our variables. Lesson learned!
Thanks,
Squirlz

2 Likes