Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8435841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:01:33+00:00 2026-06-10T07:01:33+00:00

I am using ggplot to chart some data and I noticed that legends text

  • 0

I am using ggplot to chart some data and I noticed that legends text are very long and not fitting the window.

+ opts(legend.position = 'bottom', legend.direction = 'horizontal', size=0.1)  +
    guides(colour = guide_legend(nrow = 3), size=1) 

Is there an option in ggplot to wrap the legend text to fit the window.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:01:34+00:00Added an answer on June 10, 2026 at 7:01 am

    Not as far as I know, so I have resorted to a workaround using strwidth(), which calculates the width of text in base graphics.

    title <- "This is a really excessively wide title for a plot, especially since it probably won't fit"
    

    Use par("din") to get the width of the device, and strwidth() to calculate the text size:

    par("din")[1]
    [1] 8.819444
    strwidth(title, units="inches")
    [1] 11.47222
    

    Use it in a function and plot:

    wrapTitle <- function(x, width=par("din")[1]){
      xx <- strwrap(x, width=0.8 * nchar(x) * width / strwidth(x, units="inches"))
      paste(xx, collapse="\n")
    }
    
    wrapTitle(title)
    [1] "This is a really excessively wide title for a plot, especially since it\nprobably won't fit, meaning we somehow have to wrap it"
    

    The plot:

    ggplot(mtcars, aes(wt, mpg)) + geom_point() + opts(title=wrapTitle(title))
    

    enter image description here


    If you want to save the plot to file, then you can replace par("din") with the actual saved plot dimensions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a dotplot using ggplot with the code and data that is
When plotting in R using ggplot, I've noticed that sometimes if you don't specify
I am using ggplot to generate a chart that summarises a race made up
I started to use ggplot in order to generate some graphs. I am using
I am new in R and am trying to so some graphics using ggplot
I'm using ggplot with facet_grid to make a plot for publication. I'm not sure
would like to create a function that generates graphs using ggplot. For the sake
Actually I'm using R + Python with RPY2 to manipulate data and ggplot to
I'm using ggplot to generate some graphics but get the following error: Error in
I am using ggplot to plot time course data (fixation proportions over time to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.