Announcement

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

  • How to modify orientation of (y)title text

    Dear Stata Users,

    I encounter a problem about graph's ytitle modification. In Stata's twoway plot, the default setting of ytitle is making text reads bottom to top. And with some options, we can modify this setting, for example, we can add option -orientation(horizontal)- and making text reads left to right. However, in some cases, maybe we want to make it more readable, let's say making text reads top to bottom while every character is printed horizontal. In Stata it seems there's no option to reach this and I have to quote each character one by one (graph g3 in code belows). So I want to know if there's some method that can release me from these "annoying" quotes. Thank you.

    Code:
    graph drop _all
    
    sepscatter enroll year, ytitle("ABCDEF") xtitle("") ///
    separate(scountry) recast(conn) legend(ring(0) posi(11) col(1) region(style(none))) name(g1)
    
    sepscatter enroll year, ytitle("ABCDEF", orientation(horizontal)) xtitle("") ///
    separate(scountry) recast(conn) legend(ring(0) posi(11) col(1) region(style(none))) name(g2)
    
    sepscatter enroll year, ytitle("A" "B" "C" "D" "E" "F", orientation(horizontal)) xtitle("") ///
    separate(scountry) recast(conn) legend(ring(0) posi(11) col(1) region(style(none))) name(g3)
    Code:
    * Example generated by -dataex-. To install:    ssc    install    dataex
    clear
    input int year byte scountry double enroll
    2008  1 20.93997955
    2009  1 22.51596069
    2010  1 23.94791985
    2011  1 24.87281036
    2012  1 27.18442917
    2013  1 30.16206932
    2014  1 39.39038849
    2015  1 43.39176941
    2008  7 18.94574928
    2009  7  20.1099205
    2010  7 22.68767929
    2011  7 24.80245972
    2012  7  25.0209198
    2013  7 25.01012993
    2014  7 30.47773933
    2015  7 28.83564949
    2008 20 15.11709976
    2009 20  16.1029892
    2010 20 17.91148949
    2011 20 22.86137009
    2012 20 24.36532974
    2013 20 23.89011002
    2014 20 25.53544998
    2015 20 26.87487984
    end
    label values scountry scountry
    label def scountry 1 "China", modify
    label def scountry 7 "Vietnam", modify
    label def scountry 20 "India", modify

  • #2
    sepscatter is from SSC, as you are asked to explain, but your using it seems irrelevant here.

    If you want a title on two or more lines, you have to tell Stata what text goes on each line, so I don't know what other syntax you expect to be possible here. Stata won't guess on your behalf and there is no further option such as "one word on each line".

    Comment


    • #3
      Dear Nick, yes, you are right, I use -sepscatter- (SSC) to quickly graph on data in hand, and it's irrelevant to my problem here. And as regards the question that I concerned, I think I get your point.
      p.s. I used package labutil and the -labvarch- (SSC) command yesterday and got excelent results. This p.s. is irrelevant here, I just want to take this opportunity to thank you (hope I have not disobey rules about thread opening and closing in this Forum).

      Comment


      • #4
        Thanks for the thanks, and side-remarks like those in #3 are fine.

        Comment


        • #5
          Have been inspired by -splitvallabels- command (SSC) written by Nicholas Winter & Ben Jann, I written a small piece of program to work out my question in #1. The program can split the labels for a variable into multiple chunks and store the results into local. Let's say the local is named "retitle", then you can just add ytitle(`retitle', orient(horizontal)) to work around the problem in #1. This program is specially suited for variable labels in chinese because every chinese character is stored in 2 bytes. For variable labels in english, people can add -nobreak- option as shown in below, thus the word in variable labels will not to be broken. I should have added -nobreak- option and used a more complicated loop, but I failed to achieve that. I will be very happy if someone can help me to improve this.
          Code:
          *! inspired by splitvallabels.ado v1.1.2 Nicholas Winter/Ben Jann 14aug2008
          *! modify by Chen, 2019 March 5
          
          program define splitvarlabels, rclass
              version 8.2
              syntax varlist [if] [in], [ Length(int 2) local(string)]
          
              local labname : variable label `varlist'
              local i 1
              local maxi : length local labname
              while `i'<=`maxi'/`length' {
              if "`labname'"!="" {
                  local labpart : piece `i' `length' of `"`labname'"'  //can add -nobreak- option.
                        local newstring `"`newstring'`"`labpart'"' "'
                        local i=`i'+1
                }
              }
          
              return local retitle `"`newstring'"'
              if "`local'"!="" {
                  c_local `local' `"`newstring'"'
              }
          
          end
          Code:
          sysuse sp500
          label var change "S&P Stock price change when exchange closing"
          splitvarlabels2 change, local(change) /*splitvarlabels2 add a nobreak option in the above program*/
          line change date, ylabel(-60(30)70) title("graph one with vertical ytitle") saving(g1.gph)
          line change date, ytitle(`change', orient(horizontal)) ylabel(-60(30)70) title("graph two with horizontal ytitle") saving(g2.gph)
          Click image for larger version

Name:	ytitle.png
Views:	1
Size:	184.7 KB
ID:	1486686

          Comment


          • #6
            An improved version that add nobreak option and a loop.
            Code:
            *! inspired by splitvallabels.ado v1.1.2 Nicholas Winter/Ben Jann 14aug2008
            *! modify by Chen, 2019 March 5
            
            program define splitvarlabels, rclass
                version 8.2
                syntax varlist(max=1) [if] [in], [ Length(int 2) local(string) nobreak]
            
                local labname : variable label `varlist'
                if "`break'"!="" {
                local i 1
                local maxi : word count `labname'
                while `i'<=`maxi' {
                if "`labname'"!="" {
                    local labpart : piece `i' `length' of `"`labname'"', nobreak
                          local newstring `"`newstring'`"`labpart'"' "'
                          local i=`i'+1
                    }
                  }
                }
            
                else {
                local i 1
                local maxi : length local labname
                while `i'<=`maxi'/`length' {
                if "`labname'"!="" {
                    local labpart : piece `i' `length' of `"`labname'"'
                          local newstring `"`newstring'`"`labpart'"' "'
                          local i=`i'+1
                    }
                  }
                }
            
                return local retitle `"`newstring'"'
                if "`local'"!="" {
                    c_local `local' `"`newstring'"'
                }
            
            end

            Comment


            • #7
              Originally posted by Chen Samulsion View Post
              An improved version that add nobreak option and a loop.
              Code:
              *! inspired by splitvallabels.ado v1.1.2 Nicholas Winter/Ben Jann 14aug2008
              *! modify by Chen, 2019 March 5
              
              program define splitvarlabels, rclass
              version 8.2
              syntax varlist(max=1) [if] [in], [ Length(int 2) local(string) nobreak]
              
              local labname : variable label `varlist'
              if "`break'"!="" {
              local i 1
              local maxi : word count `labname'
              while `i'<=`maxi' {
              if "`labname'"!="" {
              local labpart : piece `i' `length' of `"`labname'"', nobreak
              local newstring `"`newstring'`"`labpart'"' "'
              local i=`i'+1
              }
              }
              }
              
              else {
              local i 1
              local maxi : length local labname
              while `i'<=`maxi'/`length' {
              if "`labname'"!="" {
              local labpart : piece `i' `length' of `"`labname'"'
              local newstring `"`newstring'`"`labpart'"' "'
              local i=`i'+1
              }
              }
              }
              
              return local retitle `"`newstring'"'
              if "`local'"!="" {
              c_local `local' `"`newstring'"'
              }
              
              end
              Thanks Chen! It is very helpful!
              Kailin

              Comment


              • #8
                Is there a way to move the ytitle to the top of the y axis?

                Comment


                • #9
                  Code:
                  . sysuse auto, clear
                  (1978 automobile data)
                  
                  . help axis title options
                  
                  . scatter mpg weight, ytitle(, placement(top))

                  Comment

                  Working...
                  X