Announcement

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

  • Controlling the length of labels for legend keys

    Dear All, I am looking for a way to control the length of the key in legends of graphing commands.
    Consider the following code:
    Code:
    clear all
    version 16.0
    
    sysuse auto
    label define rep78 1 "Aaaaa a aaaaaa aaa aaaaaa aaaaaa aaaaaa a aa aaaaaaaa aaa." ///
    2 "Bbbbbbbb bb b bbbbbbbbbb bbb bbbbbbbbbbbbbbb bbbbb bbbb."
    label values rep78 rep78
    
    graph pie, over(rep78) legend(cols(1) size(2.7))
    I want to see the whole length of the legend key (up to the dot in the end). Yet the actual legend key appears truncated (see image below). One can make the legend wider by increasing the font, but not wider holding the font constant. I am fine with legend keys wrapping as a paragraph (if this is available).

    Thank you, Sergiy.

    Click image for larger version

Name:	legend.png
Views:	1
Size:	50.5 KB
ID:	1552045




  • #2
    Using Nick Winter & Ben Jann's -splitvallabels-
    Code:
    clear all
    version 16.0
    
    sysuse auto
    label define rep78 1 "Aaaaa a aaaaaa aaa aaaaaa aaaaaa aaaaaa a aa aaaaaaaa aaa." ///
    2 "Bbbbbbbb bb b bbbbbbbbbb bbb bbbbbbbbbbbbbbb bbbbb bbbb."
    label values rep78 rep78
    
    splitvallabels rep78, length(40) 
    tokenize `r(relabel)'
    
    graph pie, over(rep78) legend(cols(1) pos(6) size(2.7) label(1 `2') label( 2 `4' ) )
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	17.6 KB
ID:	1552056



    Another way - somewhere along the way -graph pie- calls -_ggroup.ado- which appears to truncate labels to 32 characters. Line 78-80:
    Code:
            if ("`truncate'" == "") {
                local truncate 32
            }
    If you change it to, say 64, then from
    Code:
    graph pie, over(rep78) legend(cols(1) pos(6) size(2.7) textwidth(64)) name(gr,replace)
    you get:
    Click image for larger version

Name:	gr.png
Views:	1
Size:	18.1 KB
ID:	1552057



    Perhaps there are good reason to key the labels to 32 characters, so caveat emptor

    Comment


    • #3
      Dear Scott, thank you very much for taking time to look into the issue and provide a helpful advice. Hacking into the code of the standard ado is not an option (because of portability of the code), so I was hoping to discover an option to legend akin to string(#) and abbreviate(#) options of the list command.

      The command splitvallabels gives me a direction, though not a complete solution to my problem. It works fine if I had to draw only one chart, but with many and with additional conditions it becomes vulnerable to empty groups:

      Code:
      clear all
      version 16.0
      
      sysuse auto
      label define rep78 1 "Aaaaa a aaaaaa aaa aaaaaa aaaaaa aaaaaa a aa aaaaaaaa aaa." ///
      2 "Bbbbbbbb bb b bbbbbbbbbb bbb bbbbbbbbbbbbbbb bbbbb bbbb."
      label values rep78 rep78
      
      splitvallabels rep78, length(40) 
      tokenize `r(relabel)'
      
      graph pie if (rep78!=2), over(rep78) legend(cols(1) pos(6) size(2.7) label(1 `2') label( 2 `4' ) )
      This means that one needs to coordinate the output of splitvallabels to a freq table of the rep78 subject to all conditions, possibly weights, by(), etc. Unfortunately (for this task) the labels are specified in ordinal position and not by the original category code, which would have been simpler.

      Comment

      Working...
      X