Announcement

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

  • Graphing Over-Time Trajectories without Excluding Cases that Have Only a Single Data Point

    Dear Listserv—

    I’m using Stata 13. My cases are country-years nested within countries. My number of observations (or country-years) per country varies between one and eight. I’m trying to create a figure that displays growth trajectories for a dozen or so countries (where year is on the X axis and average attitudes are on the Y axis). I want to be able to create a figure where: data points for countries with two or more observations are connected up but where the results for countries with a single observation are also displayed as a single data point, or dot. Can anyone suggest how to do this?

    FYI, I’ve tried the following two approaches to this task, but both have problems.

    (1) I tried using the following code, but the problem was that it connected some observations from different countries together: graph twoway (line hojust year if mena==1 & year>1988, connect(ascending)), xtitle(Year) ytitle(Attitudes) title(Growth Curves in MENA Region)

    (2) Then I tried using this code, but the problem was that countries that had only one observation were excluded from the figure. (I want these countries to appear as a single data point in the figure.) xtline hojust if mena==1 & hojust~=. & sample1==1, overlay xtitle(Year) ytitle(Attitudes) title(Growth Curves in MENA Region) legend(off) yscale(range(1 8)) ylabel(1(1)8) xscale(range(1980 2014) noextend) xlabel(1980(10)2014)

    Thanks!

    Louisa

  • #2
    Presumably there is some variable specifying countries. Let's guess at country. Then this seems consistent with what you want.

    Code:
    preserve
    
    keep if mena == 1 & year > 1988
    keep if hojust < .
    bysort country (year) : gen ncount = _N
    
    line hojust year, connect(L) xtitle(Year) ytitle(Attitudes) title(Growth Curves in MENA Region) || scatter hojust year if ncount == 1
    
    restore
    I guess you need to work a little on the legend.

    Detail: Do your readers really need that axis title Year? They should be smart enough to understand 1989 1994 ... or whatever it is. xtitle("") with a year axis just clears away a little unnecessary clutter.

    Comment


    • #3
      Thanks so much Nick!! This is going to be very helpful for an article I want to submit.

      (And, good point about Year. I'll try the graph without that label.)

      Comment


      • #4
        With perhaps 20 countries here, that's a recipe for spaghetti. For another approach, see http://www.statalist.org/forums/foru...ailable-on-ssc

        Here's an updated list of references to (essentially) that approach. More references most welcome.

        Cox, N.J. 2010. Graphing subsets. Stata Journal 10: 670-681.

        Knaflic, C.N. 2015. Storytelling with Data: A Data Visualization Guide
        for Business Professionals. Hoboken, NJ: Wiley.

        Koenker, R. 2005. Quantile Regression. Cambridge: Cambridge University
        Press. See pp.12-13.

        Schwabish, J.A. 2014. An economist's guide to visualizing data. Journal
        of Economic Perspectives 28: 209-234.

        Unwin, A. 2015. Graphical Data Analysis with R. Boca Raton, FL: CRC
        Press.

        Wallgren, A., B. Wallgren, R. Persson, U. Jorner, and J.-A. Haaland.
        1996. Graphing Statistics and Data: Creating Better Charts. Newbury
        Park, CA: Sage.


        Comment


        • #5
          Hi -- And thanks very much Nick! I wanted to reported back with the solution I ended up going with.

          (Recall, the problem was I wanted a graph where repeated observations would be connected up, but that would also display single observations as a dot.)

          The best solution ended up being to work on the graphics or design. I combined the xtline command with the scatter command using "addplot." Then I manipulated the design so that the output of the scatter command looked the same as the output from xtline. (So, both the xtline output and the scatter output were made to be the same grey color, called gs10. I didn't want the xtline command to result in dotted lines, so I made them solid lines. I had 20 countries, which is why I had to provide instructions for plots 1 through 20. And I manipulated the shape and size of the dots output by the "scatter" command. I also had to set the overall graphic "scheme" to be s2mono.)

          Also Note: Maybe because of the unbalanced nature of my data, the xtline command was essential here. The "line" command kept on connecting up the wrong cases. You might notice that I added in the "line" command as a third element in this graph, but this was so I could add in a superimposed growth trajectory line, which I set to be black (gs0), rather than grey.

          xtline hojust if musmaj==1 & hojust~=., overlay xtitle(Year) ytitle(Acceptance of Homosexuality) title(Muslim World) ///
          legend(off) scheme(s2mono) ///
          plot1(lc(gs10) lp(solid)) plot2(lc(gs10) lp(solid)) plot3(lc(gs10) lp(solid)) plot4(lc(gs10) lp(solid)) plot5(lc(gs10) lp(solid)) ///
          plot6(lc(gs10) lp(solid)) plot7(lc(gs10) lp(solid)) plot8(lc(gs10) lp(solid)) ///
          plot9(lc(gs10) lp(solid)) plot10(lc(gs10) lp(solid)) plot11(lc(gs10) lp(solid)) plot12(lc(gs10) lp(solid)) ///
          plot13(lc(gs10) lp(solid)) plot14(lc(gs10) lp(solid)) plot15(lc(gs10) lp(solid)) plot16(lc(gs10) lp(solid)) ///
          plot17(lc(gs10) lp(solid)) plot18(lc(gs10) lp(solid)) plot19(lc(gs10) lp(solid)) plot20(lc(gs10) lp(solid)) ///
          yscale(range(1 8)) ylabel(1(1)8) ///
          xscale(range(1980 2012) noextend) xlabel(1980(10)2012) ///
          addplot(scatter hojust year if musmaj==1, msize(medium) mcolor(gs10) msymbol(O) || ///
          line hatmusmaj year if year>1989 & year<2013, lcolor(gs0) lwidth(medthick))



          Louisa

          Comment


          • #6
            Thanks for reporting back. The final graph would aid understanding. The data themselves would allow experiments. All you need do to get a readable dataset is

            Code:
            ssc inst dataex 
            dataex year hojust if musmaj==1 & hojust~=.
            except that your country variable name should also appear after dataex

            Comment

            Working...
            X