Announcement

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

  • -Twoway- : using string variable values to display labels when that string variable that has been -encoded-

    Hello,
    In the following code example, I encode a string variable (in this case the variable make from auto.dta), so that I can use it in a -twoway- graph. However, I would like xlabel in my graph to display the original string variable values. Is there a way to do this?
    In my example the encoded variable is makenum=1 where make=="Chev. Chevette".
    I would like the graph xlabel to display the string var rather than the encoded numeric var.

    I have tried using Nick Cox's -labmask- but perhaps that is not the correct approach.

    Code:
    sysuse auto, clear
        sample 8
                encode make, gen(makenum) label(makenum)
                tab make
                sort make
                bysort foreign: egen trunkmin = min(trunk)
                bysort foreign: egen trunkmax = max(trunk)            
                bysort foreign: egen trunkmedi = median(trunk)
                format trunkmedi %9.2g
                l make trunkmin trunkmedi trunkmax  
                labmask makenum, val(make)
                lab var makenum "Make"
                label define labtrunkmin "Trunk size"
                label variable trunkmedi "Median"
                label variable trunkmin "Range"
                label variable trunkmax "Trunk size"
              
                twoway (rcap trunkmax trunkmin makenum, sort msize(medium)), ///
                    title(Trunk size:) ///
                    subtitle(Range & Median) ///
                    legend(pos(5) ring(0) col(1)) ///
                    scheme(sj) ///
                    xlabel(#6, angle(thirty_five)) ///
                    ylabel(0(4)20, format(%9.0f)) ///
                    xtitle("Make") ///
                    ytitle("Trunk size (cu ft)") ///
                    name(trunkmedi, replace) legend() || ///
                scatter trunkmedi makenum

  • #2
    Thanks for the clear question. In what follows I have

    1. commented out

    Code:
     
     label define labtrunkmin "Trunk size"
    which is illegal.

    2. removed the labmask call. I like labmask, but you don't need it here. encode has already produced value labels.

    3. inserted the valuelabel suboption. If you want to see value labels, you have to ask for them with this command. That is the crucial detail.

    Code:
    sysuse auto, clear
        sample 8
                encode make, gen(makenum) label(makenum)
                tab make
                sort make
                bysort foreign: egen trunkmin = min(trunk)
                bysort foreign: egen trunkmax = max(trunk)            
                bysort foreign: egen trunkmedi = median(trunk)
                format trunkmedi %9.2g
                l make trunkmin trunkmedi trunkmax  
                * labmask makenum, val(make)
                lab var makenum "Make"
                * label define labtrunkmin "Trunk size"
                label variable trunkmedi "Median"
                label variable trunkmin "Range"
                label variable trunkmax "Trunk size"
              
                twoway (rcap trunkmax trunkmin makenum, sort msize(medium)), ///
                    title(Trunk size:) ///
                    subtitle(Range & Median) ///
                    legend(pos(5) ring(0) col(1)) ///
                    scheme(sj) ///
                    xlabel(#6, valuelabel angle(thirty_five)) ///
                    ylabel(0(4)20, format(%9.0f)) ///
                    xtitle("Make") ///
                    ytitle("Trunk size (cu ft)") ///
                    name(trunkmedi, replace) legend() || ///
                scatter trunkmedi makenum

    Comment


    • #3
      Thank you for very clear and effective recommendations. I realized later that my design of horizontally oriented legend rcap symbol conflicted visually with the vertically oriented rcap data lines.
      So, I reoriented the graph to display horizontal rcap bars (complete code is below), which led to two questions:
      1. Although I used -gsort-, how can I get the new Yvar <Make> to be sorted alphabetically from top to bottom?
      2. I use the statement
      Code:
      label variable trunkmedi "Median"
      but for some reason the legend displays "Make" rather than "Median". Is it possible to switch that?

      Code:
      sysuse auto, clear
          set seed 12358
          sample 8
                  encode make, gen(makenum) label(makenum)
                  tab make
                  sort make
                  bysort foreign: egen trunkmin = min(trunk)
                  bysort foreign: egen trunkmax = max(trunk)            
                  bysort foreign: egen trunkmedi = median(trunk)
                  format trunkmedi %9.2g
                  l make trunkmin trunkmedi trunkmax  
                  * labmask makenum, val(make)
                  lab var makenum "Make"
                  * label define labtrunkmin "Trunk size"
                  label variable trunkmedi "Median"
                  label variable trunkmin "Range"
                  label variable trunkmax "Trunk size"
      
                  gsort -makenum
                  twoway (rcap trunkmax trunkmin makenum, horizontal msize(medium)), ///
                      title(Trunk size:) ///
                      subtitle(Range & Median) ///
                      legend(pos(6) ring(1) col(1)) ///
                      scheme(sj) ///
                      ylabel(#6, valuelabel angle(zero)) ///
                      xlabel(0(4)20, format(%9.0f)) ///
                      xtitle("Trunk size (cu ft)") ///
                      ytitle("Make") ///
                      name(trunkmedi, replace) legend() || ///
                  scatter makenum trunkmedi

      Comment


      • #4
        1.

        Code:
        ysc(reverse)
        2. The first "thing" (I have a fairly rich vocabulary, but that word seems as good as any other) being plotted is the capped bar; the second "thing" being plotted is makenum, which has the variable label Make by courtesy of the encode. I'd tend personally in such cases to zap the legend with legend(off).

        More personal taste, but where categories are shown, I tend to lose the ticks on the axis. If that brings the category names too close to the axis, a trick is tlcolor(none) If the tick line colo[u]r is none, then the tick is still there, just invisible as such.

        Code:
        yla(, tlc(none))

        Comment


        • #5
          Thanks for your very helpful suggestions Nick, on finessing the graph.
          I was able to resolve the legend display issue by using a trick: labeling the makenum variable with "Median".

          Code:
          sysuse auto, clear
              set seed 12358
              sample 8
                      encode make, gen(makenum) label(makenum)
                      tab make
                      sort make
                      bysort foreign: egen trunkmin = min(trunk)
                      bysort foreign: egen trunkmax = max(trunk)            
                      bysort foreign: egen trunkmedi = median(trunk)
                      format trunkmedi %9.2g
                      l make trunkmin trunkmedi trunkmax  
                      * labmask makenum, val(make)
                      lab var makenum "Median"
                      * label define labtrunkmin "Trunk size"
                      label variable trunkmedi "Median"
                      label variable trunkmin "Range"
                      label variable trunkmax "Trunk size"
          
                      twoway (rcap trunkmax trunkmin makenum, horizontal msize(medium)), ///
                          title(Trunk size:) ///
                          subtitle(Range & Median) ///
                          legend(pos(6) ring(1) col(1)) ///
                          xlabel(0(4)20, format(%9.0f)) ///
                          xtitle("Trunk size (cu ft)") ///
                          ylabel(#6, valuelabel angle(zero) tlc(none)) ///
                          ysc(reverse) ///
                          ytitle("") ///
                          name(trunkmedi, replace) || ///
                      scatter makenum trunkmedi , legend(pos(6) ring(1) col(1))

          Comment

          Working...
          X