Skip to content

Geom_bar wrongly flipped when using dev version. Scale_x_date problem? #3761

Closed

Description

@werkstattcodes

I noticed a strange difference between the ggplot2 version on CRAN and the current dev version. Below two reprex. First the result with the dev version; then the version with the package CRAN. Same data, same code.
Many thanks.

#devtools::install_github("tidyverse/ggplot2")
library(tidyverse)
library(ggplot2)

my_df <- data.frame(
  stringsAsFactors = FALSE,
                date = c("01/05/2013","01/06/2013",
                       "01/07/2013","01/08/2013","01/09/2013","01/10/2013",
                       "01/11/2013","01/12/2013","01/01/2014","01/02/2014",
                       "01/03/2014","01/04/2014","01/05/2014","01/06/2014",
                       "01/07/2014","01/08/2014","01/09/2014","01/10/2014",
                       "01/11/2014","01/12/2014","01/01/2015","01/02/2015",
                       "01/03/2015","01/04/2015","01/05/2015"),
                res = c(0L,0L,1L,0L,0L,0L,1L,0L,
                       0L,0L,2L,0L,0L,1L,1L,0L,3L,0L,0L,0L,0L,0L,
                       8L,0L,11L)
)

my_df %>% 
  mutate(date=lubridate::dmy(date)) %>% 
  ggplot()+
  geom_bar(aes(x=date,
               y=res),
           stat="identity")+
  scale_x_date(date_breaks = "1 month",
               labels=scales::label_date(format="%b"))

sessionInfo()
#> R version 3.6.1 (2019-07-05)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 18362)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_Austria.1252  LC_CTYPE=English_Austria.1252   
#> [3] LC_MONETARY=English_Austria.1252 LC_NUMERIC=C                    
#> [5] LC_TIME=English_Austria.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] forcats_0.4.0      stringr_1.4.0      dplyr_0.8.3        purrr_0.3.3       
#> [5] readr_1.3.1        tidyr_1.0.2        tibble_2.1.3       ggplot2_3.3.0.9000
#> [9] tidyverse_1.3.0   
#> 
#> loaded via a namespace (and not attached):
#>  [1] tidyselect_0.2.5 xfun_0.12        haven_2.2.0      lattice_0.20-38 
#>  [5] colorspace_1.4-1 vctrs_0.2.2      generics_0.0.2   htmltools_0.4.0 
#>  [9] yaml_2.2.0       rlang_0.4.3      pillar_1.4.3     glue_1.3.1      
#> [13] withr_2.1.2      DBI_1.1.0        dbplyr_1.4.2     modelr_0.1.5    
#> [17] readxl_1.3.1     lifecycle_0.1.0  munsell_0.5.0    gtable_0.3.0    
#> [21] cellranger_1.1.0 rvest_0.3.5      evaluate_0.14    labeling_0.3    
#> [25] knitr_1.27       fansi_0.4.1      highr_0.8        broom_0.5.3     
#> [29] Rcpp_1.0.3       scales_1.1.0     backports_1.1.5  jsonlite_1.6    
#> [33] farver_2.0.3     fs_1.3.1         hms_0.5.3        digest_0.6.23   
#> [37] stringi_1.4.5    grid_3.6.1       cli_2.0.1        tools_3.6.1     
#> [41] magrittr_1.5     crayon_1.3.4     pkgconfig_2.0.3  xml2_1.2.2      
#> [45] reprex_0.3.0     lubridate_1.7.4  assertthat_0.2.1 rmarkdown_2.1   
#> [49] httr_1.4.1       R6_2.4.1         nlme_3.1-140     compiler_3.6.1

Created on 2020-01-25 by the reprex package (v0.3.0)

library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 3.6.2

my_df <- data.frame(
  stringsAsFactors = FALSE,
                date = c("01/05/2013","01/06/2013",
                       "01/07/2013","01/08/2013","01/09/2013","01/10/2013",
                       "01/11/2013","01/12/2013","01/01/2014","01/02/2014",
                       "01/03/2014","01/04/2014","01/05/2014","01/06/2014",
                       "01/07/2014","01/08/2014","01/09/2014","01/10/2014",
                       "01/11/2014","01/12/2014","01/01/2015","01/02/2015",
                       "01/03/2015","01/04/2015","01/05/2015"),
                res = c(0L,0L,1L,0L,0L,0L,1L,0L,
                       0L,0L,2L,0L,0L,1L,1L,0L,3L,0L,0L,0L,0L,0L,
                       8L,0L,11L)
)

my_df %>% 
  mutate(date=lubridate::dmy(date)) %>% 
  ggplot()+
  geom_bar(aes(x=date,
               y=res),
           stat="identity")+
  scale_x_date(date_breaks = "1 month",
               labels=scales::label_date(format="%b"))

sessionInfo()
#> R version 3.6.1 (2019-07-05)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 18362)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_Austria.1252  LC_CTYPE=English_Austria.1252   
#> [3] LC_MONETARY=English_Austria.1252 LC_NUMERIC=C                    
#> [5] LC_TIME=English_Austria.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] forcats_0.4.0   stringr_1.4.0   dplyr_0.8.3     purrr_0.3.3    
#> [5] readr_1.3.1     tidyr_1.0.2     tibble_2.1.3    ggplot2_3.2.1  
#> [9] tidyverse_1.3.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] tidyselect_0.2.5 xfun_0.12        haven_2.2.0      lattice_0.20-38 
#>  [5] colorspace_1.4-1 vctrs_0.2.2      generics_0.0.2   htmltools_0.4.0 
#>  [9] yaml_2.2.0       rlang_0.4.3      pillar_1.4.3     glue_1.3.1      
#> [13] withr_2.1.2      DBI_1.1.0        dbplyr_1.4.2     modelr_0.1.5    
#> [17] readxl_1.3.1     lifecycle_0.1.0  munsell_0.5.0    gtable_0.3.0    
#> [21] cellranger_1.1.0 rvest_0.3.5      evaluate_0.14    labeling_0.3    
#> [25] knitr_1.27       fansi_0.4.1      highr_0.8        broom_0.5.3     
#> [29] Rcpp_1.0.3       scales_1.1.0     backports_1.1.5  jsonlite_1.6    
#> [33] farver_2.0.3     fs_1.3.1         hms_0.5.3        digest_0.6.23   
#> [37] stringi_1.4.5    grid_3.6.1       cli_2.0.1        tools_3.6.1     
#> [41] magrittr_1.5     lazyeval_0.2.2   crayon_1.3.4     pkgconfig_2.0.3 
#> [45] xml2_1.2.2       reprex_0.3.0     lubridate_1.7.4  assertthat_0.2.1
#> [49] rmarkdown_2.1    httr_1.4.1       R6_2.4.1         nlme_3.1-140    
#> [53] compiler_3.6.1

Created on 2020-01-25 by the reprex package (v0.3.0)

Activity

  1. yutannihilation commented on Jan 26, 2020

    @yutannihilation
    Member

    Thanks, it seems we need a bit better guess about date data... You can explicitly specify orientation = "x" to specify the plot orientation, by the way.

    More minimal reprex is this:

    library(ggplot2)
    
    # good ----------------------------
    
    d1 <- data.frame(
      date = as.Date("2020/01/25") + 1:3,
      res = 1:3
    )
    
    ggplot(d1) +
      geom_col(aes(x = date, y = res))

    # bad ----------------------------
    
    d2 <- data.frame(
      date = as.Date("2020/01/25") + 1:3 * 10,
      res = 1:3
    )
    
    ggplot(d2) +
      geom_col(aes(x = date, y = res))

    Created on 2020-01-26 by the reprex package (v0.3.0)

  2. yutannihilation commented on Jan 26, 2020

    @yutannihilation
    Member

    @thomasp85
    I guess has_flipped_aes() needs to know the fact that x is Date to handle the case above properly, but since it is called in setup_params(), it has no access to the information. Do you think we can fix this before the next release? (I'm afraid the fix would not be very simple...)

  3. added a commit that references this issue on Feb 3, 2020
    a9df27d
  4. yutannihilation commented on Feb 4, 2020

    @yutannihilation
    Member

    Fixed in the RC version by #3763 (not on master yet). You can try devtools::install_github("tidyverse/[email protected]") until the next release on CRAN.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions