Announcement

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

  • Drawing a tsset graph with one time x variable and 4 y quantity variables

    Hello Stata Community;

    I hope I could get some help with this, because I don't know where the problem with my data is.
    I do have this data:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9 AgriculturalCampagne long(foin ensilage fourrage_vert_automne fourrage_vert_ete)
    "2005-2006" 645000 465000  1700000  715000
    "2006-2007" 832000 558000  2370000  612000
    "2007-2008" 643590 483180  1874100 1071540
    "2008-2009" 887000 523000  2141000  914000
    "2009-2010" 596200 424100  1618000 1063249
    "2010-2011" 596139 424063  2678569  893305
    "2011-2012" 824912 492456  2128453  719441
    "2012-2013" 655284 487731  2374385  580000
    "2013-2014" 949374 572800  2817101  811000
    "2014-2015" 821000 523000  1700000  639102
    "2015-2016" 680000 466000  1900000  871000
    "2016-2017" 836225 517633  2370245  439000
    "2017-2018" 650000 485000  1700000  421000
    "2018-2019" 990887 490558  1969825  367000
    "2019-2020" 717000 345000  1500000  394000
    "2020-2021" 790000 380000 19000000  368300
    "2021-2022" 765000 407000 19000000  461000
    ""               .      .        .       .
    end
    The first variable is "Yearly Campagne", and the other variable are crop types production. I already inserted the "tsset" command in the first variable since I wanna treat it as a Yearly time variable.

    My goal is to draw a time-line graph showing the Yearly campagne as an x variable (with all the campagnes written on the axis) and as the y variable the 4 crop production, all in one graph with different line color for each crop production.

    I really would appreciate it if I could get some help.

    Thanks.

  • #2
    You certainly need a numeric time variable for things to go well here. Beyond that I suggest logarithmic scale and direct labelling. I am guessing at the units of measurement.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9 AgriculturalCampagne long(foin ensilage fourrage_vert_automne fourrage_vert_ete)
    "2005-2006" 645000 465000  1700000  715000
    "2006-2007" 832000 558000  2370000  612000
    "2007-2008" 643590 483180  1874100 1071540
    "2008-2009" 887000 523000  2141000  914000
    "2009-2010" 596200 424100  1618000 1063249
    "2010-2011" 596139 424063  2678569  893305
    "2011-2012" 824912 492456  2128453  719441
    "2012-2013" 655284 487731  2374385  580000
    "2013-2014" 949374 572800  2817101  811000
    "2014-2015" 821000 523000  1700000  639102
    "2015-2016" 680000 466000  1900000  871000
    "2016-2017" 836225 517633  2370245  439000
    "2017-2018" 650000 485000  1700000  421000
    "2018-2019" 990887 490558  1969825  367000
    "2019-2020" 717000 345000  1500000  394000
    "2020-2021" 790000 380000 19000000  368300
    "2021-2022" 765000 407000 19000000  461000
    ""               .      .        .       .
    end
    
    gen shorter = subinstr(Agri, "-20", "-", .) 
    egen year = group(shorter), label 
    
    local col1 230 159 0
    local col2 86 180 233 
    local col3 0 158 115
    local col4 0 114 178 
    
    local j = 1
    foreach v of var foin-fourrage_vert_ete { 
        if inlist(`j', 1, 2) gen label`j' = "`v'" 
        else {
            local this : subinstr local v "_" " ", all
            gen label`j' = substr("`this'", 10, .) 
        } 
        local call `call' || scatter `v' year if year == 17, ms(none) mlabsize(medium) mlabel(label`j') mlabc("`col`j''") 
        local ++j 
    } 
    
    
    line foin-fourrage_vert_ete year, ///
    xtitle("")  xsc(r(1 21)) xla(1(2)17, labsize(small) valuelabel) ///
    yla(20e6 "20" 10e6 "10" 5e6 "5" 2e6 "2" 1e6 "1" 5e5 "0.5" 2e5 "0.2", ang(h)) ytitle(Area (million ha))  ///
    ysc(log) lc("`col1'" "`col2'" "`col3'" "`col4'") legend(off) `call'
    Click image for larger version

Name:	agri .png
Views:	1
Size:	42.2 KB
ID:	1705077

    Comment


    • #3
      Nick Cox Thanks for the help Well, the unit of those numbers is in Tonnes, yet, I generated the graph and I got a problem with the Y axis, I guess I have to use Thousand Tonnes as a measurment unit, so all those numbers are devided by 1000 and that would help get rid of the unnecessary zeros and make the graph more appealing.
      Still, another problem that I got into, is the fact that the X axis doesn't show the yearly campagnes, it just shows 0 and 5 and 10 and ...

      Is there a solution for all that, please ? Thanks again for the help.

      Comment


      • #4
        Thanks for the information that the units of tonnes. Either way, there is still a puzzle about whether one output really shot up from 1.5 million to 19 million and stayed there. Is that 19000000 really 1900000?

        Otherwise you evidently used different code you're not showing us and report a problem with a time axis and difficulty with really big numbers on the y axis. Both problems are solved in my code, which I therefore recommend. We can't comment on code you don't show us.

        Comment


        • #5
          Nick Cox Ohh Sorry, I actually used the code you've shared with me And I'm actually thinking about ignoring those two numbers in what comes to the value of 19000000, even me I thought those two values were kinda wrong and uncomprehensible, but still, that was the available data.
          Yet, in what comes to the code, I've actually used the code you've shared with me

          Comment


          • #6
            That's fine then. Thanks!

            Comment

            Working...
            X