Announcement

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

  • Percent change over line graph

    Hi everyone,

    I'm trying to make a line graph that shows mean HHIs for five different industries. HHIs are on the y-axis and year on the x-axis. Is there a code that will allow me to display the percent change in HHI from the first year to the last year just over each line? I know I could input separate text boxes manually, but I'm wondering if Stata can automate the process. Thanks!

  • #2
    Code:
     ssc install lgraph //if you do not have the program, install it first with this command
    lgraph HHIs year industries // this will provide the mean for HHIs for each year by industries
    Type -help lgrpah- for help on lgraph. There are of course many other approaches based on how your data is arranged. Generate a column for %change by HHI first to last year, and follow the similar process for mean %change. Because if you have not provided any data example, this is a guess that it is what you want.

    Please read the FAQ Section on how to make meaningful post and provide data examples so that people can help you. Not necessarily people will know what you meant by HHIs. You are expected to elaborate.

    Roman

    Comment


    • #3
      Hi Jason,

      I suggest the following solution:

      Click image for larger version

Name:	figure.png
Views:	1
Size:	38.8 KB
ID:	1356092


      Code:
      clear
      input str20 industry hhi year
      
      industry         hhi         year      
      "fishing"        2200      2012
      "logging"       3700      2012
      "mining"        2650      2012
      "farming"       3000      2012
      "baking"        4800      2012
          
      "fishing"        2200      2013
      "logging"       3700      2013
      "mining"        2750      2013
      "farming"       3020      2013
      "baking"        4700      2013
          
      "fishing"        2300      2014
      "logging"       3700      2014
      "mining"        2650      2014
      "farming"       3050      2014
      "baking"        4650      2014
          
      "fishing"         2400      2015
      "logging"        3700      2015
      "mining"         2800      2015
      "farming"        3200      2015
      "baking"         4500      2015
      
      end
      summarize year
      bysort industry :gen firstyearHHI = hhi if year==r(min)
      bysort industry :gen lastyearHHI = hhi if year==r(max)
      sort industry year
      
      replace firstyearHHI =firstyearHHI[_n-1] if firstyearHHI ==.
      gen percent_change_temp = (lastyearHHI-firstyearHHI)/firstyearHHI
      bysort industry: egen percent_change = min(percent_change_temp)
      replace percent_change = round(percent_change, .0001)
      
      gen percent_change_string= string(100*percent_change)+"%"
      
      drop firstyearHHI lastyearHHI percent_change_temp percent_change
      rename percent_change_string percent_change
      levelsof industry, local(i)
      
      
      reshape wide hhi percent_change, i(year) j(industry) string
      
      
      
      
      twoway  (connected hhi* year, legend(order(1 2 3 4 5)) title("% Change HHI, 2012-2015") ytitle("HHI") note("Note: data is synthetic and represents no industries")) ///
        (scatter hhibaking year if year == 2015, msymbol(Oh) mcolor(black) mlabcolor(black) mlabel(percent_changebaking) mlabpos(11)) ///
        (scatter hhifarming year if year == 2015, msymbol(Oh) mcolor(black) mlabcolor(black) mlabel(percent_changefarming) mlabpos(11)) ///
           (scatter hhifishing year if year == 2015, msymbol(Oh) mcolor(black) mlabcolor(black) mlabel(percent_changefishing) mlabpos(11))  ///
              (scatter hhilogging year if year == 2015, msymbol(Oh) mcolor(black) mlabcolor(black) mlabel(percent_changelogging) mlabpos(11))  ///
              (scatter hhimining year if year == 2015, msymbol(Oh) mcolor(black) mlabcolor(black) mlabel(percent_changemining) mlabpos(11))
      Thanks and let me know if this is what you were looking for!
      -Ben

      Comment

      Working...
      X