wilkelab / ggtext

Improved text rendering support for ggplot2
https://wilkelab.org/ggtext/
GNU General Public License v2.0
650 stars 37 forks source link

Incorrect spacing when using multi-byte encoded characters #73

Open s-andrews opened 2 years ago

s-andrews commented 2 years ago

When including unicode characters in axis label text the spacing allowed for the character appears to be based on the number of bytes in the encoded character rather than the number of characters. This means you end up with unnecessary spaces after multi-byte characters.

library(tidyverse)
library(ggtext)

tibble(
  condition=c("3 \u2265 bytes", "2 \u00A9 bytes","1 \u003E byte"),
  value=c(3,2,1)
) %>%
ggplot(aes(x=condition,y=value)) +
  geom_point() +
  theme(axis.text.x = element_markdown(size = 20))

image

> sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

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

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

other attached packages:
 [1] ggtext_0.1.1    forcats_0.5.1   stringr_1.4.0   dplyr_1.0.6    
 [5] purrr_0.3.4     readr_2.0.0     tidyr_1.1.3     tibble_3.1.2   
 [9] ggplot2_3.3.3   tidyverse_1.3.1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7       cellranger_1.1.0 pillar_1.6.1     compiler_4.1.1  
 [5] dbplyr_2.1.1     tools_4.1.1      digest_0.6.27    jsonlite_1.7.2  
 [9] lubridate_1.7.10 lifecycle_1.0.0  gtable_0.3.0     pkgconfig_2.0.3 
[13] rlang_0.4.11     reprex_2.0.0     cli_2.5.0        rstudioapi_0.13 
[17] DBI_1.1.1        haven_2.4.1      xml2_1.3.2       withr_2.4.2     
[21] httr_1.4.2       fs_1.5.0         generics_0.1.0   vctrs_0.3.8     
[25] hms_1.1.0        gridtext_0.1.4   grid_4.1.1       tidyselect_1.1.1
[29] glue_1.4.2       R6_2.5.0         fansi_0.4.2      readxl_1.3.1    
[33] farver_2.1.0     tzdb_0.1.2       modelr_0.1.8     magrittr_2.0.1  
[37] backports_1.2.1  scales_1.1.1     ellipsis_0.3.2   rvest_1.0.0     
[41] assertthat_0.2.1 colorspace_2.0-1 labeling_0.4.2   utf8_1.2.1      
[45] stringi_1.6.1    munsell_0.5.0    broom_0.7.6      markdown_1.1    
[49] crayon_1.4.1