Announcement

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

  • Line Plots : Count and Proportions

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year str1 firm str7 region float id
    2016 "A" "America" 1
    2016 "B" "America" 2
    2016 "C" "Europe"  3
    2016 "D" "Asia"    4
    2017 "A" "America" 1
    2017 "B" "America" 2
    2017 "C" "Europe"  3
    2017 "D" "Asia"    4
    2017 "E" "Europe"  5
    2018 "A" "America" 1
    2018 "B" "America" 2
    2018 "C" "Europe"  3
    2018 "D" "Asia"    4
    2018 "E" "Europe"  5
    2018 "F" "America" 6
    end
    format %ty year
    My data set contains four variables: year, firm, region, and id. I would like to generate two graphs.

    In graph 1, I want to produce line plots that would present number of firms based on their region. To be precise, I want to plot number of firms in America, Asia, and Europe over time.

    In graph 2, I want to do similar thing; instead of number I want to plot proportions.

  • #2
    If each firm occurs at most once in each region and year then

    Code:
    isid firm region year
    will receive silent assent, after which

    Code:
     
    bysort region year : gen count = _N 
    separate count, by(region) veryshortlabel
    line count? year
    is a way forward.

    Comment


    • #3
      Thanks a lot Nick Cox! Inspired by your code, I have made an attempt to plot proportions.
      Code:
      bysort year region: gen total = _N
      bysort year: gen prop = total/_N
      separate prop, by(region) veryshortlabel
      line prop? year
      Is it okay? Does better solution exist?

      Comment

      Working...
      X