Announcement

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

  • possible to replace values with labels on y axis?

    Hello,
    I was unable to resolve my question by searching past posts and consulting Stata help files. I apologize since similar questions have been answered before. I'd like to replace y axis values with labels on a scatter plot, but my code does not replace y values with labels.

    Code:
    sysuse auto.dta, clear
    label define price_axis 0 "$0" 5000 "$5K" 10000 "$10K" 15000 "$15K"
    label values price price_axis
    scatter price mpg, ylabel(0(5000)15000, labels labsize(vsmall) angle(horizontal))
    The labels do not appear:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	39.3 KB
ID:	1385283


    Thank you for your help.
    Last edited by James Sanders; 24 Apr 2017, 16:30.

  • #2
    Unfortunately, I cannot test in Stata whether there is a way out of this.

    That said, under a scatter plot, I gather we should not expect to 'categorize' neither yvar nor xvar.
    Best regards,

    Marcos

    Comment


    • #3
      James: Perhaps try something like this:
      Code:
       scatter price mpg, ylabel(0 "$0" 5000 "$5K" 10000 "$10K" 15000 "$15K")

      Comment


      • #4
        Thank you Marcos and John. The code provided by John indeed does the trick. In searching further, I found the following also works:

        Code:
        sysuse auto.dta, clear
        label define price_axis 0 "$0" 5000 "$5K" 10000 "$10K" 15000 "$15K"
        label values price price_axis
        scatter price mpg, ylabel(0(5000)15000, valuelabel labsize(vsmall) angle(horizontal))


        In sum, the Use value labels option in Stata's point-and-click graphic interface does not appear to be the way to add labels to an axis.
        Click image for larger version

Name:	Screen Shot 2017-04-24 at 6.06.50 PM.png
Views:	1
Size:	56.0 KB
ID:	1385298



        Instead one should use the code provided immediately above or, alternatively, the code previously provided by John.
        Click image for larger version

Name:	Graph2.png
Views:	1
Size:	36.2 KB
ID:	1385299

        Comment


        • #5
          Originally posted by James Sanders View Post
          In sum, the Use value labels option in Stata's point-and-click graphic interface does not appear to be the way to add labels to an axis.
          The GUI worked for me. Did you also change the Axis rule on the Rule menu tab by clicking on the Custom radio button and entering "0(5000)15000" into the Custom rule text box? When doing that and setting up the Labels menu tab just like you show, Stata generates the following line in the Results and Review windows
          Code:
          twoway (scatter price mpg), ylabel(0(5000)15000, labels labsize(vsmall) angle(horizontal) format(%9.0fc) valuelabel)
          (I created the values labels and assigned them to the price variable beforehand.)

          Comment


          • #6
            Thanks for looking at this Joseph. Your method indeed works. I believe I have diagnosed the problem I experienced, but I cannot explain it. When I start the graph with a fit plot instead of a scatter plot, the value labels fail to appear.
            Code:
            sysuse auto.dta, clear
            label define price_axis 0 "$0" 5000 "$5K" 10000 "$10K" 15000 "$15K"
            label values price price_axis
            twoway (qfitci price mpg) (scatter price mpg), ylabel(0(5000)15000, labels labsize(vsmall) angle(horizontal) valuelabel)
            The above code produces the following graph sans labels:
            Click image for larger version

Name:	Graph3.png
Views:	1
Size:	54.7 KB
ID:	1385310



            Conversely, starting the graph with a scatter plot produces the desired value labels.
            Code:
            twoway (scatter price mpg) (qfitci price mpg), ylabel(0(5000)15000, labels labsize(vsmall) angle(horizontal) valuelabel)
            This code produces the following graph with labels:
            Click image for larger version

Name:	Graph4.png
Views:	1
Size:	51.0 KB
ID:	1385311



            A graph that places fitted values/confidence intervals behind observed values is more desirable to my tastes. Perhaps there is a way to have them render in reverse order.

            Comment


            • #7
              Originally posted by James Sanders View Post
              When I start the graph with a fit plot instead of a scatter plot, the value labels fail to appear.

              Conversely, starting the graph with a scatter plot produces the desired value labels.

              A graph that places fitted values/confidence intervals behind observed values is more desirable to my tastes. Perhaps there is a way to have them render in reverse order.
              Seems like a bug. I recommend bringing it to StataCorp's attention.

              In the meantime you can trick Stata into getting you what you want by plotting the scatterplot twice, once in the beginning (to get the value labels on the y-axis) and again after qfitci (to get the symbols in front of the CI band).
              Code:
              graph twoway scatter price mpg, msymbol(none) ylabel(0(5000)15000, labels labsize(vsmall) angle(horizontal) valuelabel) || ///
                  qfitci price mpg || ///
                  scatter price mpg, mcolor(navy) legend(off)

              Comment


              • #8
                There are two distinct issues here.

                First, the labels suboption just confirms that you want to see axis labels. It's not an instruction to use value labels when defined.

                This works and is the simple solution for the problem in #1.

                Code:
                sysuse auto.dta, clear
                label define price_axis 0 "$0" 5000 "$5K" 10000 "$10K" 15000 "$15K"
                label values price price_axis
                scatter price mpg, ylabel(0(5000)15000, valuelabel labsize(vsmall) angle(horizontal))
                Second, even when scatter is used second, the value labels don't appear with code like this.

                Code:
                twoway qfitci price mpg ///
                || scatter price mpg, ylabel(0(5000)15000, labsize(vsmall) angle(horizontal) valuelabel)
                I don't have an explanation for that.
                Last edited by Nick Cox; 25 Apr 2017, 04:07.

                Comment


                • #9
                  Thanks for the clarification Nick and for the helpful tip Joseph. In the absence of a clear explanation for the latter issue, I'll follow Joseph's advice and plot the scatter plot twice.

                  Comment

                  Working...
                  X