Bold or italicize some axis text - ggplot2

I'm trying to create some axis text as bold and some as italic. Here's a reproducible example of what I'm trying to do. In this toy example, I want anything that ends in a "t" to be bold and the label to be italic otherwise.

I've tried fiddling around with expressions but not getting anywhere. p2 and p3 are all bold and all italic, respectively but I want some bold and some italic.

library(tidyverse)
library(glue)
#> 
#> Attaching package: 'glue'
#> The following object is masked from 'package:dplyr':
#> 
#>     collapse

mpg_alt <- mpg %>%
  mutate(
    BoldLabel=str_ends(class, "t"),
    StyledClass=if_else(BoldLabel, expression(bold(class)), expression(italic(class))) %>% as.list()
  )

mpg_alt %>%
  distinct(BoldLabel, class)
#> # A tibble: 7 x 2
#>   class      BoldLabel
#>   <chr>      <lgl>    
#> 1 compact    TRUE     
#> 2 midsize    FALSE    
#> 3 suv        FALSE    
#> 4 2seater    FALSE    
#> 5 minivan    FALSE    
#> 6 pickup     FALSE    
#> 7 subcompact TRUE

p1 <- mpg_alt %>%
  ggplot(aes(class)) +
  geom_bar()

p2 <- p1 +
  theme(axis.text.x = element_text(face="bold"))
p3 <- p1 +
  theme(axis.text.x = element_text(face="italic"))

mpg_alt %>%
  ggplot(aes(StyledClass)) +
  geom_bar()
#> Warning: Computation failed in `stat_count()`:
#> invalid 'type' (list) of argument

Created on 2022-01-11 by the reprex package (v2.0.0)

Session info
sessionInfo()
#> R version 4.0.5 (2021-03-31)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 20.04.3 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/atlas/libblas.so.3.10.3
#> LAPACK: /usr/lib/x86_64-linux-gnu/atlas/liblapack.so.3.10.3
#> 
#> locale:
#>  [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
#>  [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
#>  [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
#> [10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] glue_1.4.2      forcats_0.5.1   stringr_1.4.0   dplyr_1.0.5    
#>  [5] purrr_0.3.4     readr_1.4.0     tidyr_1.1.3     tibble_3.1.0   
#>  [9] ggplot2_3.3.3   tidyverse_1.3.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] styler_1.6.2      tidyselect_1.1.0  xfun_0.22         haven_2.3.1      
#>  [5] colorspace_2.0-0  vctrs_0.3.7       generics_0.1.0    htmltools_0.5.1.1
#>  [9] yaml_2.2.1        utf8_1.2.1        rlang_0.4.10      R.oo_1.24.0      
#> [13] pillar_1.5.1      withr_2.4.1       DBI_1.1.1         R.utils_2.11.0   
#> [17] dbplyr_2.1.1      readxl_1.3.1      modelr_0.1.8      R.cache_0.15.0   
#> [21] lifecycle_1.0.0   cellranger_1.1.0  munsell_0.5.0     gtable_0.3.0     
#> [25] rvest_1.0.0       R.methodsS3_1.8.1 evaluate_0.14     knitr_1.31       
#> [29] curl_4.3          fansi_0.4.2       highr_0.8         broom_0.7.6      
#> [33] Rcpp_1.0.6        backports_1.2.1   scales_1.1.1      jsonlite_1.7.2   
#> [37] mime_0.10         fs_1.5.0          hms_1.0.0         digest_0.6.27    
#> [41] stringi_1.5.3     grid_4.0.5        cli_2.4.0         tools_4.0.5      
#> [45] magrittr_2.0.1    crayon_1.4.1      pkgconfig_2.0.3   ellipsis_0.3.1   
#> [49] xml2_1.3.2        reprex_2.0.0      lubridate_1.7.10  rstudioapi_0.13  
#> [53] assertthat_0.2.1  rmarkdown_2.7     httr_1.4.2        R6_2.5.0         
#> [57] compiler_4.0.5

Hi Stephanie,

Have you thought about trying the ggtext package? That might be the easiest and clearest way to achieve what you're after. It lets you write markdown syntax rather than have to faff on with expressions!

library(tidyverse)
library(glue)
library(ggtext)

mpg_alt <- mpg %>%
  mutate(
    BoldLabel = str_ends(class, "t"),
    StyledClass = if_else(BoldLabel, glue("<b>{class}</b>"), glue("<i>{class}</i>"))
  )

mpg_alt %>%
  ggplot(aes(StyledClass)) +
  geom_bar() +
  theme(axis.text.x = ggtext::element_markdown())

Created on 2022-01-11 by the reprex package (v2.0.1)

2 Likes

Thanks! This worked, my actual text had some "<" and ">" in it so first had to change that to &lt; and &gt;, respectively.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.