How to format selected axis text words with italic and bold using ggtext

959 views Asked by At

Any guidance on getting bold and italic formatted axis text using ggtext would be appreciated.

Possibly a problem due to my set up or is something missing?

What I've tried

library(ggplot2)
library(ggtext)


df1 <- data.frame(var1 = c("italic text",
                           "bold text"),
                  var2 = 1:2)

# neither of the formatting options on offer in ggtext seem to work:


var_lab_mkd = c("*italic* text",
                   "**bold** text")

var_lab_css = c("<i>italic</i> text",
                   "<b>bold</b> text")

ggplot(df1, aes(var2, var1))+
  geom_col()+
  scale_y_discrete(labels = var_lab_mkd)+
  # scale_y_discrete(labels = var_lab_css)+
  theme(axis.text.y.left = element_markdown())+
  labs(title = "Axis text formating not working")

This results in:

enter image description here

What I would like, but using ggtext

var_lab_exp = c(expression(italic(italic)~text),
                expression(bold(bold)~text))

ggplot(df1, aes(var2, var1))+
  geom_col()+
  scale_y_discrete(labels = var_lab_exp)+
  labs(title = "Axis text formated as desired")

enter image description here

R session information

R version 4.2.0 (2022-04-22 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.utf8 
[2] LC_CTYPE=English_United Kingdom.utf8   
[3] LC_MONETARY=English_United Kingdom.utf8
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.utf8    

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

other attached packages:
[1] reprex_2.0.1  ggtext_0.1.1  ggplot2_3.3.5

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.8.3      highr_0.9         pillar_1.7.0      compiler_4.2.0   
 [5] R.utils_2.11.0    R.methodsS3_1.8.1 tools_4.2.0       digest_0.6.29    
 [9] evaluate_0.15     lifecycle_1.0.1   tibble_3.1.6      gtable_0.3.0     
[13] R.cache_0.15.0    pkgconfig_2.0.3   rlang_1.0.2       rstudioapi_0.13  
[17] cli_3.3.0         yaml_2.3.5        xfun_0.30         fastmap_1.1.0    
[21] knitr_1.38        withr_2.5.0       dplyr_1.0.8       stringr_1.4.0    
[25] styler_1.7.0      xml2_1.3.3        generics_0.1.2    vctrs_0.4.1      
[29] fs_1.5.2          grid_4.2.0        gridtext_0.1.4    tidyselect_1.1.2 
[33] glue_1.6.2        R6_2.5.1          processx_3.5.3    fansi_1.0.3      
[37] rmarkdown_2.14    callr_3.7.0       clipr_0.8.0       purrr_0.3.4      
[41] farver_2.1.0      magrittr_2.0.3    ps_1.7.0          htmltools_0.5.2  
[45] scales_1.2.0      ellipsis_0.3.2    colorspace_2.0-3  labeling_0.4.2   
[49] utf8_1.2.2        stringi_1.7.6     munsell_0.5.0     markdown_1.1     
[53] crayon_1.5.1      R.oo_1.24.0  
1

There are 1 answers

3
Quinten On

When I run your first code for both formatting, it seems to work:

library(ggplot2)
library(ggtext)


df1 <- data.frame(var1 = c("italic text",
                           "bold text"),
                  var2 = 1:2)

# neither of the formatting options on offer in ggtext seem to work:


var_lab_mkd = c("*italic* text",
                "**bold** text")

var_lab_css = c("<i>italic</i> text",
                "<b>bold</b> text")

ggplot(df1, aes(var2, var1))+
  geom_col()+
  scale_y_discrete(labels = var_lab_mkd)+
  # scale_y_discrete(labels = var_lab_css)+
  theme(axis.text.y.left = element_markdown())+
  labs(title = "Axis text formating not working")

Output:

enter image description here

The text is bold and italic.

library(ggplot2)
library(ggtext)


df1 <- data.frame(var1 = c("italic text",
                           "bold text"),
                  var2 = 1:2)

# neither of the formatting options on offer in ggtext seem to work:


var_lab_mkd = c("*italic* text",
                "**bold** text")

var_lab_css = c("<i>italic</i> text",
                "<b>bold</b> text")

ggplot(df1, aes(var2, var1))+
  geom_col()+
  #scale_y_discrete(labels = var_lab_mkd)+
  scale_y_discrete(labels = var_lab_css)+
  theme(axis.text.y.left = element_markdown())+
  labs(title = "Axis text formating not working")

Output:

enter image description here

As you can see both formating works.

My sessionInfo:

R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS 12.3.1

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib

locale:
[1] nl_NL.UTF-8/nl_NL.UTF-8/nl_NL.UTF-8/C/nl_NL.UTF-8/nl_NL.UTF-8

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

other attached packages:
 [1] ggtext_0.1.1      lubridate_1.8.0   data.table_1.14.2 sjmisc_2.8.9      fpp_0.5           tseries_0.10-49   lmtest_0.9-39     zoo_1.8-9        
 [9] expsmooth_2.3     fma_2.4           forecast_8.16     forcats_0.5.1     stringr_1.4.0     dplyr_1.0.8       purrr_0.3.4       readr_2.1.2      
[17] tidyr_1.2.0       tibble_3.1.6      ggplot2_3.3.5     tidyverse_1.3.1   mapview_2.10.0   

loaded via a namespace (and not attached):
  [1] utf8_1.2.2           tidyselect_1.1.2     htmlwidgets_1.5.4    grid_4.1.0           pROC_1.18.0          devtools_2.4.3       munsell_0.5.0       
  [8] codetools_0.2-18     units_0.8-0          future_1.24.0        withr_2.5.0          colorspace_2.0-3     knitr_1.37           uuid_1.0-4          
 [15] rstudioapi_0.13      stats4_4.1.0         wk_0.6.0             ggsignif_0.6.3       TTR_0.24.3           listenv_0.8.0        labeling_0.4.2      
 [22] ggplotgui_1.0.0      plotROC_2.2.1        farver_2.1.0         rprojroot_2.0.2      parallelly_1.30.0    vctrs_0.4.0          generics_0.1.2      
 [29] ipred_0.9-12         xfun_0.30            markdown_1.1         R6_2.5.1             ggiraph_0.8.2        flexmix_2.3-17       cachem_1.0.6        
 [36] assertthat_0.2.1     promises_1.2.0.1     scales_1.1.1         nnet_7.3-17          gtable_0.3.0         globals_0.14.0       processx_3.5.2      
 [43] timeDate_3043.102    rlang_1.0.2          systemfonts_1.0.4    splines_4.1.0        rstatix_0.7.0        rgdal_1.5-28         lazyeval_0.2.2      
 [50] ModelMetrics_1.2.2.2 broom_0.7.12         s2_1.0.7             abind_1.4-5          yaml_2.3.5           reshape2_1.4.4       modelr_0.1.8        
 [57] crosstalk_1.2.0      backports_1.4.1      rsconnect_0.8.25     httpuv_1.6.5         quantmod_0.4.18      gridtext_0.1.4       caret_6.0-90        
 [64] tools_4.1.0          lava_1.6.10          usethis_2.1.5        ellipsis_0.3.2       raster_3.5-15        RColorBrewer_1.1-2   proxy_0.4-26        
 [71] sessioninfo_1.2.2    Rcpp_1.0.8.3         plyr_1.8.6           base64enc_0.1-3      classInt_0.4-3       ps_1.6.0             prettyunits_1.1.1   
 [78] ggpubr_0.4.0         rpart_4.1.16         fracdiff_1.5-1       haven_2.4.3          fs_1.5.2             leafem_0.1.6         magrittr_2.0.3      
 [85] SparseM_1.81         reprex_2.0.1         pkgload_1.2.4        hms_1.1.1            mime_0.12            evaluate_0.15        xtable_1.8-4        
 [92] leaflet_2.0.3.9000   readxl_1.3.1         testthat_3.1.2       compiler_4.1.0       mice_3.14.0          KernSmooth_2.23-20   crayon_1.5.1        
 [99] htmltools_0.5.2      mgcv_1.8-39          later_1.3.0          tzdb_0.2.0           DBI_1.1.2            sjlabelled_1.1.8     dbplyr_2.1.1        
[106] MASS_7.3-55          sf_1.0-7             car_3.0-12           Matrix_1.4-0         brio_1.1.3           cli_3.2.0            quadprog_1.5-8      
[113] parallel_4.1.0       insight_0.17.0       gower_1.0.0          pkgconfig_2.0.3      sp_1.4-6             terra_1.5-21         plotly_4.10.0       
[120] recipes_0.2.0        xml2_1.3.3           foreach_1.5.2        hardhat_0.2.0        webshot_0.5.2        prodlim_2019.11.13   rvest_1.0.2         
[127] snakecase_0.11.0     callr_3.7.0          digest_0.6.29        rmarkdown_2.13       cellranger_1.1.0     curl_4.3.2           shiny_1.7.1         
[134] urca_1.3-0           satellite_1.0.4      modeltools_0.2-23    lifecycle_1.0.1      nlme_3.1-155         jsonlite_1.8.0       carData_3.0-5       
[141] desc_1.4.1           viridisLite_0.4.0    fansi_1.0.3          pillar_1.7.0         lattice_0.20-45      fastmap_1.1.0        httr_1.4.2          
[148] pkgbuild_1.3.1       survival_3.3-1       glue_1.6.2           xts_0.12.1           remotes_2.4.2        png_0.1-7            iterators_1.0.14    
[155] class_7.3-20         stringi_1.7.6        memoise_2.0.1        e1071_1.7-9          future.apply_1.8.1