<p>I googled &quot;ggplot2 labels break&quot; and this was the second link: <a href="https://www.andrewheiss.com/blog/2022/06/23/long-labels-ggplot/" rel="nofollow noreferrer">https://www.andrewheiss.com/blog/2022/06/23/long-labels-ggplot/</a></p>
<p>It shows how exactly to do this, something like this after you introduce line breaks:</p>
<pre class="lang-r prettyprint-override"><code>p &lt;- ggplot(fgseaResTidy,
        aes(reorder(topPathways, NES), NES)) +  
        geom_col(aes(fill=padj&lt;0.05)) + 
        coord_flip() + 
        labs(x=&quot;Pathway&quot;, y=&quot;Normalized Enrichment Score&quot;) +
        theme_minimal()

p + scale_x_discrete(
            labels = label_wrap(100))
</code></pre>

CC BY-SA 4.0
2024-10-19T15:55:34.377Z
Thank you for your answer. I also saw this post and I actually tried it from there. But it seems I am doing something wrong, nothing is changing. See picture added to post above.
Bine
CC BY-SA 4.0
2024-10-19T18:53:44.177Z
@Bine How did you introduce line breaks into the label names? You need to do that first, as Ram RS notes above. And please, post code as text, not pictures.
Cloudberry
CC BY-SA 4.0
2024-10-20T09:32:28.990Z
Got it, issue was the "_" :)
Bine
CC BY-SA 4.0
2024-10-18T19:30:48.060Z
2024-10-18T19:30:48.060Z
Ram RS
<p>Good afternoon,</p>
<p>I am trying for a while now to wrap these labels in my plot (I want that it goes to the next line instead of making a very long line).
I tried with the scales package in R, but it seems not to work..
Any chance someone can advise me on this?</p>
<pre class="lang-r prettyprint-override"><code>p&lt;-ggplot(fgseaResTidy, aes(reorder(topPathways, NES), NES)) +  
geom_col(aes(fill=padj&lt;0.05)) +   coord_flip() +   labs(x=&quot;Pathway&quot;,
y=&quot;Normalized Enrichment Score&quot;) +   theme_minimal()

p + scale_x_discrete(
    labels = function(x) str_wrap(x, width = 10),
    drop = FALSE   )
</code></pre>
<p><a href="https://i.sstatic.net/UmehtRtE.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/UmehtRtE.png" alt="enter image description here" /></a></p>
<h2>Update</h2>
<p>Trying with code recommendation from answer below:</p>
<p><a href="https://i.sstatic.net/YjlKriSx.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/YjlKriSx.png" alt="enter image description here" /></a></p>

CC BY-SA 4.0
2024-10-18T19:25:55.233Z
How would one break where to wrap? As far as the program knows, there is no white space to break at even if things allowed for wrapping to happen. Also, font size would go down when wrapped, or rows with wrapping would get larger. Which of these do you expect?
Ram RS
CC BY-SA 4.0
2024-10-20T09:31:19.080Z
Ahh ok! now I got it. I added this line and now it works!! `fgseaResTidy$pathway=str_replace_all(fgseaResTidy$pathway, "_", " ")`
Bine
<r><ggplot2>
Wrap names in ggplot in R
# Wrap names in ggplot in R

(Asked by: username_0 on 2024-10-18)

Good afternoon,
I am trying for a while now to wrap these labels in my plot (I want that it goes to the next line instead of making a very long line).
I tried with the scales package in R, but it seems not to work..
Any chance someone can advise me on this?
p<-ggplot(fgseaResTidy, aes(reorder(topPathways, NES), NES)) +  
geom_col(aes(fill=padj<0.05)) +   coord_flip() +   labs(x="Pathway",
y="Normalized Enrichment Score") +   theme_minimal()

p + scale_x_discrete(
    labels = function(x) str_wrap(x, width = 10),
    drop = FALSE   )


Update
Trying with code recommendation from answer below:

**Tags:** r, ggplot2
> **Comments:**
> **username_1**: How would one break where to wrap? As far as the program knows, there is no white space to break at even if things allowed for wrapping to happen. Also, font size would go down when wrapped, or rows with wrapping would get larger. Which of these do you expect?  
> **username_0**: Ahh ok! now I got it. I added this line and now it works!! `fgseaResTidy$pathway=str_replace_all(fgseaResTidy$pathway, "_", " ")`  

---

## 1 answers

### Answer by username_1 on 2024-10-18. Score: 2 | Accepted answer

I googled "ggplot2 labels break" and this was the second link: https://www.andrewheiss.com/blog/2022/06/23/long-labels-ggplot/
It shows how exactly to do this, something like this after you introduce line breaks:
p <- ggplot(fgseaResTidy,
        aes(reorder(topPathways, NES), NES)) +  
        geom_col(aes(fill=padj<0.05)) + 
        coord_flip() + 
        labs(x="Pathway", y="Normalized Enrichment Score") +
        theme_minimal()

p + scale_x_discrete(
            labels = label_wrap(100))

> **Comments:**
> **username_0**: Thank you for your answer. I also saw this post and I actually tried it from there. But it seems I am doing something wrong, nothing is changing. See picture added to post above.  
> **username_2**: @Bine How did you introduce line breaks into the label names? You need to do that first, as Ram RS notes above. And please, post code as text, not pictures.  
> **username_0**: Got it, issue was the "_" :)  

---

