Announcement

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

  • Plotting multiple frequency histograms recast as lines at once

    I have a dataset of event instances that include information such as year, financial cost of event, state the event occurred in, etc.

    I can graph the total number of events per year with

    Code:
     graph twoway histogram year, discrete freq recast(line) xtitle("Year") ytitle("Event Count") title("Number of US Events Each Year")
    However, I want to subdivide the events based on the state in which they occurred in, and then have a separate line for each state. I'm trying to do that by subdividing the dataset into states and then renaming the year variable as "yearKentucky" and "yearVirginia",

    but then when I try to run

    Code:
     graph twoway histogram yearKentucky yearVirginia, discrete freq recast(line) xtitle("Year") ytitle("Event Count") title("Number of Events Each Year in Kentucky and Virginia")
    I have two many variables specified. Is there a quick workaround for this?
    Thank you muchly!



  • #2
    In essence you want a line plot and so there is no need to go via a histogram.

    Here is a code sketch. Many of the details will be different in your data. If you can't figure out a translation, you need please to give us a data example, as requested and explained in FAQ Advice #12.

    Code:
    bysort year state : gen count = _N
    
    line count year if state == "KY" || line count year if state == "VA",  legend(order(1 "Kentucky" 2 "VIrginia") ring(0) pos(5) col(1))
    However, if the real problem features many more than two states, you may need to rethink your strategy.

    Comment

    Working...
    X