Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I wrap AND truncate axis labels via ggplot?

My axis labels on a column graph are unreasonably long. Initially, I addressed this via stringr thanks to dibisan’s solution on an existing question which allowed me to truncate the labels using the following code:

ig_plot + scale_x_discrete(label = function(x) str_trunc(x, width = 40)) +
          theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))

Plot with truncated labels

I then decided that I wanted to show more characters, and found Sandy Muspratt’s solution to an existing question, using str_wrap:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ig_plot + scale_x_discrete(label = function(x) str_wrap(x, width = 30)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))

Plot with wrapped labels

This second plot is more similar to what I want to display. However, it has too many characters. I want to wrap the labels so that each label is 2 lines of 30 characters, followed by ellipses representing excess characters.

I have tried combining both str_trunc and str_wrap within the scale_x_discrete function as below:

ig_plot + scale_x_discrete(label = function(x) str_wrap(x, width = 30),
                                              str_trunc(x,60)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))

This does not work and generates the error: Error in str_trunc(x, 60) : object 'x' not found.

I had also tried combining as a separate geom, but this results only in the truncation:

ig_plot + scale_x_discrete(label = function(x) str_wrap(x, width = 30)) +
  scale_x_discrete(label = function(x) str_trunc(x, width = 60)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))

Can someone please help write an expression to truncate to 60 characters, while line-breaking at 20? Ideally using stringr (though other solutions appreciated too!).

Code for ig_plot:

ig_plot <- ggplot(ig_protein_long, 
                  aes(x = Protein.names, 
                      y = Abundance,
                      fill = Sample)) +
  geom_col(position = "dodge2")

Data:

Protein.names                                                                                                                                                    Protein.Names2 Sample        Abundance
   <chr>                                                                                                                                                            <chr>          <chr>             <dbl>
 1 Immunoglobulin heavy constant gamma 1 (Ig gamma-1 chain C region) (Ig gamma-1 chain C region EU) (Ig gamma-1 chain C region KOL) (Ig gamma-1 chain C region NIE) IGHG1_HUMAN    2_1A            5864380
 2 Immunoglobulin heavy constant gamma 1 (Ig gamma-1 chain C region) (Ig gamma-1 chain C region EU) (Ig gamma-1 chain C region KOL) (Ig gamma-1 chain C region NIE) IGHG1_HUMAN    2_1A_deglycos   4048260
 3 Immunoglobulin heavy constant gamma 1 (Ig gamma-1 chain C region) (Ig gamma-1 chain C region EU) (Ig gamma-1 chain C region KOL) (Ig gamma-1 chain C region NIE) IGHG1_HUMAN    2_1D            5688230
 4 Immunoglobulin heavy constant gamma 1 (Ig gamma-1 chain C region) (Ig gamma-1 chain C region EU) (Ig gamma-1 chain C region KOL) (Ig gamma-1 chain C region NIE) IGHG1_HUMAN    2_1D_deglycos   3701410
 5 Immunoglobulin heavy constant gamma 1 (Ig gamma-1 chain C region) (Ig gamma-1 chain C region EU) (Ig gamma-1 chain C region KOL) (Ig gamma-1 chain C region NIE) IGHG1_HUMAN    AED5            8264160
 6 Immunoglobulin heavy constant gamma 1 (Ig gamma-1 chain C region) (Ig gamma-1 chain C region EU) (Ig gamma-1 chain C region KOL) (Ig gamma-1 chain C region NIE) IGHG1_HUMAN    AED5_deglycos   7439780
 7 Immunoglobulin heavy constant gamma 1 (Ig gamma-1 chain C region) (Ig gamma-1 chain C region EU) (Ig gamma-1 chain C region KOL) (Ig gamma-1 chain C region NIE) IGHG1_HUMAN    AI5             1189120
 8 Immunoglobulin heavy constant gamma 1 (Ig gamma-1 chain C region) (Ig gamma-1 chain C region EU) (Ig gamma-1 chain C region KOL) (Ig gamma-1 chain C region NIE) IGHG1_HUMAN    AI5_deglycos    1179410
 9 Immunoglobulin heavy constant gamma 3 (HDC) (Heavy chain disease protein) (Ig gamma-3 chain C region)                                                            IGHG3_HUMAN    2_1A             625329
10 Immunoglobulin heavy constant gamma 3 (HDC) (Heavy chain disease protein) (Ig gamma-3 chain C region)                                                            IGHG3_HUMAN    2_1A_deglycos    717111
11 Immunoglobulin heavy constant gamma 3 (HDC) (Heavy chain disease protein) (Ig gamma-3 chain C region)                                                            IGHG3_HUMAN    2_1D             592491
12 Immunoglobulin heavy constant gamma 3 (HDC) (Heavy chain disease protein) (Ig gamma-3 chain C region)                                                            IGHG3_HUMAN    2_1D_deglycos    651356
13 Immunoglobulin heavy constant gamma 3 (HDC) (Heavy chain disease protein) (Ig gamma-3 chain C region)                                                            IGHG3_HUMAN    AED5                 NA
14 Immunoglobulin heavy constant gamma 3 (HDC) (Heavy chain disease protein) (Ig gamma-3 chain C region)                                                            IGHG3_HUMAN    AED5_deglycos    203275
15 Immunoglobulin heavy constant gamma 3 (HDC) (Heavy chain disease protein) (Ig gamma-3 chain C region)                                                            IGHG3_HUMAN    AI5                  NA
16 Immunoglobulin heavy constant gamma 3 (HDC) (Heavy chain disease protein) (Ig gamma-3 chain C region)                                                            IGHG3_HUMAN    AI5_deglycos         NA
17 Immunoglobulin heavy constant gamma 4 (Ig gamma-4 chain C region)                                                                                                IGHG4_HUMAN    2_1A             957383
18 Immunoglobulin heavy constant gamma 4 (Ig gamma-4 chain C region)                                                                                                IGHG4_HUMAN    2_1A_deglycos    804135
19 Immunoglobulin heavy constant gamma 4 (Ig gamma-4 chain C region)                                                                                                IGHG4_HUMAN    2_1D             984904
20 Immunoglobulin heavy constant gamma 4 (Ig gamma-4 chain C region)                                                                                                IGHG4_HUMAN    2_1D_deglycos    645221
21 Immunoglobulin heavy constant gamma 4 (Ig gamma-4 chain C region)                                                                                                IGHG4_HUMAN    AED5            2239410
22 Immunoglobulin heavy constant gamma 4 (Ig gamma-4 chain C region)                                                                                                IGHG4_HUMAN    AED5_deglycos   2265830
23 Immunoglobulin heavy constant gamma 4 (Ig gamma-4 chain C region)                                                                                                IGHG4_HUMAN    AI5              283809
24 Immunoglobulin heavy constant gamma 4 (Ig gamma-4 chain C region)                                                                                                IGHG4_HUMAN    AI5_deglycos     176392
25 Immunoglobulin heavy constant mu (Ig mu chain C region) (Ig mu chain C region BOT) (Ig mu chain C region GAL) (Ig mu chain C region OU)                          IGHM_HUMAN     2_1A            1819170
26 Immunoglobulin heavy constant mu (Ig mu chain C region) (Ig mu chain C region BOT) (Ig mu chain C region GAL) (Ig mu chain C region OU)                          IGHM_HUMAN     2_1A_deglycos   1617100
27 Immunoglobulin heavy constant mu (Ig mu chain C region) (Ig mu chain C region BOT) (Ig mu chain C region GAL) (Ig mu chain C region OU)                          IGHM_HUMAN     2_1D            1904510
28 Immunoglobulin heavy constant mu (Ig mu chain C region) (Ig mu chain C region BOT) (Ig mu chain C region GAL) (Ig mu chain C region OU)                          IGHM_HUMAN     2_1D_deglycos   1260750
29 Immunoglobulin heavy constant mu (Ig mu chain C region) (Ig mu chain C region BOT) (Ig mu chain C region GAL) (Ig mu chain C region OU)                          IGHM_HUMAN     AED5            7667500
30 Immunoglobulin heavy constant mu (Ig mu chain C region) (Ig mu chain C region BOT) (Ig mu chain C region GAL) (Ig mu chain C region OU)                          IGHM_HUMAN     AED5_deglycos   7660800
31 Immunoglobulin heavy constant mu (Ig mu chain C region) (Ig mu chain C region BOT) (Ig mu chain C region GAL) (Ig mu chain C region OU)                          IGHM_HUMAN     AI5              366542
32 Immunoglobulin heavy constant mu (Ig mu chain C region) (Ig mu chain C region BOT) (Ig mu chain C region GAL) (Ig mu chain C region OU)                          IGHM_HUMAN     AI5_deglycos     494940
33 Immunoglobulin heavy constant alpha 1 (Ig alpha-1 chain C region) (Ig alpha-1 chain C region BUR) (Ig alpha-1 chain C region TRO)                                IGHA1_HUMAN    2_1A           38816100
34 Immunoglobulin heavy constant alpha 1 (Ig alpha-1 chain C region) (Ig alpha-1 chain C region BUR) (Ig alpha-1 chain C region TRO)                                IGHA1_HUMAN    2_1A_deglycos  25503900
35 Immunoglobulin heavy constant alpha 1 (Ig alpha-1 chain C region) (Ig alpha-1 chain C region BUR) (Ig alpha-1 chain C region TRO)                                IGHA1_HUMAN    2_1D           42940400
36 Immunoglobulin heavy constant alpha 1 (Ig alpha-1 chain C region) (Ig alpha-1 chain C region BUR) (Ig alpha-1 chain C region TRO)                                IGHA1_HUMAN    2_1D_deglycos  23146300
37 Immunoglobulin heavy constant alpha 1 (Ig alpha-1 chain C region) (Ig alpha-1 chain C region BUR) (Ig alpha-1 chain C region TRO)                                IGHA1_HUMAN    AED5          114771000
38 Immunoglobulin heavy constant alpha 1 (Ig alpha-1 chain C region) (Ig alpha-1 chain C region BUR) (Ig alpha-1 chain C region TRO)                                IGHA1_HUMAN    AED5_deglycos 105117000
39 Immunoglobulin heavy constant alpha 1 (Ig alpha-1 chain C region) (Ig alpha-1 chain C region BUR) (Ig alpha-1 chain C region TRO)                                IGHA1_HUMAN    AI5            56312200
40 Immunoglobulin heavy constant alpha 1 (Ig alpha-1 chain C region) (Ig alpha-1 chain C region BUR) (Ig alpha-1 chain C region TRO)                                IGHA1_HUMAN    AI5_deglycos   56323800
41 Immunoglobulin heavy constant alpha 2 (Ig alpha-2 chain C region) (Ig alpha-2 chain C region BUT) (Ig alpha-2 chain C region LAN)                                IGHA2_HUMAN    2_1A            6839840
42 Immunoglobulin heavy constant alpha 2 (Ig alpha-2 chain C region) (Ig alpha-2 chain C region BUT) (Ig alpha-2 chain C region LAN)                                IGHA2_HUMAN    2_1A_deglycos   3586850
43 Immunoglobulin heavy constant alpha 2 (Ig alpha-2 chain C region) (Ig alpha-2 chain C region BUT) (Ig alpha-2 chain C region LAN)                                IGHA2_HUMAN    2_1D            5939300
44 Immunoglobulin heavy constant alpha 2 (Ig alpha-2 chain C region) (Ig alpha-2 chain C region BUT) (Ig alpha-2 chain C region LAN)                                IGHA2_HUMAN    2_1D_deglycos   3094120
45 Immunoglobulin heavy constant alpha 2 (Ig alpha-2 chain C region) (Ig alpha-2 chain C region BUT) (Ig alpha-2 chain C region LAN)                                IGHA2_HUMAN    AED5           26295900
46 Immunoglobulin heavy constant alpha 2 (Ig alpha-2 chain C region) (Ig alpha-2 chain C region BUT) (Ig alpha-2 chain C region LAN)                                IGHA2_HUMAN    AED5_deglycos  26165000
47 Immunoglobulin heavy constant alpha 2 (Ig alpha-2 chain C region) (Ig alpha-2 chain C region BUT) (Ig alpha-2 chain C region LAN)                                IGHA2_HUMAN    AI5             6706120
48 Immunoglobulin heavy constant alpha 2 (Ig alpha-2 chain C region) (Ig alpha-2 chain C region BUT) (Ig alpha-2 chain C region LAN)                                IGHA2_HUMAN    AI5_deglycos    5510820

>Solution :

To achieve your desired result wrap str_trunc inside str_wrap like so:

df <- data.frame(
  Protein.names = c(
    "Immunoglobulin heavy constant gamma 1 (Ig gamma-1 chain C region) (Ig gamma-1 chain C region EU) (Ig gamma-1 chain C region KOL) (Ig gamma-1 chain C region NIE)",
    "Immunoglobulin heavy constant mu (Ig mu chain C region) (Ig mu chain C region BOT) (Ig mu chain C region GAL) (Ig mu chain C region OU)"
  )
)

library(ggplot2)
library(stringr)

ggplot(df, aes(Protein.names)) +
  geom_bar() +
  scale_x_discrete(label = function(x) str_wrap(str_trunc(x, 60), width = 30)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading