Announcement

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

  • Percentages

    Hi Statlist,

    I am trying to build a scatterplot and would like to input data in the form of percentages with the format x% both in the labels of the points and the y-axis.
    My code is as follows:

    Code:
    twoway (scatter share_class_two Year, mlabel(share_class_two) xlabel(2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015)
    Data are (example):

    . dataex

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float Year double(recalled_class_two recalled_class_one) float(share_class_two share_class_one)
    2004  38  6 .012850862 .0020290834
    2005 120  2  .04058167 .0006763612
    2006 120  9  .04058167  .003043625
    2007 103  3   .0348326 .0010145417
    2008 102  4  .03449442 .0013527224
    2009 441 10  .14913765  .003381806
    2010 183 10  .06188705  .003381806
    2011 674 21   .2279337  .007101792
    2012 228 15  .07710517  .005072709
    2013 235 20  .07947244  .006763612
    2014 449 27   .1518431  .009130876
    2015 264 19  .08927967  .006425431
    end

    The resulting graph is a scatterplot with y axis labeled in the format 0 .05 .1 .15 and so on until .25. Labels on the points are again in the same format.
    What I would like to do is to change both formats to be 0 5% 10% 15% and so on.


    Many thanks,

    Federico

  • #2
    Dear Federico,

    This requires a little extra coding:

    Code:
    * Create a variable with the required percentage representational value
    gen sc2p=100*round(share_class_two,.01)
    * Create a variable with the required value label (note using usedisplayformat to constrain Stata's perfection)
    tostring sc2p , gen(txt1sc2p) usedisplayformat force
    * Finally, create the marker label
    gen p="%"
    egen txt_sc2p = concat(txt1sc2p p)
    
    * Usually, the scale of an axis is noted in the scale title:
    twoway (scatter share_class_two Year, mlabel(txt_sc2p)  xlabel(2004(1)2015) ///
        ylabel(, angle(0) labsize(*.8)) ymtick(.01(.01).24) ytitle("% Share Class Two"))
    Then you should get:
    Click image for larger version

Name:	Tutorial_Percentage_labels_1.png
Views:	1
Size:	35.7 KB
ID:	1490772

    Code:
    * But, if you have to, this would get what you ask for:
    twoway (scatter share_class_two Year, mlabel(txt_sc2p)  xlabel(2004(1)2015) ///
        ylabel(0 "0%" .05 "5%" .1 "10%" .15 "15%" .2 "20%" .25 "25%" , angle(0) labsize(*.8)) ///
        ymtick(.01(.01).24) ytitle("Share Class Two"))
    The you should get:
    Click image for larger version

Name:	Tutorial_Percentage_labels_2.png
Views:	1
Size:	35.7 KB
ID:	1490773

    Happy graphing!
    http://publicationslist.org/eric.melse

    Comment


    • #3
      Rounding using round(, .01) will not always give you what you want. Occasionally you will get some fluff with far too many decimal places. Rounding to integers is much simpler.

      Here's a tweak of Eric's helpful code. The marker label variable can be created directly with

      Code:
      gen txt_sc2p = string(round(100 * share_class_two, 1)) + "%"

      Comment


      • #4
        Thank you very much for the complete and very useful answers! Both perfectly do the job.
        I am sorry but I find it difficult to implement well structured graphs and would like to learn step by step.

        Thank you again to both,

        Federico

        Comment


        • #5
          You're welcome.

          On the graph design: Much depends on your audience. I think it's fine to put percent in the axis title. Putting it on each axis label and on each data point too seems to me overkill. If readers won't read the axis title (or whatever else you provide as extra text), that's their failing. On the other hand, if you are addressing innumerate readers, you will know what they need or want.

          Comment


          • #6
            Thank you for your opinion. I agree with you on the label on each data point. I will try to be as clear as possible on the text part.

            Thank you again

            Comment

            Working...
            X