Announcement

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

  • Using splitvallabels in graphs

    Hi everyone,

    Cutting to the chase, while producing graphs, specifically catplot, I'm using splitvallabels to split my labels of the variables but while using splitvallables in catplot,
    (a) one of my labels of the variable which is "other" is missing.
    (b) and the distribution of the labels within the variable is also wrong.

    To show you in detail, this is the code I'm trying to run:

    foreach var of varlist var1 var2 var3{
    cap confirm numeric var `var'
    if !_rc {

    local varlab : variable label `var'
    local varlab = subinstr("`macval(varlab)'","$","",.)

    qui sum `var'
    count if !missing(`var')
    local N `r(N)'
    {
    splitvallabels `var'
    local relabel `r(relabel)'
    catplot `var', percent var1opts(label(labsize(vsmall)) relabel(`r(relabel)')) blabel(bar, position(outside) format(%4.1f) size(vsmall)) ///
    title("`macval(varlab)'", size(small) pos(11) justification(left)) legend(size(tiny)) l1title("") ///
    ytitle ("Percentage", size(small)) graphregion(fcolor(white)) aspect(1.2) ///
    note("{it:n=`N' respondents}", size(small))
    graph export "$figures/`var'.png", replace
    graph export "$reports/`var'.eps", replace
    }

    else {
    graph twoway scatteri 1 1 "No observations yet", mlabpos(0) msym(i) ///
    title("`var'", pos(11) justification(left) size(small)) graphregion(fcolor(white)) ///
    yscale(off noline) xscale(off noline)
    graph export "$figures/`var'.png", replace
    graph export "$reports/`var'.eps", replace
    }
    }
    }

    and, this is the graph I'm getting :

    Click image for larger version

Name:	With_split_new.png
Views:	2
Size:	56.4 KB
ID:	1715434


    While on the other hand, if I'm using the catplot without splitvallabels then all of my labels are appearing including the label "other" with the correct distribution, just the label is being cut short.

    foreach var of varlist var1 var2 var3{
    cap confirm numeric var `var'
    if !_rc {

    local varlab : variable label `var'
    local varlab = subinstr("`macval(varlab)'","$","",.)

    qui sum `var'
    count if !missing(`var')
    local N `r(N)'
    {
    catplot `var', percent var1opts(label(labsize(vsmall))) blabel(bar, position(outside) format(%4.1f) size(vsmall)) ///
    title("`macval(varlab)'", size(small) pos(11) justification(left)) legend(size(third_tiny)) l1title("") ///
    ytitle ("Percentage", size(small)) graphregion(fcolor(white)) aspect(1.2) ///
    note("{it:n=`N' respondents}", size(small))
    graph export "$figures/`var'.png", replace
    graph export "$reports/`var'.eps", replace
    }

    else {
    graph twoway scatteri 1 1 "No observations yet", mlabpos(0) msym(i) ///
    title("`var'", pos(11) justification(left) size(small)) graphregion(fcolor(white)) ///
    yscale(off noline) xscale(off noline)
    graph export "$figures/`var'.png", replace
    graph export "$reports/`var'.eps", replace
    }
    }
    }

    The graph is as follows:
    Click image for larger version

Name:	without_split.png
Views:	1
Size:	46.0 KB
ID:	1715432

    Can anyone please help me why with splitvallabels, the label other is disappearing and the distribution is also happening wrong?

    Thank you!
    Attached Files
    Last edited by Sunidhi Agarwal; 30 May 2023, 01:34.

  • #2
    Where did you get splitvallabels? It is not part of official Stata nor could I find it on SSC. I cannot tell you what (if anything) is wrong with splitvallabels if I cannot see it. I can guess what it does, and I have a rough idea how it does it (or at least, how I would do it), but bugs are often tiny details in the code.

    Also, as is stated in the Statalist FAQ (black bar near the top of this page) you are asked to provide a minimal example of your data, so we can see what is actually happening.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Maarten, this is strange. I have installed the program from SSC on 24th March 2022. The reference is Winter, Nick, Ben Jann (2004). SPLITVALLABELS: Stata module to split up value labels for multi-line graph labelling. Statistical Software Components S436401, Boston College Department of Economics, revised 14 Aug 2008.
      I can still find it under http://fmwww.bc.edu/RePEc/bocode/s/

      But
      Code:
      search splitvallabels
      in Stata 18 gives no matches...

      Comment


      • #4
        splitvallabels is indeed from SSC, as the OP is asked to explain (FAQ Advice #12). catplot is from SSC, ditto.

        The problem in #1 is fairly simple and the solution is simple and documented.

        You need the recode option with splitvallabels for use with catplot as it is a wrapper for graph hbar (equivalently graph bar).

        Comment


        • #5
          Hi Nick, thank you for your reply!

          I did try recoding as well, the same problem appeared again.

          As an alternative, I shortened the labels name and ran the catplot without using splitvallabels.

          Comment


          • #6


            Hi Sunidhi,
            I had the same problem as you and was stumped. Then we discovered that the problem occurs when your graph does not contain cases in all categories of the split labels variable. The recode option maps the levels of the variable based on the entire sample, not just the ones used in your graph. So if your graph does not use all categories, the labels will be misplaced. The solution was to specify the if/in option. In my case:

            Code:
            #this code was giving me the wrong split labels
            splitvallabels var1, length(45) recode local(relabel)
            graph hbar (percent), over(var2, gap(*1)) over(var1,  relabel(`relabel') sort (2) gap(*1) lab(labsize(vsmall)))  blabel(bar, format(%4.1f) size(vsmall))  asyvars title(`: variable label var1') ytitle("%") name(var1_co, replace) scheme (tab1) note("N= 15455")
            
            # but like for you, this code was showing the correct labels in the graph:
            graph hbar (percent), over(var2, gap(*1)) over(var1,  sort (2) gap(*1) lab(labsize(vsmall)))  blabel(bar, format(%4.1f) size(vsmall))  asyvars title(`: variable label var1') ytitle("% s") name(var1_nosplit, replace) scheme (tab1) note("N= 15455")
            
            #the correct code is this one, note the if. The var1 has 17 levels, but over var2 it only has 14, so the labels were recoded up to 17 and misplaced or disappeared in the graph. Adding the if solves the problem:
            splitvallabels var1 if !missing(var2), length(45) recode local(relabel)
            graph hbar (percent), over(var2, gap(*1)) over(var1,  relabel(`relabel') sort (2) gap(*1) lab(labsize(vsmall)))  blabel(bar, format(%4.1f) size(vsmall))  asyvars title(`: variable label var1') ytitle("% ") name(var1_nomiss, replace) scheme (tab1) note("N= 15455")
            I hope it helps you!

            Comment

            Working...
            X