Announcement

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

  • Removing the vertical space between multiple graphs

    Dear Community,

    I am a Stata 17 user. I plot line graphs for 21 countries covering 2000-2018. In each graph, there are two lines named carbon and fourier. Below is an example of my dataset:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str34 country int year float(carbon id fourier)
    "Australia" 2000   .11910188 1    .12940282
    "Australia" 2001   .11729448 1    .12833495
    "Australia" 2002   .13385691 1    .12770738
    "Australia" 2003   .14196791 1    .12795834
    "Australia" 2004   .14345711 1    .12943089
    "Australia" 2005   .14281034 1    .13233568
    "Australia" 2006   .12774472 1    .13672818
    "Australia" 2007   .12869008 1    .14250264
    "Australia" 2008   .13165198 1    .14940353
    "Australia" 2009   .17034286 1     .1570533
    "Australia" 2010      .16453 1    .16499317
    "Australia" 2011   .16648103 1    .17273302
    "Australia" 2012   .18699826 1    .17980434
    "Australia" 2013    .1915309 1     .1858111
    "Australia" 2014   .19498566 1    .19047257
    "Australia" 2015   .19519643 1     .1936539
    "Australia" 2016   .19913107 1    .19538054
    "Australia" 2017   .19104116 1    .19583565
    "Australia" 2018    .1880693 1    .19534014
    "Austria"   2000   .06848034 2    .06939875
    "Austria"   2001   .06204083 2    .06132264
    "Austria"   2002   .06006476 2    .05406203
    "Austria"   2003    .0503393 2    .04812311
    "Austria"   2004   .04238531 2    .04386881
    "Austria"   2005  .035559107 2    .04147954
    "Austria"   2006   .03483601 2    .04093356
    "Austria"   2007    .0402198 2    .04200943
    "Austria"   2008   .05063824 2    .04430992
    "Austria"   2009   .04605066 2    .04730511
    "Austria"   2010    .0450753 2     .0503898
    "Austria"   2011   .05936556 2    .05294907
    "Austria"   2012   .06413041 2    .05442498
    "Austria"   2013   .05875508 2    .05437694
    "Austria"   2014    .0483415 2    .05252954
    "Austria"   2015   .03730266 2    .04880233
    "Austria"   2016    .0348463 2     .0433186
    "Austria"   2017  .035698645 2   .036391962
    "Austria"   2018    .0403587 2    .02849239
    "Belgium"   2000  .037054755 3   .032760195
    "Belgium"   2001   .02304836 3    .02661626
    "Belgium"   2002  .020087115 3   .019728873
    "Belgium"   2003  .008589108 3    .01240482
    "Belgium"   2004  .008907577 3     .0049982
    "Belgium"   2005 .0002929789 3 -.0021279356
    "Belgium"   2006 -.012985984 3  -.008640932
    "Belgium"   2007 -.016497875 3   -.01427458
    "Belgium"   2008 -.021405246 3  -.018857956
    "Belgium"   2009 -.018448105 3  -.022333955
    "Belgium"   2010   -.0202843 3   -.02476547
    "Belgium"   2011 -.024024444 3   -.02632858
    "Belgium"   2012  -.02801865 3   -.02729348
    "Belgium"   2013  -.03328071 3   -.02799517
    "Belgium"   2014  -.02987244 3  -.028797183
    "Belgium"   2015 -.030965704 3   -.03005219
    "Belgium"   2016 -.031977374 3   -.03206376
    "Belgium"   2017  -.03363424 3  -.035053477
    "Belgium"   2018  -.03779807 3   -.03913694
    "Canada"    2000   .12833479 4     .1333923
    "Canada"    2001   .11813153 4    .12748383
    "Canada"    2002   .12230187 4    .12016221
    "Canada"    2003    .1172834 4     .1119533
    "Canada"    2004   .10871444 4    .10347908
    "Canada"    2005   .10835042 4    .09539033
    "Canada"    2006   .09427632 4      .088296
    "Canada"    2007   .07679803 4    .08269735
    "Canada"    2008   .07312575 4     .0789335
    "Canada"    2009    .0673351 4    .07714474
    "Canada"    2010      .06768 4    .07725738
    "Canada"    2011    .0773233 4    .07899164
    "Canada"    2012   .08508106 4    .08189202
    "Canada"    2013   .09713881 4    .08537666
    "Canada"    2014   .10177702 4     .0888004
    "Canada"    2015   .09207828 4    .09152465
    "Canada"    2016   .08885638 4    .09298664
    "Canada"    2017   .09136125 4    .09276036
    "Canada"    2018   .08317746 4     .0906028
    "Chile"     2000   -.7589697 5    -.7558202
    "Chile"     2001   -.7635748 5     -.762177
    "Chile"     2002   -.7770681 5    -.7675414
    "Chile"     2003   -.7743408 5    -.7705911
    "Chile"     2004   -.7652876 5    -.7702547
    "Chile"     2005   -.7513134 5    -.7658278
    "Chile"     2006   -.7393435 5    -.7570491
    "Chile"     2007   -.7391461 5    -.7441292
    "Chile"     2008   -.7280036 5    -.7277269
    "Chile"     2009    -.728761 5    -.7088791
    "Chile"     2010   -.7113042 5    -.6888871
    "Chile"     2011   -.6841534 5    -.6691765
    "Chile"     2012   -.6411921 5    -.6511422
    "Chile"     2013   -.6129913 5    -.6359978
    "Chile"     2014   -.6130918 5    -.6246433
    "Chile"     2015   -.6112306 5    -.6175684
    "Chile"     2016    -.612393 5    -.6147987
    "Chile"     2017   -.6311616 5    -.6158935
    "Chile"     2018   -.6247708 5    -.6199932
    end
    I run this code to get the graphs:

    Code:
        line stoc fourier year,  by(country,  bgcolor(white) graphregion(color(white)) rescale note("") legend(off)) ylabel(,angle(90)) xtitle("") by(, plegend(on)) scheme(meta) ylabel(, angle(0)) xlabel(  2000 2010 2020)
    My figures are as below:
    Click image for larger version

Name:	fourier_stata.png
Views:	1
Size:	202.2 KB
ID:	1722029

    Now I have two questions which I could not manage to solve.
    1. Even though I label my x-axis starting from the first year (2000) of my dataset, there is a space between the first year and y-axis. In other words, how can I intersect y-axis and my lines?
    2. There is a vertical space between each graph. How can I decrease this space? I have 5 rows of graphs but I want these rows to be as close as possible.

    I really appreciate any help.

  • #2
    1. Use the option

    Code:
    plotregion(margin(l=0))
    2. Use the suboption compact of by()

    Comment


    • #3
      Thanks a lot, Nick.

      Comment


      • #4
        You got what you asked for, but the underlying problem looks clear: You are plotting 21 graphs and want each to be legible and also want the whole display to work well.

        This is a common problem and a tough call.

        I submit that your display is clear in principle but of limited use in practice. Is a reader, including yourself, really going to spend time studying the different vertical scales and comparing 21 countries?

        Further, alphabetical country name order is tidy in principle but only helpful if there is a dictionary or directory role of looking up particular countries. It's less useful for seeing patterns of similarity and difference.

        This isn't your fault. It's the fault of the design.

        I have addressed this kind of problem in various places. The tension is between superimposing series, with the risk of spaghetti, and juxtaposing series, with the risk that comparison becomes (much) harder.

        https://journals.sagepub.com/doi/pdf...36867X19893641

        https://journals.sagepub.com/doi/pdf...6867X211025838

        https://journals.sagepub.com/doi/pdf...6867X211045582

        Here is one approach. I have faked a dataset of approximately similar complexity.

        Code:
        clear 
        set obs 220
        gen id = ceil(_n/11)
        gen country = word("`c(ALPHA)'", id)
        bysort id : gen year = 2010 + _n 
        gen whatever = ceil(_n/11) + rnormal(0,1)
        
        * machinery to separate countries with ranks 1, 3, 5, ... from those with ranks 2, 4, 6, ...
        * use criterion other than mean if it makes more sense 
        egen mean = mean(whatever), by(country)
        egen group = group(mean country)
        gen which = mod(group, 2)
        
        line whatever year, c(L) by(which, note("") legend(off)) subtitle("", pos(9) fcolor(none) nobexpand)  xla(2011(2)2021) ///
        || scatter whatever year if year == 2021, ms(none) mla(country) mlabcolor(stc2) ///
        || scatter whatever year if year == 2011, ms(none) mla(country) mlabcolor(stc2) mlabpos(9) xsc(r(2010.5 .))
        Click image for larger version

Name:	spaghetti2.png
Views:	1
Size:	91.0 KB
ID:	1722197

        The devices used include

        * Direct labelling of countries. In your case two or three letter abbreviations of country names should probably work quite well. There are standard abbreviations in frequent use.

        * 20 or so countries on a single graph would probably not work well. A device used here is to use two groups with some separation between individual series.

        Other possibilities include using different colours and say three panels, not two.

        Comment

        Working...
        X