Announcement

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

  • Twoway Graph

    I am trying to create a twoway graph using
    Code:
    twoway (line internet year), by(, title(Use of Internet by Region) subtitle(EMDEs (2011, 2014, 2017)) note(Source: International Telecommunication Union)) by(regionwb)
    I get the following graph:
    Click image for larger version

Name:	Graph4.jpg
Views:	1
Size:	81.5 KB
ID:	1686596



    Instead, I would like to create a single graph with several lines (each line representing the regions), something similar to this (instead of the countries, here the regions):
    Click image for larger version

Name:	Rdexp_years.jpg
Views:	1
Size:	71.1 KB
ID:	1686595



    Moreover, I would like to change the names of the y and x axis, but constantly keep getting error messages.

    I would highly appreciate if someone could help me out.

    Many thanks and kind regards,
    Julie
    Last edited by Julie Illoul; 24 Oct 2022, 15:08.

  • #2
    This code is untested, since there's no data example, but this should point you in the right direction

    Code:
    encode regionwb, gen(region_name)
    xtset region_name year
    
    xtline internet, overlay title("Use of Internet by Region") subtitle("EMDEs (2011, 2014, 2017)") note("Source: International Telecommunication Union") xtitle("X-axis Title") ytitle("Y-axis Title")
    You can also consider fabplot (type search fabplot in Stata)

    Code:
    fabplot line internet year, by(regionwb, compact rows(2))
    Last edited by Justin Niakamal; 24 Oct 2022, 17:31.

    Comment


    • #3
      I want to pick up Justin Niakamal 's suggestions and add some small details.

      This dataset echoes your region names and years of data and uses some numbers a little similar to your data.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str26 region int year byte internet
      "East Asia & Pacific"        2011 28
      "East Asia & Pacific"        2014 35
      "East Asia & Pacific"        2017 42
      "Europe and Central Asia"    2011 39
      "Europe and Central Asia"    2014 50
      "Europe and Central Asia"    2017 61
      "Latin America & Caribbean"  2011 34
      "Latin America & Caribbean"  2014 41
      "Latin America & Caribbean"  2017 54
      "Middle East & North Africa" 2011 27
      "Middle East & North Africa" 2014 40
      "Middle East & North Africa" 2017 60
      "South Asia"                 2011 12
      "South Asia"                 2014 18
      "South Asia"                 2017 22
      "Sub-Saharan Africa"         2011 10
      "Sub-Saharan Africa"         2014 16
      "Sub-Saharan Africa"         2017 22
      end
      
      set scheme s1color 
      
      encode region, gen(id)
      xtset id year 
      
      xtline internet, name(G1, replace) xla(2011(3)2017) recast(connected) overlay xtitle("") plot1opts(lc(blue) mc(blue)) 
      
      myaxis better=region, sort(mean internet) descending 
      
      fabplot line internet year, by(better) name(G2, replace) front(connected) frontopts(msize(large) lw(thick)) xla(2011(3)2017) xtitle("")
      fabplot and myaxis are from the Stata Journal.

      Click image for larger version

Name:	ITU_G1.png
Views:	1
Size:	55.0 KB
ID:	1686614
      Click image for larger version

Name:	ITU_G2.png
Views:	1
Size:	64.9 KB
ID:	1686615


      I want to make some suggestions that are variously bland (B) and more contentious (C).

      B1. Use dates 2011 2014 2017 to match data points, rather than relying on Stata's defaults.

      B2. The x axis title "year" or "years" does no harm, but is surely redundant. Readers who needs to be told that 2011 2014 and 2017 are out of their depths.

      B3. Alphabetical order is conventional and an understandable default but surely not better than an ordering based on the data themselves (here the mean, but I would be happy with median, geometric mean, value in 2017, etc., as ordering criterion).

      C1. The first graph is conventional but vastly over-rated as a graph style. The key or legend is at best awkward and at worst likely to mean that many readers go, "Oh, clearly a line graph with a legend, so I will glance at it." Few will be able to memorise the legend and then apply it, and the result is some back and forth which quickly gets tedious. I added the marker symbols so that the reader has more handles than six thin lines in a mix of colours, but it's still hard.

      C2. The front-and-back plot is likely to be seem less familiar. I've sprinkled the examples I've encountered in references in the 2021 Stata Journal article and would be delighted to hear of others. But it allows a connected story to be read from the plots.

      There isn't a perfect graph here -- for example labels for 2017 and 2011 are too close on the bottom -- but the traditional overlay spaghetti plot shouldn't be accepted without demur.



      Comment


      • #4
        Dear Justin and Nick,

        Thank you so much! This helps a lot and I highly appreciate it!

        The front-and-back plot (fabplot) is so much better than what I tried to create. I will definitely be using it. I also replaced the mean with median, as I thought the measure would suit my data better.

        Thank you again!

        Kind regards,
        Julie

        Comment


        • #5
          Thanks for #4.

          Some relevant references:

          SJ-21-2 gr0087 . . Front-and-back plots to ease spaghetti and paella problems
          (help fabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
          Q2/21 SJ 21(2):539--554
          explores front-and-back plots, in which each subset of data
          is shown separately with the other subsets as backdrop

          SJ-19-4 gr0080 . . . . . . Some simple devices to ease the spaghetti problem
          . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
          Q4/19 SJ 19(4):989--1008 (no commands)
          gives guidance on avoiding the spaghetti problem in graphics
          (where multiple time series or other functional traces show
          mostly a tangled mess)

          SJ-10-4 gr0046 . . . . . . . . . . . . . . . Speaking Stata: Graphing subsets
          . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
          Q4/10 SJ 10(4):670--681 (no commands)
          explores graphical comparison of results for two or more
          subsets where each subset is plotted in a separate panel,
          with the rest of the data as a backdrop

          Comment


          • #6
            Dear Nick,

            Thank you very much. I found your article (Front-and-back plots to ease spaghetti and paella problems) yesterday, and it was also very helpful.

            Best,
            Julie

            Comment


            • #7
              Thank you for this discussion!
              I have a similar need, however neither solution works for me
              -Spaghetti plot do not allow to control (format and label etc) individual spaghetti strands or to twoway to superimpose different group of strands
              - fab plot does not allow to plot more than one strand in the foreground, or to plot one by category at a time
              -xtline reduces categories to 16 formats, recycling line styles (?)

              I'm preparing a little dataset to exemplify as per forum guidelines but in the meanwhile, any suggestion?
              In my case spaghetti are a feature, not a porblem. I really need a graph as in Is big tech now just too big to stomach? | Technology sector | The Guardian
              rather than Our house-price forecast expects the global rally to lose steam (economist.com)

              Thanks!

              N

              Comment

              Working...
              X