Announcement

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

  • Graphs in panel data

    Dear Nick,



    When I do: xtline MSMEemployment Uncertainty if countrygroup == 6

    All the six countries within one group will appear in separate graphs. When I add overlay after the comma, Stata says that overlay option may not be used with multible variables, becasue I am plotting two variable for each country.

    How can I plot two variables for each country within one group and put it in one graph?

    I would appreciate much your response,

    Thank you,


  • #2
    Consider this example:

    Code:
    webuse grunfeld
    xtline mvalue invest if inrange(company, 1, 6)
    sort company year
    line mvalue invest year if inrange(company, 1, 6) , c(L L)

    Comment


    • #3
      Dear Nick,

      Thank you for the response. I need to clarify what is inrange here? the name of a group? or it is a command as well

      Comment


      • #4
        It's a function, not a command. Note that a search would tell you that.

        Code:
        . search inrange 
        
        
        
        [FN]    Programming functions . . . . . . . . . . . . .  Programming functions
                (help programming functions)
        
        [FN]    Programming functions . . . . . . . . . inrange() programming function
                (help inrange())
        
        SJ-7-4  dm0033  . . . . . . Speaking Stata: Counting groups, especially panels
                . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                Q4/07   SJ 7(4):571--581                                 (no commands)
                discusses how to count panels through reduction commands
                or through tabulation commands and how to overcome
                problems that do not yield easily to these approaches
        
        SJ-6-4  dm0026  . . . . . . Stata tip 39: In a list or out? In a range or out?
                . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                Q4/06   SJ 6(4):593--595                                 (no commands)
                tip for use of inlist() and inrange()

        Comment


        • #5
          Thank you!

          Comment


          • #6

            Dear Nick, I want to do the same thing. I have a panel data of 3 stock index and I want to plot for each index. When I use your code sort company year line mvalue invest year if inrange(company, 1, 6) , c(L L)) it works but I can't differenciate the 3 series. So can I add something to this code to plot the mvalue of each company in a different color please ? I would appreciate much your response, Thank you, Emna

            Comment


            • #7
              I don't know why this thread started with addressing me; it's not reserved in any such sense.

              For 3 series, xtline, overlay can work fine. Here is an example you can try.

              Code:
              webuse grunfeld, clear
              xtset company year
              set scheme s1color 
              
              * these are options common to both example commands 
              local myopts overlay ysc(log) yla(20 50 100 200 500 1000, ang(h)) xtitle("") 
              
              xtline invest if inlist(company, 1, 2, 3) , `myopts' legend(ring(0) pos(5) col(1) order(1 "1" 2 "2" 3 "3")) name(G1) 
              
              xtline invest if inlist(company, 1, 2, 3) , `myopts' legend(off)  ///
              addplot(scatter invest year if inlist(company, 1, 2, 3) & year == 1954, ms(none) mla(company) mlabsize(medium)) name(G2)
              1. A previous xtset is required for xtline.

              2. With many time series logarithmic scale is a good idea, in which case you need to take care of the axis labels yourself. For much more detail see
              https://journals.sagepub.com/doi/10....867X1801800116 This may not apply to your data.

              3. Whatever your school teachers may have told you about explaining what goes on each axis, an x-axis title such as year or date is usually unnecessary for this kind of graph. I removed it here.

              4. A problem with this kind of graph is that you need a legend, or something equivalent, but the legend or its alternative can take up a great deal of valuable real estate. Here I trimmed the default text "company 1" etc down to "1" etc. An alternative is trailing end labels, used in the second graph. That works best if identifiers are short, or can be made short. Labels at the start of each series may also help.

              5. Choose your own preferred scheme.

              See also e.g. https://www.statalist.org/forums/for...ailable-on-ssc

              Click image for larger version

Name:	emna1.png
Views:	1
Size:	29.5 KB
ID:	1492290



              Click image for larger version

Name:	emna2.png
Views:	1
Size:	29.2 KB
ID:	1492291

              Comment


              • #8
                Hi all,

                I'm struggling with finding the right command for overlaying my line graphs. I'm working with panel data, and the graph I wish to create is one line graph showing average precipitation levels separately for each region I'm working with.

                So far I've tried:
                Code:
                egen regionyear = group(region year)
                egen MEANregionyear=mean(rainfall), by(regionyear)
                tabstat MEANregionyear, stats(count mean) by(regionyear)
                separate MEANregionyear, by(region)
                twoway line MEANregionyear year, by(region)
                But I do not know how to combine the graphs into one.
                Will it be easier using xtline, since I'm working with panel data?

                Thank you so much for your help in advance.

                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input float rainfallmm double year long region
                 27.25257 1988 5
                 26.55535 1989 5
                30.222525 1990 5
                 36.28749 1991 5
                 34.01302 1992 5
                26.423775 1993 5
                 28.51853 1994 5
                25.074846 1995 5
                26.750185 1996 5
                27.604815 1997 5
                 29.87177 1998 5
                 24.60539 1999 5
                16.817682 2000 5
                15.403734 2001 5
                 24.32237 2002 5
                 27.39291 2003 5
                 24.06963 2004 5
                 29.60997 2005 5
                 27.12046 2006 5
                 25.32968 2007 5
                21.505365 2008 5
                31.106476 2009 5
                22.452065 2010 5
                 25.53845 2011 5
                 31.52742 2012 5
                 28.91377 2013 5
                25.967264 2014 5
                33.449856 2015 5
                28.248095 2016 5
                        . 2017 5
                        . 2018 5
                        . 2019 .
                66.524025 1988 2
                 63.91075 1989 2
                 67.68621 1990 2
                 76.48907 1991 2
                 66.47468 1992 2
                71.772896 1993 2
                 83.85102 1994 2
                 89.73432 1995 2
                 95.78157 1996 2
                 70.55569 1997 2
                 80.19603 1998 2
                 88.70529 1999 2
                 62.89793 2000 2
                 64.84759 2001 2
                 87.40131 2002 2
                 79.38502 2003 2
                 86.85091 2004 2
                 84.01238 2005 2
                 81.72401 2006 2
                 82.57442 2007 2
                 81.57327 2008 2
                105.86436 2009 2
                107.84811 2010 2
                 59.83802 2011 2
                 81.53146 2012 2
                 83.96725 2013 2
                 106.4032 2014 2
                 78.13724 2015 2
                 91.31202 2016 2
                        . 2017 2
                        . 2018 2
                274.34576 1988 1
                 240.0255 1989 1
                198.81413 1990 1
                264.01498 1991 1
                  268.401 1992 1
                 252.2328 1993 1
                300.87372 1994 1
                 248.7933 1995 1
                 271.2207 1996 1
                209.18135 1997 1
                113.25059 1998 1
                 237.8423 1999 1
                246.33046 2000 1
                206.23317 2001 1
                 234.2887 2002 1
                248.40926 2003 1
                191.13663 2004 1
                 299.0641 2005 1
                 315.7864 2006 1
                 305.3027 2007 1
                 249.6262 2008 1
                 258.4567 2009 1
                 266.5146 2010 1
                220.74986 2011 1
                246.84955 2012 1
                257.90283 2013 1
                249.95905 2014 1
                303.85583 2015 1
                 217.0324 2016 1
                        . 2017 1
                        . 2018 1
                        . 1999 .
                        . 2017 .
                232.89783 1988 3
                237.58434 1989 3
                212.72667 1990 3
                 179.2526 1991 3
                end
                format %ty year
                label values region region_numeric
                label def region_numeric 1 "East Asia & Pacific", modify
                label def region_numeric 2 "Europe & Central Asia", modify
                label def region_numeric 3 "Latin America & Caribbean", modify
                label def region_numeric 5 "South Asia", modify

                Comment


                • #9
                  Code:
                  preserve 
                  collapse (mean) rainfallmm, by(region year)
                  xtset region year
                  xtline rainfallmm, overlay
                  restore
                  Nick Cox shows how you can have "trailing end labels" in place of a legend in #7 if you prefer that.

                  Comment


                  • #10
                    Thank you so much Andrew!

                    Comment

                    Working...
                    X