Announcement

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

  • Labeling the axis

    Hi statalist,

    I'm seeking your advice on how to label the axes. My code below produced the following chart.

    Code:
    graph twoway    (bar beta ind if gru==1 & dep==1 & ind!=0, horizontal barw(0.5)) ///
                        (rcap mxci mnci ind if gru==1 & dep==1 & ind!=0, horizontal col(gs4)) ///
                        (scatter ind beta if gru==1 & dep==1 & ind!=0, ms(none) mlab(lbel) mlabcol(gs4) mlabpos(7))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	27.2 KB
ID:	1778945


    Now then… ideally, I want the y-axis to be labeled the same as the value label of 'ind'. I know that this can be done manually, as shown in the code below:
    Code:
    ylab(1 "I can't" 2 "get my" 3 "head" 4 "around it", noticks angle())
    Is there a smarter or faster way to label the axis? For your information, 'ind' is already labeled.
    Code:
    gen ind = .
            replace ind = 0 if missing(cate)
            replace ind = 1 if cate==0 & ctrl=="ctrl2"
            replace ind = 2 if cate==1 & ctrl=="ctrl2"
            replace ind = 3 if cate==2 & ctrl=="ctrl2"
            replace ind = 4 if cate==3 & ctrl=="ctrl2"
            replace ind = 5 if cate==4 & ctrl=="ctrl2"
            replace ind = 6 if cate==0 & ctrl=="ctrl4"
            replace ind = 7 if cate==1 & ctrl=="ctrl4"
            replace ind = 8 if cate==2 & ctrl=="ctrl4"
            replace ind = 9 if cate==3 & ctrl=="ctrl4"
            replace ind = 10 if cate==4 & ctrl=="ctrl4"
            lab def ind ///
            0 "All firms" ///
            1 "All firms (controlling VAL)" ///
            2 "L VAL" ///
            3 "LM VAL" ///
            4 "UM VAL" ///
            5 "H Val" ///
            6 "All firms (controlling VA)" ///
            7 "L VA" ///
            8 "LM VA" ///
            9 "UM VA" ///
            10 "H VA" ///
            , replace
            lab val ind ind
    Thank you!
    Last edited by Bayu Agnimaruto; 18 Jun 2025, 05:14.

  • #2
    Use ylabel(, valuelabel):
    Code:
    sysuse auto
    label define rep78 0 "All firms" ///
            1 "All firms (controlling VAL)" ///
            2 "L VAL" ///
            3 "LM VAL" ///
            4 "UM VAL" ///
            5 "H Val" ///
            6 "All firms (controlling VA)" ///
            7 "L VA" ///
            8 "LM VA" ///
            9 "UM VA" ///
            10 "H VA"
    label values rep78 rep78
    graph twoway (bar weight rep78, horizontal barw(0.5)) ///
           (rcap price weight rep78, horizontal col(gs4)) ///
           (scatter rep78 weight, ms(none) mlabcol(gs4) mlabpos(7) ylabel(, valuelabel))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	156.1 KB
ID:	1778950

    Comment


    • #3
      Thanks a lot Chen! It works perfectly!

      Comment

      Working...
      X