Announcement

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

  • #16
    In response to @Sergiy Radyakin, I have now added a "stack" option, so that his bar graph comparing the "very poor" to the "somehat better off" can be produced more easily as follows:

    Code:
       . sysuse auto, clear
       . linewrap , longstring(`"very poor, but not as bad as the other guys"') maxlength(15) stack name(firstbar)
       . linewrap , longstring(`"somewhat better, though still far from perfect"') maxlength(15) stack name(secondbar) add
       . graph bar price if rep78<3, over(rep78, relabel(1 `"`r(firstbar)'"' 2 `"`r(secondbar)'"'  )) title("Desired Stata layout") name(better2, replace)
    To respond to the need expressed by @roy zhong when he initiated this thread, I've also added Unicode capabilities to linewrap and a new option "characters". Here's the code to demonstrate the requested bar graph with vertcally stacked Chinese characters.

    Code:
       . clear
       . input double 系数 str15 行业
        .363 "农业采矿业"
        .203 "制造业"
        .307 "建筑业"
        .501 "低端服务业"
        .6 "高端服务业"
        .543 "社会服务业"
        end
        
       . label var 系数  "Variable name"
       . label var 行业  "Bar label"
    One simple approach to graphing the above data with horizontal labels would be:
    Code:
       . graph bar (asis) 系数, over(行业) ytitle("系数") name(vbarhlbl, replace)
    Or, as Nick Cox suggested, one can use horizontal labels with a horizonal bar graph:
    Code:
    graph hbar (asis) 系数, over(行业) ytitle("系数") name(hbarhlbl, replace)
    If the user really does want the Chinese characters to be stacked in a vertical label under each bar, as @roy zhong requested, linewrap can achieve that effect by combining the options "stack" and "characters" like this:
    Code:
       . foreach obs of numlist 1/`=_N' {
            local lbl = 行业[`obs']
            linewrap ,longstr(`"`lbl'"') characters stack name(obs`obs') `add'
            local add add
            local lbl
            
            local thislbl =  `"`obs'  `"`r(obs`obs')'"'"'
            local opt_relabel = `"`opt_relabel'"' +  " "  + `"`thislbl'"'
        }
        
      .  *   Here's the syntax for the option -relabel()-:
      .  di `"`opt_relabel'"'
        
      .  *   Now we can construct the vertical bar with the vertical labels by using the -relabel()- option with the above local macro:
      .  graph bar (asis) 系数, over(行业, relabel(`opt_relabel')) ytitle("系数") name(vbarvlbl, replace)
    Here's the result:
    Click image for larger version

Name:	vbarvlbl.png
Views:	1
Size:	194.5 KB
ID:	1594811


    The new version of -linewrap- with these capabilities can be obtained by either updating the user's previously installed copy like this:
    Code:
    ado update linewrap, update
    or installing the utility for the first time with:
    Code:
       . view net describe linewrap, from("http://digital.cgdev.org/doc/stata/MO/Misc")
    Because Stata introduced Unicode capabilities in Stata 14, linewrap now requires that or a newer version of Stata. But updating or installing linewrap also installs linewrap11,which works with Stata 11, 12 or 13, but lacks Unicode capabilities.
    Last edited by Mead Over; 24 Feb 2021, 00:16.

    Comment

    Working...
    X