Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to wrap text in title for a two way graph

    Dear all,
    I wonder if anyone knows how to "wrap" the text of a title when creating figures and graphs.
    See the mock example below:
    Code:
    sysuse auto,
    * this is what i get
    scatter price mpg, title("This is a very long title that I hope will adjust, but it may not if it is not long enough, so I add more text")
    * This is something similar to what  I would like to get:
    scatter price mpg, title("This is a very long title that I hope will adjust," "but it may not if its not long enough, so I add more text")
    So the first figure is what i currently have, whereas the lower one is what I want.
    The caveat: the title is given by the variable description, thus, I do not know how to break the text for the title, considering it will be obtained using locals.
    In other words, I would like to know if there is an equivalent to a "wrapping" function when the text of a title exceeds the width of the graph region.

    Thank you in advance.
    Fernando

  • #2
    Dear Fernando,

    The closest I could get to what you are looking for is by (ab)using the community provided package splitvallabels (Nick Winter, Ben Jann).
    However, the objective of splitvallabels is to automatically wrap long value lables over multiple lines for graphing purposes.
    It will not wrap a long variable label (note that your example exceeds the 80 characters limit).
    So, I exercised a mock example by creating a variable that has a label with your exceedingly long string of characters as a 'title' (actually, it is permitted to use a value label that is longer than 80 characters).
    But, all this 'heavy lifting' will not produce the title you are looking for because twoway scatter does not accept it (or I am unable to get it to work because splitvallabels includes the value itself, of course).
    However, when I manually copy the wrapped code that splitvallabels produces into the twoway scatter title option, the graph actually is created as you want.
    So, this sort of proofs that a solution like that of splitvallabels should be possible.
    But my coding skills are limited, therefore, I suggest that you inspect the splitvallabels ado file and rework their solution to produce the wrapped text as the parts that the title option will accept.
    Unless someone else has a better solution to this problem, maybe such effort could result in yet another fine package. Certainly I could use that(!),
    Here is my feeble work:
    Code:
    * Preliminaries
    ssc install splitvallabels
    
    * Data
    sysuse auto, clear
    
    * Example
    gen var_t = 1
    label define lbl_var_t 1 "This is a very long title that I hope will adjust, but it may not if it is not long enough, so I add more text"
    label values var_t lbl_var_t
    splitvallabels var_t , length(55)
    dis `r(relabel)'
    scatter price mpg, title(`r(relabel)')
    // invalid 'so' 
    // r(198);
    scatter price mpg, title(`"This is a very long title that I hope will adjust, but"' `"it may not if it is not long enough, so I add more text"')
    Click image for larger version

Name:	Example_TitleTextWrap.png
Views:	1
Size:	45.6 KB
ID:	1594113


    Best,
    Eric
    http://publicationslist.org/eric.melse

    Comment


    • #3
      Similar issues can be found at https://www.statalist.org/forums/for...or-legend-keys and https://www.statalist.org/forums/for...ture-like-this. Maybe it is something you can request on the wishlist, but the default behavior makes you think twice about whether it is sound to have very long titles or legends. Another issue that is likely to come up is how much text wrapping should be allowed? Should you have 95% text and 5% graph? Certainly, that would not be desirable.

      Comment


      • #4
        There is a wide space in which programmers sometimes decline, by implication if not by design, to make it easy or even possible to do extreme things that users do or might want to do. If you are the (user and) programmer, then the line is often tempting that the command is intended to do what you want it to do, so users should feel free to write their own code. Things I will never do, for example, include direct support for starring P-values as if they were like hotels and restaurants (those were the days...).

        That said, I would put any really long title in the caption below a graph using word or text processing software and am curious why that is not a good solution.

        Comment


        • #5
          How about this:
          Code:
          . sysuse auto,
          (1978 Automobile Data)
          
          . linewrap , longstring("This is a very long title that I hope will adjust, but it may not if its not long enough, so I add more text") maxlength(55)
          . return list
          
          macros:
                       r(nlines) : "2"
                        r(line2) : "but it may not if its not long enough, so I add more text"
                        r(line1) : "This is a very long title that I hope will adjust,"
          
          . scatter price mpg, title("`r(line1)'" "`r(line2)'")
          Available from:

          Code:
          view net describe linewrap, from("http://digital.cgdev.org/doc/stata/MO/Misc")
          Last edited by Mead Over; 12 Feb 2021, 05:13.

          Comment


          • #6
            Dear ericmelse , Nick Cox , Andrew Musau, and Mead Over
            Thank you for all your answers and comments.
            I do agree, 90% of the time, I simply use word, or any other word processing software, to add titles and format them as needed (Often quicker than trying to play around with an obscured Stata code). And those are good solutions.
            On this occasion, I thought I may have overlooked something within the two-way graph title options, that someone else was aware of.
            I do agree with Nick, that this feature of "wrapping" a title may not be a widely needed feature. However, since this problem arose as I was trying to mass produce some margin plots for data exploration, I thought there could be some elegant solution for it.

            On the bright side, I did learn how to add a single "legend" for the plots. (Same data exploration different problem).

            Thanks again,
            Best regards

            Comment


            • #7
              I've updated linewrap to add Unicode support and a "stack" option. See my post here:

              Comment

              Working...
              X