jrnold / ggthemes

Additional themes, scales, and geoms for ggplot2

Home Page:http://jrnold.github.io/ggthemes/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`ggthemes::theme_wsj()` doesn't work properly with `ggplot2::theme()`

IndrajeetPatil opened this issue · comments

# setup
set.seed(123)
library(ggplot2)
library(ggthemes)

# basic plot
(p <- ggplot(mtcars, aes(wt, mpg)) + 
  geom_jitter() + 
  geom_line()) 

# further modification
p + 
  ggthemes::theme_wsj() +
  ggplot2::theme(
    axis.title.x = ggplot2::element_text(size = 11, face = "bold"),
    axis.title.y = ggplot2::element_text(size = 11, face = "bold")
  ) 
#> Error in unit(rep(just$hjust, n), "npc"): 'x' and 'units' must have length > 0

Created on 2018-12-07 by the reprex package (v0.2.1)

Session info
devtools::session_info()
#> - Session info ----------------------------------------------------------
#>  setting  value                                             
#>  version  R Under development (unstable) (2018-11-30 r75724)
#>  os       Windows 10 x64                                    
#>  system   x86_64, mingw32                                   
#>  ui       RTerm                                             
#>  language (EN)                                              
#>  collate  English_United States.1252                        
#>  ctype    English_United States.1252                        
#>  tz       America/New_York                                  
#>  date     2018-12-07                                        
#> 
#> - Packages --------------------------------------------------------------
#>  package     * version     date       lib
#>  assertthat    0.2.0       2017-04-11 [1]
#>  backports     1.1.2       2017-12-13 [1]
#>  base64enc     0.1-3       2015-07-28 [1]
#>  bindr         0.1.1       2018-03-13 [1]
#>  bindrcpp      0.2.2       2018-03-29 [1]
#>  callr         3.0.0       2018-08-24 [1]
#>  cli           1.0.1.9000  2018-10-30 [1]
#>  colorspace    1.3-2       2016-12-14 [1]
#>  crayon        1.3.4       2017-09-16 [1]
#>  curl          3.2         2018-03-28 [1]
#>  desc          1.2.0       2018-10-30 [1]
#>  devtools      2.0.1       2018-10-26 [1]
#>  digest        0.6.18      2018-10-10 [1]
#>  dplyr         0.7.8       2018-11-10 [1]
#>  evaluate      0.12        2018-10-09 [1]
#>  fs            1.2.6       2018-08-23 [1]
#>  ggplot2     * 3.1.0.9000  2018-11-27 [1]
#>  ggthemes    * 4.0.1       2018-12-08 [1]
#>  glue          1.3.0       2018-07-17 [1]
#>  gtable        0.2.0       2016-02-26 [1]
#>  htmltools     0.3.6       2017-04-28 [1]
#>  httr          1.3.1       2017-08-20 [1]
#>  knitr         1.20        2018-02-20 [1]
#>  labeling      0.3         2014-08-23 [1]
#>  lazyeval      0.2.1       2017-10-29 [1]
#>  magrittr      1.5         2014-11-22 [1]
#>  memoise       1.1.0       2017-04-21 [1]
#>  mime          0.6         2018-10-05 [1]
#>  munsell       0.5.0       2018-06-12 [1]
#>  pillar        1.3.0.9001  2018-11-05 [1]
#>  pkgbuild      1.0.2       2018-10-16 [1]
#>  pkgconfig     2.0.2       2018-08-16 [1]
#>  pkgload       1.0.2       2018-10-29 [1]
#>  plyr          1.8.4       2016-06-08 [1]
#>  prettyunits   1.0.2       2015-07-13 [1]
#>  processx      3.2.1       2018-12-05 [1]
#>  ps            1.2.1       2018-11-06 [1]
#>  purrr         0.2.5.9000  2018-11-28 [1]
#>  R6            2.3.0       2018-10-04 [1]
#>  Rcpp          1.0.0       2018-11-07 [1]
#>  remotes       2.0.2       2018-10-30 [1]
#>  rlang         0.3.0.1     2018-10-25 [1]
#>  rmarkdown     1.10.16     2018-11-23 [1]
#>  rprojroot     1.3-2       2018-01-03 [1]
#>  scales        1.0.0       2018-08-09 [1]
#>  sessioninfo   1.1.1       2018-11-05 [1]
#>  stringi       1.2.4       2018-07-20 [1]
#>  stringr       1.3.1       2018-05-10 [1]
#>  testthat      2.0.1       2018-10-13 [1]
#>  tibble        1.4.99.9006 2018-11-29 [1]
#>  tidyselect    0.2.5       2018-10-11 [1]
#>  usethis       1.4.0.9000  2018-11-30 [1]
#>  withr         2.1.2       2018-03-15 [1]
#>  xml2          1.2.0       2018-01-24 [1]
#>  yaml          2.2.0       2018-07-25 [1]
#>  source                            
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.0)                    
#>  CRAN (R 3.5.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  Github (r-lib/cli@56538e3)        
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  Github (r-lib/desc@7c12d36)       
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  Github (tidyverse/ggplot2@23a23cd)
#>  Github (jrnold/ggthemes@e95e3fb)  
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  Github (r-lib/pillar@c5bf622)     
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  Github (tidyverse/purrr@b4ae036)  
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  Github (rstudio/rmarkdown@34d2fd0)
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.6.0)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  Github (tidyverse/tibble@a248cf1) 
#>  CRAN (R 3.5.1)                    
#>  Github (r-lib/usethis@a4a3f01)    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#>  CRAN (R 3.5.1)                    
#> 
#> [1] C:/Users/inp099/Documents/R/win-library/3.6
#> [2] C:/Program Files/R/R-devel/library

I know how to fix this in the theme itself, but I know how to fix the code so that it works.
The problem is that axis.text = element_blank() in the theme. When you set axis.text.x it is not inheriting the right defaults. I can't tell if it's a problem with how I've defined the theme or with ggplot.
But if you add axis.text = element_text() it will work.

p + 
  theme_wsj() +
  theme(
    axis.title = element_text(),
    axis.title.y = element_text(size = 11, face = "bold"),
    axis.title.x = element_text(size = 11, face = "bold")
  )

Thanks.

This works, but, unfortunately, breaks some other functionality I have in a custom theme. I'd prefer if this is fixed in the theme_wsj() itself.

I'll take a look at it when I have time, but I'm still not sure what's going on in the inheritance. Setting the elements in the inheritance of axis.title.x (meaning title and axis.title) to element_text objects should work, and I don't see how they can break other themes. You can set the styling to whatever you want. It's just that at the moment theme_wsj() doesn't have axis titles because the examples I saw did not include axis titles.

Will it be possible to have this resolved before the next release of ggthemes?

Really need to close an old issue in my package that has been pending on account of this.

If it's of any help, this is the traceback for the error:

> traceback()
20: stop("'x' and 'units' must have length > 0")
19: unit(rep(just$hjust, n), "npc")
18: x %||% unit(rep(just$hjust, n), "npc")
17: title_spec(label, x = x, y = y, hjust = hjust, vjust = vjust, 
        angle = angle, gp = gp, debug = debug, check.overlap = check.overlap)
16: titleGrob(label, x, y, hjust = hj, vjust = vj, angle = angle, 
        gp = modify_list(element_gp, gp), margin = margin, margin_x = margin_x, 
        margin_y = margin_y, debug = element$debug, ...)
15: element_grob.element_text(el, ...)
14: element_grob(el, ...)
13: element_render(theme = theme, element = paste0("axis.title.", 
        label, modify), label = labels[[label]][[i]], margin_x = label == 
        "y", margin_y = label == "x")
12: FUN(X[[i]], ...)
11: lapply(c(1, 2), function(i) {
        modify <- if (i == 1) {
            switch(label, x = ".top", y = ".left")
        }
        else {
            switch(label, x = ".bottom", y = ".right")
        }
        if (is.null(labels[[label]][[i]]) || is.waive(labels[[label]][[i]])) 
            return(zeroGrob())
        element_render(theme = theme, element = paste0("axis.title.", 
            label, modify), label = labels[[label]][[i]], margin_x = label == 
            "y", margin_y = label == "x")
    })
10: FUN(X[[i]], ...)
9: lapply(names(labels), function(label) {
       lapply(c(1, 2), function(i) {
           modify <- if (i == 1) {
               switch(label, x = ".top", y = ".left")
           }
           else {
               switch(label, x = ".bottom", y = ".right")
           }
           if (is.null(labels[[label]][[i]]) || is.waive(labels[[label]][[i]])) 
               return(zeroGrob())
           element_render(theme = theme, element = paste0("axis.title.", 
               label, modify), label = labels[[label]][[i]], margin_x = label == 
               "y", margin_y = label == "x")
       })
   })
8: f(..., self = self)
7: self$render_labels(labels, theme)
6: f(..., self = self)
5: layout$render(geom_grobs, data, theme, plot$labels)
4: ggplot_gtable.ggplot_built(data)
3: ggplot_gtable(data)
2: print.ggplot(x)
1: (function (x, ...) 
   UseMethod("print"))(x)

Closing this because I don't have time to work on it. I will accept a pull request.

Actually, I don't know how or when, but this was already fixed with ggplot2 updates 😁

# setup
set.seed(123)
library(ggplot2)
library(ggthemes)

# basic plot
ggplot(mtcars, aes(wt, mpg)) + 
    geom_jitter() + 
    geom_line() +
  ggthemes::theme_wsj() +
  ggplot2::theme(
    axis.title.x = ggplot2::element_text(size = 11, face = "bold"),
    axis.title.y = ggplot2::element_text(size = 11, face = "bold")
  ) 

Created on 2020-12-28 by the reprex package (v0.3.0)