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:
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")
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
When I run your first code for both formatting, it seems to work:
Output:
The text is bold and italic.
Output:
As you can see both formating works.
My sessionInfo: