Announcement

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

  • Hide or turn off variable's label in statplot command (supaxis edits?)

    Dear Stata users,

    I use -statplot- (from SSC) wrote by eric_a_booth and Nick Cox. I want to modify variable's label, however, I can not find how to do that. Put it concretely, I want to (1) throughly hide / turn off the variable's label that appears in the left, or if this can not be done, then (2) use a shorter label instead. I know I can specify option varnames to use varname instead of its label. But I think that's not enough. And (3) provide more support to Chinese characters. At least up to now, the option swap() has no effect to Chinese labels.

    Code:
    . which statplot
    c:\ado\plus\s\statplot.ado
    *! statplot - plots of summary statistics
    *! 1.3.0  Eric A. Booth and Nicholas J. Cox  18 July 2026
    Code:
    sysuse citytemp, clear
    statplot heatdd, over(region)
    label variable heatdd "this is a very very long story about Heating degree days"
    statplot heatdd, over(region)
    statplot heatdd, over(region) wrap(18)
    label variable heatdd "这是一个关于高温天数的很长很长很长的故事"
    statplot heatdd, over(region)
    statplot heatdd, over(region) wrap(18)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	151.3 KB
ID:	1787275

  • #2
    Chen Samulsion Thanks for such a clear, reproducible example. The option you're looking for is varopts().

    Briefly, why the label is there at all. statplot turns your variables into categories and hands them to graph hbar, so with a single variable the text on the left is a category label for that variable, not an axis title. That is why ytitle() and similar options never reach it. varopts() passes suboptions straight through to that over().. which gives you full control of it.
    Some options :

    1. Hide the label, example:
    label variable heatdd "this is a very very long story about Heating degree days"
    Code:
    statplot heatdd, over(region) varopts(label(nolabel))
    The region labels stay; only the variable label goes.

    2. Use a shorter label, ex:
    Code:
     
     statplot heatdd, over(region) varopts(relabel(1 "Heating DD"))
    This changes only the graph, not your data. With several variables, number them in plotted order: varopts(relabel(1 "A" 2 "B")).

    3. Chinese labels

    You're right that wrap() does nothing here ( I think in your post you mean wrap() not swap()) and I am glad you reported it, it overlaps with another bug I'm working on fixing currently and can go into an update on SSC soon .Essentially wrap() looks for spaces to break the text , and your label has none: wordcount("这是一个关于高温天数的很长很长很长的故事") returns 1.
    It also measures width with length() which counts bytes rather than characters, so the widths would come out wrong even if spaces were present. Both are things that will be in future updates, but in the meantime, set the lines yourself with nested double quotes :
    Code:
     
     label variable heatdd "这是一个关于高温天数的很长很长很长的故事" statplot heatdd, over(region) ///     varopts(relabel(1 `" "这是一个关于高温天数的" "很长很长很长的故事" "'))
    That gives a clean two-line Chinese label. You could instead insert a space where you want the break and let wrap() find it, but I would not: that route runs into the limit described below, and relabel() does not.

    One last thing worth knowing. In your first graph the label is actually drawn as this is a very very long story a, cut at exactly 32 characters. graph reads an over() category label through a 32-character window and silently drops the rest, so long labels get trimmed whether or not you use wrap(). Text supplied through relabel() is not subject to that limit, which is one more reason to prefer it. ( I will route long labels through relabel() in the next update.)
    Side note: You could also do a hackish relabel to hide labels on the fly:

    Code:
    statplot heatdd, over(region) ///
        varopts(relabel(1 `"."') label(labcolor(white*0)))
    Full testing code to run/apply to your data:
    Code:
    sysuse citytemp, clear
    label variable heatdd "this is a very very long story about Heating degree days"
    
    * 0. the problem
    statplot heatdd, over(region)
    
    * 1. hide the variable label completely (region labels are untouched)
    statplot heatdd, over(region) varopts(label(nolabel))
    
    * 2. use shorter text instead, without editing the variable label
    statplot heatdd, over(region) varopts(relabel(1 "Heating DD"))
    
    * 2b. restyle rather than replace: colour, size, angle
    *     note it is label(labcolor()), not color() -- over() has no color()
    statplot heatdd, over(region) ///
        varopts(relabel(1 "Heating DD") label(labcolor(navy) labsize(small)))
    
    *----------------------------------------------------------------------*
    * 3. Chinese labels
    *----------------------------------------------------------------------*
    label variable heatdd "这是一个关于高温天数的很长很长很长的故事"
    
    * why wrap() does nothing here: one "word", and length() counts bytes
    di wordcount("这是一个关于高温天数的很长很长很长的故事")   //  1
    di length("这是一个关于高温天数的很长很长很长的故事")      // 60 bytes
    di ustrlen("这是一个关于高温天数的很长很长很长的故事")     // 20 characters
    di udstrlen("这是一个关于高温天数的很长很长很长的故事")    // 40 display columns
    
    statplot heatdd, over(region) wrap(18)          // no effect, as you found
    
    * set the lines yourself instead: relabel() takes several quoted strings
    statplot heatdd, over(region) ///
        varopts(relabel(1 `" "这是一个关于高温天数的" "很长很长很长的故事" "'))
    
    * relabel() is also free of the 32-character limit that silently trims
    * labels taken from the variable label, so it handles long text too
    statplot heatdd, over(region) ///
        varopts(relabel(1 `" "这是一个关于高温天数的很长很长很长的故事" "又加了一些更多的中文字符在这里" "'))
    *aside:    
    statplot heatdd, over(region) ///
        varopts(relabel(1 `"."') label(labcolor(white*0)))
    Eric A. Booth | Sr. Researcher | Texas2036.org | www.github.com/EricABooth

    Comment


    • #3
      Dear eric_a_booth, thank you so much for your great illumination and demonstration. I love -statplot- much more. By the way, I am surprising at knowing that you know Chinese ("又加了一些更多的中文字符在这里"). Best Wishes.

      Comment

      Working...
      X