Announcement

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

  • Graph combine ignores specified xlab values

    I am using graph combine to group time series plots.

    As you know dates on the x-axis are special, so I specify the manually (the same for all sub-graphs)
    Code:
      xlab(`=d(29feb2020)' `=d(01may2020)' `=d(01jul2020)' `=d(01sep2020)' `=d(01nov2020)' `=d(01jan2021)' `=d(28feb2021)', format(%td) ang(45))
    For each single graph this produces the axis labels how I want them. However, when I combine it using

    Code:
    graph combine lux switz italy ser, 
        ysize(25) xsize(45) xcommon
        graphregion(color(white))
    It does display the correct angle, but not the dates I provided (instead it gives the dates I would get without manual entry). Did you experience this too and have an idea where it could come from?

  • #2
    EDITED: With the following example, I cannot reproduce your problem. I would suggest that you provide a reproducible example.

    Code:
    sysuse auto
    tw scatter mpg weight, xlab(1000(1500)6000) ylab(10(5) 40) saving(gr1, replace)
    tw scatter mpg price, xlab(1000(3000)15000) saving(gr2, replace)
    gr combine gr1.gph gr2.gph, xcommon ycommon  ysize(25) xsize(45)
    Last edited by Andrew Musau; 17 Mar 2021, 18:15.

    Comment


    • #3
      Yea, it seems to only do that with dates. This example reproduces it.

      Code:
      sysuse xtline1.dta, replace
      
      forval i = 1(1)3 {
          #delimit ;
              twoway
              (line calories day if person == `i'),
              xtitle("") ytitle("")
              graphregion(margin(2 7 2 2))
              xlab(`=d(01jan2002)' `=d(01mar2002)'  `=d(01may2002)' `=d(01jul2002)' `=d(01sep2002)' `=d(01nov2002)' `=d(01jan2003)', format(%td) ang(45))
              plotregion(style(none)) scheme(plotplain)
              name(gr_`i', replace) nodraw;
          #delimit cr
      }
      
          #delimit ;
          graph combine gr_1 gr_2 gr_3,
          ysize(25) xsize(45) xcommon
          graphregion(color(white));
          #delimit cr

      Comment


      • #4
        It appears that there is an issue with conversion from SIF to %td display if using the -xcommon- option of gr combine. Either

        1. Drop the -xcommon- option or
        2. Have the values as SIF in the label (number, not date format) and then provide your own labels.

        For #2, the following works:

        Code:
        sysuse xtline1.dta, replace
        
        forval i = 1(1)3 {
            #delimit ;
                twoway
                (line calories day if person == `i'),
                xtitle("") ytitle("")
                graphregion(margin(2 7 2 2))
                xlab(`=d(01jan2002)' "`:di %td `=d(01jan2002)''" `=d(01mar2002)' "
                `: di %td `=d(01mar2002)''" `=d(01may2002)' "
                `: di %td `=d(01may2002)''"`=d(01jul2002)' "`: di %td `=d(01jul2002)''"
                `=d(01sep2002)' "`: di %td `=d(01sep2002)''" `=d(01nov2002)'"
                `: di %td `=d(01nov 2002)''"`=d(01jan2003)' "`: di %td `=d(01jan2003)''",
                 format(%9.0f) ang(45))
                plotregion(style(none)) scheme(plotplain)
                name(gr_`i', replace) nodraw;
            #delimit cr
        }
        
            #delimit ;
            graph combine gr_1 gr_2 gr_3,
            ysize(25) xsize(45) xcommon
            graphregion(color(white));
            #delimit cr
        Last edited by Andrew Musau; 18 Mar 2021, 05:10.

        Comment


        • #5
          Indeed, option 2 works, thank you very much Andrew Musau! Do you maybe also have a clue why with this formatting the range of the x-axis increases on the left? It actually extends beyond the minimum value of day in the dataset.

          Comment


          • #6
            Do you maybe also have a clue why with this formatting the range of the x-axis increases on the left? It actually extends beyond the minimum value of day in the dataset.
            I do not completely get this. Maybe you can illustrate with an example. What I see is that the label appears truncated if specifying the option -ang(45)- in the individual graphs. You can increase the margin outside the y-axis to avoid this.

            Code:
            ysc(outergap(10))

            Comment


            • #7
              See Figure below, which is the output from the code. January 1 2002 is the minimum value of day in the dataset and should be the beginning of the axis. Here there is a considerable gap on the left. Is it clearer what I mean? Changing the margin outside the y-axis does not change this behaviour


              Click image for larger version

Name:	Graph.png
Views:	1
Size:	94.3 KB
ID:	1598532

              Comment


              • #8
                Yes, I can replicate the behavior with my example in #2. It appears that gr combine will add an extra unit at the start of the graph. As a workaround, you can calculate what a unit is and rescale the x variable, using labels to mask the ensuing values. In the example in #3, a unit is equal to 60.

                Code:
                . di `=d(01mar2002)'- `=d(01jan2002)'+ 1
                60

                Code:
                sysuse xtline1.dta, replace
                replace day= day-60
                forval i = 1(1)3 {
                    #delimit ;
                        twoway
                        (line calories day if person == `i'),
                        xtitle("") ytitle("")
                        graphregion(margin(2 7 2 2))
                        xlab(`=d(01jan2002)-60' "`:di %td `=d(01jan2002)''" `=d(01mar2002)-60'
                        " `: di %td `=d(01may2002)''"  `=d(01may2002)-60' " `: di %td `=d(01may2002)''"
                        `=d(01jul2002)-60' "`: di %td `=d(01jul2002)''"
                        `=d(01sep2002)-60' "`: di %td `=d(01sep2002)''" `=d(01nov2002)-60'
                        "`: di %td `=d(01nov 2002)''" `=d(01jan2003)-60' "`: di %td `=d(01jan2003)''",
                         format(%9.0f) ang(45)) 
                        plotregion(style(none)) scheme(plotplain)
                        name(gr_`i', replace) nodraw;
                    #delimit cr
                }
                
                    #delimit ;
                    graph combine gr_1 gr_2 gr_3,
                    ysize(25) xsize(45) xcommon
                    graphregion(color(white));
                    #delimit cr
                The issue is that the entire gap now shifts to the right-hand side.
                Click image for larger version

Name:	Graph.png
Views:	1
Size:	205.8 KB
ID:	1598603





                As a compromise, you may want the plot centered so that you have an equal gap in the right- and left-hand side. Therefore, you use half a unit to rescale, in this case, 60/2 = 30.

                Code:
                sysuse xtline1.dta, replace
                replace day= day-30
                forval i = 1(1)3 {
                    #delimit ;
                        twoway
                        (line calories day if person == `i'),
                        xtitle("") ytitle("")
                        graphregion(margin(2 7 2 2))
                        xlab(`=d(01jan2002)-30' "`:di %td `=d(01jan2002)''" `=d(01mar2002)-30'
                        " `: di %td `=d(01may2002)''"  `=d(01may2002)-30' " `: di %td `=d(01may2002)''"
                        `=d(01jul2002)-30' "`: di %td `=d(01jul2002)''"
                        `=d(01sep2002)-30' "`: di %td `=d(01sep2002)''" `=d(01nov2002)-30'
                        "`: di %td `=d(01nov 2002)''" `=d(01jan2003)-30' "`: di %td `=d(01jan2003)''",
                         format(%9.0f) ang(45)) 
                        plotregion(style(none)) scheme(plotplain)
                        name(gr_`i', replace) nodraw;
                    #delimit cr
                }
                
                    #delimit ;
                    graph combine gr_1 gr_2 gr_3,
                    ysize(25) xsize(45) xcommon
                    graphregion(color(white));
                    #delimit cr
                Click image for larger version

Name:	Graph.png
Views:	1
Size:	202.7 KB
ID:	1598604

                Last edited by Andrew Musau; 18 Mar 2021, 14:04.

                Comment


                • #9
                  Thanks, good idea! But I am not following.. The difference between the specified dates varies, between Jan 1 and Mar 1 it is 59, for most two months it is 61 and between Jul 1 and Sep 1 it is 62. So just substracting 60 (or 30) everywhere will shift the values of dates, or not? I guess I can remedy this by shifting dates depending on individual difference between dates..
                  Last edited by Felix Stips; 19 Mar 2021, 04:44.

                  Comment


                  • #10
                    For labeling purposes, I don't think that it matters much that the dates are 1 or 2 days off if a unit is 60 days. So 30 still remains a good approximation.

                    Comment

                    Working...
                    X