Announcement

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

  • I want to plot the trend in stata

    Hey Guys, i am trying to plot the trend in stata. For this purpose, i put years on x-axis 2004, 2006, 2008, 2010, 2012, 2014, and 2016. On y-axis there are two variables number of workers and GDP growth. So basically these two lines should be in the same graph with different colors. But when i plot the graph on x-axis it shows me 2005, 2010 and 2015. How can i put all the years which i have mentioned above? Apart from this i am unable to change the color of both lines, for instance, GDP Growth and number of workers come with the same color in the graph. Lastly, i wanted to plot another graph for hour wage and gdp and want to join these two graphs like for a number of workers/gdp and hour wage/gdp. What is the command for this? I am putting sample data and graph as well so you can have the idea.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year float(officials gdp)
    2004 5.09 7.4
    2006 4.48 6.2
    2008 2.67 1.7
    2010 3.51 1.6
    2012 3.95 3.5
    2014 14.1 4.7
    2016    8 5.7
    end
    format %ty year
    Please see the attachment for one graph. i need to create another graph like this and need to join them just like a mirrior image.

    Attached Files

  • #2
    Please show the command that you used to create the graph so that we can propose modifications. Please also read the FAQ (especially section 12) for advice on asking questions in a manner that makes it easier for others to help.

    Comment


    • #3
      Code:
       twoway (tsrline gdp officials, lcolor(red) lwidth(vvvthin) lpattern(solid) connect(direct)), ttitle(, size(medium) margin(medium)) tscale(lwidth(vthin) lpattern(solid) line)
      The data is at alternative years from 2004 to 2006 when i declare the data the option is annual. Is it correct to declare it annual data? I have mentioned the command above please tell how can i solve this problem. Thank you

      Comment


      • #4
        Plot the data for the two variables separately, using "||", and change the x-axis labels with xlabel().
        Code:
        tsset year
        twoway (tsline gdp, lcolor(red)) || (tsline officials, lcolor(blue)), xlabel(2004(2)2016)

        Comment


        • #5
          Code:
          twoway (tsrline gdp officials, lcolor(red) lwidth(vvvthin) lpattern(solid) connect(direct)), ttitle(, size(medium) margin(medium)) tscale(lwidth(vthin) lpattern(solid) line) xla(2004(2)2016)

          Comment


          • #6
            Here's another way to do it, perhaps especially applicable with responses that have quite different units of measurement.

            Code:
            set scheme s1color 
            multiline officials gdp year, xla(2004(2)2016) recast(connected) xtitle("")
            The multiline code is available here:

            Code:
            *! 1.2.0 NJC 8mar2017 
            *! 1.1.0 NJC 2oct2016 
            * 1.0.0 NJC 5sept2016 
            program multiline 
                version 11
                syntax varlist(numeric) [if] [in] ///
                [, by(str asis) mylabels(str asis) *] 
                
                quietly { 
                    marksample touse 
                    count if `touse' 
                    if r(N) == 0 exit 2000 
            
                    preserve 
                    keep if `touse' 
                    drop `touse' 
                
                    gettoken yvar rest : varlist 
                    local J = 0 
                    while "`yvar'" != "" { 
                        local ++J 
                        local lbl`J' : var label `yvar' 
                        if `"`lbl`J''"' == "" local lbl`J' "`yvar'" 
                        local last "`yvar'" 
                        gettoken yvar rest : rest    
                    } 
            
                    local xvar "`last'" 
                    local yvar : list varlist - xvar 
            
                    capture tsset 
                    if "`r(panelvar)'" != "" local panelvar "`r(panelvar)'" 
            
                    foreach v of local yvar { 
                        local call `call' `v' `panelvar' `xvar' 
                    }
            
                    tempname y
                    tempfile mydo 
                    local label : value label `xvar'  
                    label save `label' using "`mydo'" 
                    stack `call', into(`y' `panelvar' `xvar') clear 
                    do "`mydo'" 
                    if "`label'" != "" { 
                        label val `xvar' `label' 
                    } 
            
                    local Jm1 = `J' - 1 
                    if `"`mylabels'"' != "" { 
                        tokenize `mylabels' 
                        forval j = 1/`Jm1' { 
                            label def _stack `j' `"``j''"', add 
                        } 
                    } 
                    else forval j = 1/`Jm1' { 
                        label def _stack `j' `"`lbl`j''"', add 
                    } 
                    label val _stack _stack 
                } 
            
                sort `panelvar' `xvar' 
                label var `xvar'  `"`lbl`J''"'
            
                line `y' `xvar', by(_stack, col(1) yrescale note("") `by') ///
                ytitle("") yla(, ang(h)) c(L) ///
                subtitle(, pos(9) bcolor(none) nobexpand place(e)) `options' 
            end
            Click image for larger version

Name:	multiline3.png
Views:	1
Size:	24.3 KB
ID:	1400137

            Comment


            • #7
              Thank you sir.

              Comment


              • #8
                #6: https://www.statalist.org/forums/for...ailable-on-ssc explains posting of improved code and help, now downloadable from SSC.

                Comment

                Working...
                X