Announcement

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

  • how to color two sets of 15 lines in twoway tsline, one set blue, one set red

    Dear Statalist,

    I have two sets of lines, each set containing 15 lines. Is there a way to apply different colors to the two sets while each line in the same set has the same color? I note that there is limit of the number of arguments I can use for the lcolor option.

    Grateful for advice.

    Thanks.
    Charlie
    Last edited by charlie wong; 17 Sep 2017, 00:36.

  • #2
    It would help to have the code you are starting from, to better understand the organization of the data you are starting from, and the graph you hope to end up with.
    You might look into "pstyles".
    Doug Hemken
    SSCC, Univ. of Wisc.-Madison

    Comment


    • #3
      Originally posted by Doug Hemken View Post
      It would help to have the code you are starting from, to better understand the organization of the data you are starting from, and the graph you hope to end up with.
      You might look into "pstyles".
      Thanks Doug. I am working with the following code:

      graph twoway tsline Set1A Set1B Set1C Set1* Set2A Set2B Set2C Set2* , ///
      lcolor (red red red red red red red red red red red red red red blue blue blue blue blue blue ) ///
      subtitle(,size(small)) tlabel(,format(%tCHH) labsize(vsmall)) legend(off) ytitle("") xtitle("") title("") ///
      yscale(noline lpattern() lwidth() ///
      xsize(10) ysize(20) xlabel(#10) ylabel(1(4)14, labsize() tlength(zero) glwidth(vvvthin) glstyle(dot) glcolor(black)) ytick(none) ymlabel(none)


      My question is, given the limit on number of arguments, is there a way to assign one color to lines of Set1 and another color to lines of Set2.

      For illustration, the Set1* lines are headline inflation, and Set2* lines are underlying inflation. I want to plot on the same graph both types of inflation for a number of countries. I already transformed the inflation rates for each country such that for country A, value 1 correspond 0% inflation rate, for country B value 2 correspond to 0% inflation rate, so that the lines for country A will fluctuate around 1 , and those for country B around 2, country C around 3, etc. And for the pair of lines for each country, I want red for headline inflation, and blue for underlying, so that they can be distinguished.

      I hope this makes the picture clear. thanks!

      charlie


      Comment


      • #4
        Unfortunately, there is no data set to work with. However, no matter the code, there is always - luckily enough! - the option to edit graphs directly in Stata, I mean, by point-and-click operations.
        Best regards,

        Marcos

        Comment


        • #5
          Originally posted by Marcos Almeida View Post
          Unfortunately, there is no data set to work with. However, no matter the code, there is always - luckily enough! - the option to edit graphs directly in Stata, I mean, by point-and-click operations.
          my apologies for not providing a data set to work with. unfortunately editing graph directly in Stata may not be convenient enough as I need to automate the process.

          the following code should illustrate my query.

          ***************
          clear *
          set obs 100

          forvalues country =1/15{
          foreach inflation in "headline" "underlying" {
          gen `inflation'`country'=1+runiform(0,0.1)+`country'
          }
          }
          gen time=_n
          tsset time

          graph twoway tsline headline* underlying* , ///
          lcolor (red red red red red red red red red red red red red red blue blue blue blue blue blue ) ///
          subtitle(,size(small)) tlabel(,format(%tCHH) labsize(vsmall)) legend(off) ytitle("") xtitle("") title("")

          *************

          thanks.
          charlie

          Comment


          • #6
            That's a lot of lines in one graph. What do you think of showing the lines for each country in separate panels? This has the following advantages:
            • No transformation of the inflation rates is necessary because country lines don't overlap.
            • The lines have the same colors in all panels.
            • Small variations in the inflation rate can be seen more easily.
            Code:
            clear
            set obs 100
            
            forvalues country = 1/15 {
              foreach inflation in "headline" "underlying" {
                gen `inflation'`country' = 1 + runiform(0,0.1)
              }
            }
            
            gen time = _n
            tsset time
            reshape long headline underlying, i(time) j(country)
            
            graph twoway tsline headline underlying, ///
              by(country, legend(at(16) pos(0)) note("")) ///
              ylabel(,angle(0)) legend(col(1)) xtitle("") ytitle("")

            Comment


            • #7
              Originally posted by Friedrich Huebler View Post
              That's a lot of lines in one graph. What do you think of showing the lines for each country in separate panels? This has the following advantages:
              • No transformation of the inflation rates is necessary because country lines don't overlap.
              • The lines have the same colors in all panels.
              • Small variations in the inflation rate can be seen more easily.
              Code:
              clear
              set obs 100
              
              forvalues country = 1/15 {
              foreach inflation in "headline" "underlying" {
              gen `inflation'`country' = 1 + runiform(0,0.1)
              }
              }
              
              gen time = _n
              tsset time
              reshape long headline underlying, i(time) j(country)
              
              graph twoway tsline headline underlying, ///
              by(country, legend(at(16) pos(0)) note("")) ///
              ylabel(,angle(0)) legend(col(1)) xtitle("") ytitle("")
              Thanks for the suggestion Friedrich. But I am afraid I need to stick to one graph, because I actually need to do by groups (for example by continent) which i didn't show in the example. So I need one graph for one set of countries and then put different sets of country in one page.

              It seems the option to apply one color to a set of lines is not available in Stata?

              Comment


              • #8
                Why don't you create one graph per group of countries and graph combine them later?

                Comment


                • #9
                  Originally posted by Friedrich Huebler View Post
                  Why don't you create one graph per group of countries and graph combine them later?
                  deleted
                  Last edited by charlie wong; 17 Sep 2017, 10:32.

                  Comment


                  • #10
                    Thanks for providing workable data.

                    In the example below (after I - reshaped long - the data), when comparing values from both variables, we see there is practically no difference, hence the "overlying layers" pattern.

                    Code:
                    . gen id = _n
                    
                    . reshape long headline underlying, i(id) j(phase)
                    (note: j = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)
                    
                    Data                               wide   ->   long
                    -----------------------------------------------------------------------------
                    Number of obs.                      100   ->    1500
                    Number of variables                  31   ->       4
                    j variable (15 values)                    ->   phase
                    xij variables:
                         headline1 headline2 ... headline15   ->   headline
                    underlying1 underlying2 ... underlying15  ->   underlying
                    -----------------------------------------------------------------------------
                    
                    . gen dif = headline - underlying
                    
                    . sum dif, detail
                    
                                                 dif
                    -------------------------------------------------------------
                          Percentiles      Smallest
                     1%     -.084836      -.0985012
                     5%    -.0679829      -.0962563
                    10%    -.0548494      -.0919361       Obs               1,500
                    25%    -.0276403      -.0911989       Sum of Wgt.       1,500
                    
                    50%     .0016491                      Mean           .0013718
                                            Largest       Std. Dev.      .0409135
                    75%       .03104       .0940919
                    90%     .0564899       .0941477       Variance       .0016739
                    95%      .071502       .0947561       Skewness      -.0178256
                    99%     .0860038       .0967388       Kurtosis       2.395841
                    Edit: after quite a while, did I notice Friedrich' suggestion on a similar verge, i.e, to - reshape long - as well.

                    Last edited by Marcos Almeida; 17 Sep 2017, 11:29.
                    Best regards,

                    Marcos

                    Comment


                    • #11
                      Originally posted by charlie wong View Post
                      It seems the option to apply one color to a set of lines is not available in Stata?
                      It is possible to draw the two lines with the same color for all countries.
                      Code:
                      twoway (tsline headline*, lcolor(red red red red red red red red red red red red red red red)) ///
                        (tsline underlying*, lcolor(blue blue blue blue blue blue blue blue blue blue blue blue blue blue blue)), ///
                        subtitle(,size(small)) tlabel(,format(%tCHH) labsize(vsmall)) legend(off) ytitle("") xtitle("") title("")

                      Comment


                      • #12
                        Originally posted by Friedrich Huebler View Post

                        It is possible to draw the two lines with the same color for all countries.
                        Code:
                        twoway (tsline headline*, lcolor(red red red red red red red red red red red red red red red)) ///
                        (tsline underlying*, lcolor(blue blue blue blue blue blue blue blue blue blue blue blue blue blue blue)), ///
                        subtitle(,size(small)) tlabel(,format(%tCHH) labsize(vsmall)) legend(off) ytitle("") xtitle("") title("")
                        This works like a charm! Thank you so much Friedrich!

                        Comment

                        Working...
                        X