Announcement

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

  • Alternate Sci-Notation for Graphs

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int(gdp7 year) double cf_0 byte relative_0
     2284 1960 2344.5752805835496 -30
     2388 1961  2410.555300400853 -29
     2527 1962  2565.170095355413 -28
     2610 1963 2680.1819865730668 -27
     2806 1964 2850.2205099133694 -26
     3005 1965 3049.2355450697996 -25
     3168 1966 3294.6976320281647 -24
     3241 1967  3457.126790346017 -23
     3571 1968  3748.937513372169 -22
     3998 1969 4053.9835620080307 -21
     4367 1970  4313.265847486722 -20
     4686 1971 4643.6328845354155 -19
     5055 1972  5067.659012151861 -18
     5553 1973  5627.864741147243 -17
     6074 1974 6138.4684340114745 -16
     6603 1975  6637.413276120334 -15
     7367 1976  7334.825054463951 -14
     8090 1977  8023.515936373996 -13
     8928 1978   8924.21961297197 -12
    10067 1979   9854.24671087441 -11
    11083 1980 10760.977180535365 -10
    12115 1981 11892.800173885393  -9
    12761 1982 12369.192922761604  -8
    13519 1983  13211.47763314891  -7
    14481 1984 14423.075860809462  -6
    15291 1985  15282.67629347721  -5
    15998 1986 15993.358177955923  -4
    16679 1987 16851.079161089652  -3
    17786 1988 18042.316421826057  -2
    18994 1989  19248.25044872261  -1
    20465 1990  20254.86643544301   0
    21602 1991 20818.459230348755   1
    22154 1992 21654.308331137978   2
    21878 1993 22290.622004675584   3
    22371 1994 23394.456620703248   4
    23035 1995  24245.63418341099   5
    23742 1996 25206.203243412278   6
    24156 1997 26368.449542536662   7
    24931 1998 27441.339580822205   8
    25755 1999 28673.510595792643   9
    26943 2000 30217.040951444593  10
    27449 2001 31105.584968837804  11
    28348 2002  32076.66496251002  12
    28855 2003  33222.11056743127  13
    end
    format %ty year
    label var gdp7 "Real West Germany" 
    label var cf_0 "SCUL West Germany, 1990" 
    cls
    tsset year, y
    
    twoway (tsline gdp7, lcolor(black) lwidth(thick)) (tsline cf_0, lcolor(black) lwidth(medthin) lpattern(tight_dot)), ylabel(, format(%1.0e)) legend(region(fcolor(none)) position(4) ring(0))
    This may seems like a super pedantic question, but I don't like the way Stata's scientific notation is represented in graphics. Is there a way for Stata to represent scientific notation as, for example, simply "3.0" and then at the top of the y axis have something like 10 x 4?


    I don't know, I'm big on aesthetics, and I like making my graphics look clean and not awkward.

  • #2
    You could use mylabels from SSC here. mylabels has been lurking on SSC since 2003 but it is written up in Stata Journal 22(4) in press. Some projects move more slowly than others.

    A benefit of letting this command marinate for a while is that it has acquired extra options given whatever reasonable desires occur to me or to other people.

    In reverse,

    On 15 May 2022 https://www.statalist.org/forums/for...dable-from-ssc announced firstonly and lastonly options tuning prefix and suffix options.

    29 September 2016 https://www.statalist.org/forums/for...updated-on-ssc announced an update

    and so on back to

    5 May 2003 https://www.stata.com/statalist/arch.../msg00084.html announcing the command on SSC.

    I only did that to amuse myself. Little things....

    Jared's variables lead to default y axis labels that are some version of 0 10000 20000 30000 40000. In fact I would probably choose those labels unless the units made a scaling by 1000 appear sensible in which case 0 10 20 30 40 would be even better.

    Powers of 10 can be shown as below in the first (left-hand) graph and an alternative in the second (right-hand) graph is to use an abbreviation such as k (indicating thousands) (or the text 000 or the text thousands or ...). The specification lastonly is naturally not at all compulsory.

    What is crucial here is that neither desire can be met by a change of display format. You must use text manipulation on the labels, or more precisely the numbers that are to be represented by labels, but you may use SMCL.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int(gdp7 year) double cf_0 byte relative_0
     2284 1960 2344.5752805835496 -30
     2388 1961  2410.555300400853 -29
     2527 1962  2565.170095355413 -28
     2610 1963 2680.1819865730668 -27
     2806 1964 2850.2205099133694 -26
     3005 1965 3049.2355450697996 -25
     3168 1966 3294.6976320281647 -24
     3241 1967  3457.126790346017 -23
     3571 1968  3748.937513372169 -22
     3998 1969 4053.9835620080307 -21
     4367 1970  4313.265847486722 -20
     4686 1971 4643.6328845354155 -19
     5055 1972  5067.659012151861 -18
     5553 1973  5627.864741147243 -17
     6074 1974 6138.4684340114745 -16
     6603 1975  6637.413276120334 -15
     7367 1976  7334.825054463951 -14
     8090 1977  8023.515936373996 -13
     8928 1978   8924.21961297197 -12
    10067 1979   9854.24671087441 -11
    11083 1980 10760.977180535365 -10
    12115 1981 11892.800173885393  -9
    12761 1982 12369.192922761604  -8
    13519 1983  13211.47763314891  -7
    14481 1984 14423.075860809462  -6
    15291 1985  15282.67629347721  -5
    15998 1986 15993.358177955923  -4
    16679 1987 16851.079161089652  -3
    17786 1988 18042.316421826057  -2
    18994 1989  19248.25044872261  -1
    20465 1990  20254.86643544301   0
    21602 1991 20818.459230348755   1
    22154 1992 21654.308331137978   2
    21878 1993 22290.622004675584   3
    22371 1994 23394.456620703248   4
    23035 1995  24245.63418341099   5
    23742 1996 25206.203243412278   6
    24156 1997 26368.449542536662   7
    24931 1998 27441.339580822205   8
    25755 1999 28673.510595792643   9
    26943 2000 30217.040951444593  10
    27449 2001 31105.584968837804  11
    28348 2002  32076.66496251002  12
    28855 2003  33222.11056743127  13
    end
    format %ty year
    label var gdp7 "Real West Germany" 
    label var cf_0 "SCUL West Germany, 1990" 
    cls
    tsset year, y
    set scheme s1color 
    local opts ytitle(Something explanatory) xtitle("") legend(pos(11) col(1))
    
    mylabels 0 1 2 3 4, myscale(@*1e4) suffix("{&dot}10{sup:4}") local(yla)
    
    twoway (tsline gdp7, lcolor(black) lwidth(thick)) (tsline cf_0, lcolor(black) lwidth(medthin) lpattern(tight_dot)), ylabel(`yla', ang(h)) legend(region(fcolor(none)) position(4) ring(0)) `opts' name(G1, replace)
    
    mylabels 0 10 20 30 40, myscale(@*1000) suffix("k") lastonly local(yla)
    
    twoway (tsline gdp7, lcolor(black) lwidth(thick)) (tsline cf_0, lcolor(black) lwidth(medthin) lpattern(tight_dot)), ylabel(`yla', ang(h)) legend(region(fcolor(none)) position(4) ring(0)) `opts' name(G2, replace)
    
    graph combine G1 G2


    Click image for larger version

Name:	jared.png
Views:	1
Size:	33.9 KB
ID:	1678320

    Comment


    • #3
      Thank you Nick. This worked. It's interesting though that StataCorp hasn't done something like this already yet in canon-Stata.
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input int gdp7 double(year cf_0 cf_adh)
       2284 1960 2344.5752805835496           2231.817
       2388 1961  2410.555300400853           2332.624
       2527 1962  2565.170095355413            2457.04
       2610 1963 2680.1819865730668           2578.499
       2806 1964 2850.2205099133694           2738.749
       3005 1965 3049.2355450697996 2883.7169999999996
       3168 1966 3294.6976320281647           3110.981
       3241 1967  3457.126790346017 3275.8700000000003
       3571 1968  3748.937513372169 3569.7360000000003
       3998 1969 4053.9835620080307           3926.556
       4367 1970  4313.265847486722           4371.962
       4686 1971 4643.6328845354155  4730.762000000001
       5055 1972  5067.659012151861           5140.733
       5553 1973  5627.864741147243            5661.93
       6074 1974 6138.4684340114745           6274.634
       6603 1975  6637.413276120334  6756.467000000001
       7367 1976  7334.825054463951  7382.656000000001
       8090 1977  8023.515936373996  8110.109000000001
       8928 1978   8924.21961297197  8812.389000000001
      10067 1979   9854.24671087441  9924.780999999999
      11083 1980 10760.977180535365          11011.972
      12115 1981 11892.800173885393          12134.579
      12761 1982 12369.192922761604 12796.325999999997
      13519 1983  13211.47763314891          13638.753
      14481 1984 14423.075860809462          14445.123
      15291 1985  15282.67629347721          15318.459
      15998 1986 15993.358177955923           15967.85
      16679 1987 16851.079161089652          16719.378
      17786 1988 18042.316421826057          17832.224
      18994 1989  19248.25044872261          19039.058
      20465 1990  20254.86643544301 20167.095999999998
      21602 1991 20818.459230348755 20949.071000000004
      22154 1992 21654.308331137978 21688.791999999998
      21878 1993 22290.622004675584          22153.965
      22371 1994 23394.456620703248          23100.324
      23035 1995  24245.63418341099          23947.265
      23742 1996 25206.203243412278 24808.960999999996
      24156 1997 26368.449542536662 25657.530000000002
      24931 1998 27441.339580822205 26631.412999999997
      25755 1999 28673.510595792643 27709.765000000003
      26943 2000 30217.040951444593          29318.362
      27449 2001 31105.584968837804          29932.662
      28348 2002  32076.66496251002 31085.891000000003
      28855 2003  33222.11056743127 32032.738999999998
      end
      format %ty year
      
      tsset year, y
      label var gdp7 "Real West Germany" 
      label var year "Year" 
      label var cf_0 "SCUL West Germany, 1990" 
      label var cf_adh "ADH, 1990" 
      
      labvars cf_adh year "ADH, 1990" "Year"
      
      mylabels 0 1 2 3 4, myscale(@*1e4) suffix("{sup:10×4}") lastonly local(yla)
      
      cls
      twoway (tsline gdp, lcolor(black) lwidth(thick)) ///
      (tsline cf_adh if year >= 1990, lcolor(green) lpat(dash) lwidth(medthick)) ///
      (tsline cf_0 if year >= 1990, lcolor(red) lpat(dash) lwidth(medthick)), ///
       legend(pos(9) ring(0) ///
       order(1 "Observed West Germany" 2 "ADH West Germany" 3 "SCUL West Germany") ///
       fcolor(white) region(fcolor(none))) ///
       tline(1990, lcol("52 44 44") lpat(dash) lwidth(thin)) ///
       yti("GDP per Capita") ti("In-Time Placebos") ylab(`yla')  //

      Comment

      Working...
      X