Search code examples
rdataframeggplot2stringrradix

In R, I want to prevent long labels from ruining the image by using '\n'


I am currently using library(fgsea) to create a bar plot in ggplot2 based on the results. However, the long pathway labels are causing issues with the image. I tried to use stringr::str_wrap() and strwrap() to address this, but the labels are not being wrapped properly. Can you suggest how to resolve this issue?

View(results)
                                        pathway         pval       padj        ES      NES nMoreExtreme size  leadingEdge
1                       GOCC_CORNIFIED_ENVELOPE 0.0001061684 0.01894047 0.8468409 2.168420            0   56 KRT77, C....
2                           GOBP_KERATINIZATION 0.0001061684 0.01894047 0.8465294 2.167623            0   56 KRT77, C....
3       GOBP_INTERMEDIATE_FILAMENT_ORGANIZATION 0.0001064849 0.01894047 0.7977803 2.037876            0   55 KRT77, K....
4                         GOCC_KERATIN_FILAMENT 0.0001105828 0.01894047 0.8149854 2.005688            0   39 KRT77, C....
5 GOMF_STRUCTURAL_CONSTITUENT_OF_SKIN_EPIDERMIS 0.0001156069 0.01917240 0.8476196 1.996867            0   28 KRT77, K....
6            GOMF_SEQUENCE_SPECIFIC_DNA_BINDING 0.0000999900 0.01894047 0.4561934 1.286778            0 1520 HOXD13, ....

Solution

  • Since strwrap assumes blank spaces (" ") which are not present in your pathway, you can replace the _ with a space, string-wrap it, then collapse back into one string, optionally putting the _ back.

    results$pathway |>
      gsub("_", " ", x=_) |>
      sapply(function(st) paste(strwrap(st, 25), collapse = "\n"), USE.NAMES=FALSE) |>
      gsub(" ", "_", x=_) |>
      gsub("\n", "\n_", x=_)
    # [1] "GOCC_CORNIFIED_ENVELOPE"                           "GOBP_KERATINIZATION"                              
    # [3] "GOBP_INTERMEDIATE\n_FILAMENT_ORGANIZATION"         "GOCC_KERATIN_FILAMENT"                            
    # [5] "GOMF_STRUCTURAL\n_CONSTITUENT_OF_SKIN\n_EPIDERMIS" "GOMF_SEQUENCE_SPECIFIC\n_DNA_BINDING"             
    

    (This is drawn out a bit for easy-reading, it can easily be condensed into a single call.)