Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-10-19T15:55:34.377Z
Answers.Text: 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.
Answers.UserDisplayName: Bine
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-10-19T18:53:44.177Z
Answers.Text: @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.
Answers.UserDisplayName: Cloudberry
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-10-20T09:32:28.990Z
Answers.Text: Got it, issue was the "_" :)
Answers.UserDisplayName: Bine
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-10-18T19:30:48.060Z
Answers.LastActivityDate: 2024-10-18T19:30:48.060Z
Answers.UserDisplayName: Ram RS
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-10-18T19:25:55.233Z
Comments.Text: 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?
Comments.UserDisplayName: Ram RS
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-10-20T09:31:19.080Z
Comments.Text: Ahh ok! now I got it. I added this line and now it works!! `fgseaResTidy$pathway=str_replace_all(fgseaResTidy$pathway, "_", " ")`
Comments.UserDisplayName: Bine
Tags: <r><ggplot2>
Title: Wrap names in ggplot in R
ThreadText: # 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?
  
  ```r
  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   )
  
  ```
  
  [![enter image description here](https://i.sstatic.net/UmehtRtE.png)](https://i.sstatic.net/UmehtRtE.png)
  
  ## Update
  
  Trying with code recommendation from answer below:
  
  [![enter image description here](https://i.sstatic.net/YjlKriSx.png)](https://i.sstatic.net/YjlKriSx.png)
  
  **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:
  
  ```r
  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**: @username_0 How did you introduce line breaks into the label names? You need to do that first, as username_1 notes above. And please, post code as text, not pictures.  
  > **username_0**: Got it, issue was the "_" :)  
  
  ---
  
  