Announcement

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

  • Value Labels as Legend Names

    How come when we do
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte region int year float cigsale
    1 1970     109.7
    1 1971    117.65
    1 1972   126.025
    1 1973   132.375
    1 1974   138.125
    1 1975   142.225
    1 1976   147.425
    1 1977     148.3
    1 1978   149.975
    1 1979    146.85
    1 1980   148.975
    1 1981   145.925
    1 1982   146.725
    1 1983   142.175
    1 1984   134.425
    1 1985   135.225
    1 1986     129.3
    1 1987    131.45
    1 1988     129.9
    3 1970       123
    3 1971       121
    3 1972     123.5
    3 1973     124.4
    3 1974     126.7
    3 1975     127.1
    3 1976       128
    3 1977     126.4
    3 1978     126.1
    3 1979     121.9
    3 1980     120.2
    3 1981     118.6
    3 1982     115.4
    3 1983     110.8
    3 1984     104.8
    3 1985     102.8
    3 1986      99.7
    3 1987      97.5
    3 1988      90.1
    4 1970 116.51428
    4 1971 118.87143
    4 1972     127.4
    4 1973     127.7
    4 1974 129.52856
    4 1975 131.82857
    4 1976 132.48572
    4 1977 130.92857
    4 1978 129.88571
    4 1979 128.75714
    4 1980 125.92857
    4 1981 126.37143
    4 1982 122.61429
    4 1983 116.57143
    4 1984 107.72857
    4 1985 106.22857
    4 1986 103.48572
    4 1987      99.6
    4 1988  93.58572
    end
    label values region region
    label def region 1 "East South Central", modify
    label def region 3 "California", modify
    label def region 4 "Mountain", modify
    cls
    
    twoway (line cigsale year if region==3, lcolor("12 10 0")) ///
        (line cigsale year if region==1, lcolor(gs13)) ///
        (line cig year if region==4, lcolor(gs14))
    We get three parts of the legend each named "cigsale"? Ideally I'd want them to be named "California", "East South Central" and "Mountain" division. I know we can just rename them with the ordering option, but why doesn't Stata do this automatically? Do I sound stupid for asking this?


    EDIT:
    This is the first time I've ever needed
    Code:
    xtset region year, y
    xtline cigsale if inlist(region,1,3,4), overlay
    I guess this works, but I still don't get why Stata wouldn't do this intrinsically. Presumably there's a better way?
    Last edited by Jared Greathouse; 22 Jul 2022, 10:32.

  • #2
    Because the variable that you are graphing is "cigsale", and the fact that you specify different -if- conditions does not change this. Here is an alternative method that I learned from looking at the various examples by Nick Cox.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte region int year float cigsale
    1 1970     109.7
    1 1971    117.65
    1 1972   126.025
    1 1973   132.375
    1 1974   138.125
    1 1975   142.225
    1 1976   147.425
    1 1977     148.3
    1 1978   149.975
    1 1979    146.85
    1 1980   148.975
    1 1981   145.925
    1 1982   146.725
    1 1983   142.175
    1 1984   134.425
    1 1985   135.225
    1 1986     129.3
    1 1987    131.45
    1 1988     129.9
    3 1970       123
    3 1971       121
    3 1972     123.5
    3 1973     124.4
    3 1974     126.7
    3 1975     127.1
    3 1976       128
    3 1977     126.4
    3 1978     126.1
    3 1979     121.9
    3 1980     120.2
    3 1981     118.6
    3 1982     115.4
    3 1983     110.8
    3 1984     104.8
    3 1985     102.8
    3 1986      99.7
    3 1987      97.5
    3 1988      90.1
    4 1970 116.51428
    4 1971 118.87143
    4 1972     127.4
    4 1973     127.7
    4 1974 129.52856
    4 1975 131.82857
    4 1976 132.48572
    4 1977 130.92857
    4 1978 129.88571
    4 1979 128.75714
    4 1980 125.92857
    4 1981 126.37143
    4 1982 122.61429
    4 1983 116.57143
    4 1984 107.72857
    4 1985 106.22857
    4 1986 103.48572
    4 1987      99.6
    4 1988  93.58572
    end
    label values region region
    label def region 1 "East South Central", modify
    label def region 3 "California", modify
    label def region 4 "Mountain", modify
    
    separate cigsale, by(region) veryshortlabel
    line cigsale? year, lcolor("12 10 0" gs13 gs14)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	31.2 KB
ID:	1674690

    Comment


    • #3
      Fair question
      that is why i have been adding "alegend" automatic legend to my commands, by using the value labels.

      It should be easy to write a command that does the following:

      mline cigsale year , over(region)

      right now you could do that with points
      ssc install mscatter

      mscatter cigsale year, over(region)

      In general, if Stata doesn't do what you want...teach it.

      Edit
      And there you go, Was it Nick?
      Last edited by FernandoRios; 22 Jul 2022, 10:40.

      Comment


      • #4
        Oh... I see the game now, separate must it work with reshape under the hood to do this. Okay this makes sense, thanks so much!

        Comment


        • #5
          Not so. separate and sepscatter which uses it have nothing whatsoever to with reshape.

          The essence is to map one variable to several new ones containing the same information. Stata's general layout -- whether you think it is wide or long -- remains exactly as it was.

          The point to separate -- to justify repetition of information -- is that sometimes having separate variables make things easier, as here

          At first sight mscatter (SSC) seems to have about the same aim as sepscatter (SSC). No doubt there is some offlap.
          Last edited by Nick Cox; 22 Jul 2022, 11:21.

          Comment


          • #6
            I think when I wrote it i had the same goal as you did Nick, except that i wanted to use as many "groups" as possible, allowing for different type of control on colors. (variables and colorpalette)
            On the last version, it now combines some of the features of lpoly, lift, to add more info to the graph.
            All and all, it was really my attempt to make my life easier, without realizing you already had something similar.
            F

            Comment


            • #7
              Fernando RIos Sure; not a problem.

              I am a strong Darwinian on ideas. If an idea is any good, it should get noticed and used. If an idea is any good, people will think their way towards it from different perspectives.

              Except when I am not a strong Darwinian. As Howard Aiken said a while back: Don't worry about people stealing your ideas. If they are any good, you'll have to ram them down their throats. (Not the most elegant choice of words, but the point is that sometimes you have to advertise a little vigorously before people notice.)

              I am reminded obliquely that some time ago someone wrote on Statalist to the effect that all these user-written commands could be great, but someone should try them all and then tell us which are really good. I was left wondering how and how well that would ever work out. .Let's start with commands to map results to reports...

              Comment


              • #8
                Like the difference between collect, esttab, outreg, or any other random thing that can export results to paper? Nick Cox

                Comment

                Working...
                X