Announcement

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

  • Twoway line graph with non-integer intervals

    Hi,
    I want to create a two-way line graph with the line restricted between two years value. The problem is the subcommand "if" doesn't work since I want to start in the middle of the year whereas my year variable only contains only integer value of years (2011,2012,2013,...). I want the line to start at decimal values (line 2013.5, 2014.,...), which is in the middle of the interval. Two-way graphs seem to automatically round the year range so that it is contained within an integer value range.

    Below is a simple replication example:

    Code:
    clear 
    set obs 10
    gen year = _n + 2010
    gen value = 2
    twoway (line value year if year >= 2013.5 & year <= 2015.5), xlabel(2011(1)2020)

    The line is between 2014 and 2016 whereas I want it to between 2013.5 and 2015.5.
    Thank you!






  • #2
    This is my approach but it seems too complicated. Really appreciate any input on how to streamline this!

    Code:
    clear 
    set obs 10
    gen year = _n + 2010
    gen value = 2
    expand 2 if year == 2013
    duplicates tag year, gen(tag)
    bysort year: gen num = _n if tag == 1
    replace year = 2013.5 if num == 2
    
    twoway (line value year if year >= 2013.5 & year <= 2015.5),  xlabel(2011(1)2020)

    Comment


    • #3
      There is no "automatic rounding" here.

      You don't have data points for 2013.5 or 2015.5 so line sees nothing to plot. Its role is to draw line segments between data points, and nothing else.

      You would get what you want if you had data points corresponding to your specification.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(value year)
      2 2013.5
      2 2015.5
      end
      
      line value year
      What you want can also be done with

      Code:
      twoway scatteri 2 2013.5 2 2015.5, recast(line)
      Detail: if is a qualifier, not a subcommand, not that that is material to your problem.

      Comment

      Working...
      X