Announcement

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

  • How to show observation number in a tw line (data label)

    I have a pretty simple code for generating graphs for a list of regions. But I would like to explicit the number of each observation inside the graph (near the line). Or maybe just the more recent number, the max and min. How can I do that? In order to make myself clear, I want to "use" some similar code to the data label option in Excel.

    Code:
    levelsof region, local(levels)
    foreach l of local levels {
    tw line cont anotrim_ativ if region == "`l'", /// 
        ytitle("NÂș of firms") xtitle("Quarter/year") ///
        lpattern(solid) lwidth(thick medthick) lcolor(navy) ///
        title("Number of firms by quarter - `l'")
    }
    Thanks

  • #2
    Sorry, but my experience with MS Excel is dominated by getting data out of there into Stata, followed by a swift exit. More generally what is familiar to frequent Excel users is often unfamiliar to the most active people here, just as you couldn't accurately assume Stata familiarity among experts in an Excel forum (or so I guess).

    Others may well be able to advise much better.

    But the observation number can be put into a variable directly

    Code:
    gen long obsno = _n
    and used in any graph. Note that it won't be changed automatically if the sort order of the data changes.

    There is no data example behind your code and I can't follow the rest of what you want, but that may help a little.

    Comment


    • #3
      Yeah, I know excel is not very useful, I just said it like an analogy, so people could understand what I want. I am sorry. I appreciate your answer, but is not it. I want to explicit the value of each observation from the data in the graph (not just the line, but the value that the line represents)

      Supopse you have a database like:
      year x
      2019 12
      2020 13
      2021 16
      2022 14

      then I want a to show the values of varaible x explicitly, inside the graph/ together withe the line. I wanna show explicit "14" for 2022 etc

      For R users, we can do that by geom_text. I leave an image below, from a question related to mine, but for R ( in an stackoverflow: https://stackoverflow.com/questions/...ne-in-ggplot-2)



      Comment


      • #4
        Thanks for the detail, which makes matters clear. This is Stata terminology biting. you briefly. For example, in Stata documentation you will find statements like

        _n contains the number of the current observation.
        because in Stata each observation is a case, record or row in the dataset.

        You want to see the value of each variable as text, which makes perfect sense.

        Run this script for some details of technique.

        Code:
        clear
        input year x
        2019 12
        2020 13
        2021 16
        2022 14
        end
        
        set scheme s1color
        
        twoway bar x year, base(0) barw(0.8) fcolor(blue*0.2) lcolor(blue) || scatter x year, ms(none) mlabel(x) mlabpos(12) legend(off) yla(0(4)16) ysc(r(0 18)) name(G1, replace)
        
        gen where = 2
        
        twoway bar x year, base(0) barw(0.8) fcolor(blue*0.2) lcolor(blue) || scatter where year, ms(none) mlabel(x) mlabpos(0) ytitle(x) yla(0(4)16) ysc(r(0 18)) legend(off) name(G2, replace)
        https://stackoverflow.com/users/1820446/nick-cox

        Comment


        • #5
          Thank you very much, Nick. Your code works fine. I could also look for more details in this graph options ms(), mlabel() which I didnt know about.

          Comment

          Working...
          X