Announcement

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

  • Multiple overlaid connected line graphs

    Hi! This is my first post, so please forgive any lack of clarity.

    I have a dataset with measurements (observations) from traffic sensors around a city over one year.

    Measurements are taken hourly by sensors that are in one of three possible neighbourhoods (variable name nb and it can assume value of either 0, 1 or 2).

    Each measurement has multiple variables including density (cars\lane-km) and speed (average speed). Moreover, each measurement has multiple variables for time in different granularities, like date_and_time, day_of_the_year, month_of_the_year, etc.

    I am asked to plot a graph for density and speed "using different color and shape to denote the observations across these three groups."


    I tried with twoway connected but it the graph was just full of dots everywhere, plus it wouldn't be correct anyway because I had no idea on how to differentiate by neighbourhood.

    Anny ideas?

    Thank you so much in advance.

  • #2
    please provide a data example by typing -dataex- in Stata and copying the output in the results window

    Comment


    • #3
      Takk Oyvind!

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float tid double(date_time date) float(year month day week hour density) double speed float ringinterval
       1 1.7041536e+12 19724 2014 1 1 3 0 4.5952687 78.05 1
       2 1.7041572e+12 19724 2014 1 1 3 1  11.84449 53.33 0
       3 1.7041608e+12 19724 2014 1 1 3 2  2.492961 80.71 1
       4 1.7041644e+12 19724 2014 1 1 3 3 1.0735373 55.89 2
       5  1.704168e+12 19724 2014 1 1 3 4  7.488051 75.32 0
       6 1.7041716e+12 19724 2014 1 1 3 5  .9792957 77.63 2
       7 1.7041752e+12 19724 2014 1 1 3 6  5.766262 69.08 0
       8 1.7041788e+12 19724 2014 1 1 3 7  10.35987 55.02 0
       9 1.7041824e+12 19724 2014 1 1 3 8 2.6912215  89.3 2
      10  1.704186e+12 19724 2014 1 1 3 9  5.988253 76.33 1
      end
      format %tc date_time
      format %td date
      label values week DOW
      label def DOW 3 "Wednesday", modify
      Last edited by Johan Lundqvist; 02 Feb 2022, 13:09.

      Comment


      • #4
        I also did list in 1/5 in case it's easier to understand.

        The variable of interest are ringinterval (the last one, it's the neighbourhood), density, speed.

        Code:
         +------------------------------------------------------------------------------------------------------------+
          1. | tid |          date_time |      date | year | month | day |      week | hour  | spd  |   vsb  |      temp  |
             |   1 | 01jan2014 00:00:00 | 01jan2014 | 2014 |     1 |   1 | Wednesday |    0  |   2  |  6.08  |         6  |
             |--------------------------+-----------+---------------------------------------------------------------------|
             |       rh | winddir | wd0 | wd1 | wd2 | wd3 | wd4 | wd5 | wd6 | wd7 | wd8 | wd9 | wd10 | wd11 | wd12 | wd13 |
             | 25.15113 |      SE |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   1 |   0 |   0 |    0 |    0 |    0 |    0 |
             |--------------------+-----+-----------------------------------------------+---------------------------------|
             | wd14 | wd15 | wd16 | skc | sky0 | sky1 | sky2 | sky3  | sky4  |     rid  | speed  |      ring  |  density  |
             |    0 |    0 |    0 |   . |    0 |    0 |    0 |    0  |    0  | HI9011b  | 78.05  | (3rd,4th]  | 4.595269  |
             |------------------------------------------------------------------------------------------------------------|
             |                  _merge              |              peak              |              ringin~l              |
             |             Matched (3)              |                 0              |                     1              |
             +------------------------------------------------------------------------------------------------------------+
        
             +------------------------------------------------------------------------------------------------------------+
          2. | tid |          date_time |      date | year | month | day |      week | hour  | spd  |   vsb  |      temp  |
             |   2 | 01jan2014 01:00:00 | 01jan2014 | 2014 |     1 |   1 | Wednesday |    1  |   5  |  6.08  |         7  |
             |--------------------------+-----------+---------------------------------------------------------------------|
             |       rh | winddir | wd0 | wd1 | wd2 | wd3 | wd4 | wd5 | wd6 | wd7 | wd8 | wd9 | wd10 | wd11 | wd12 | wd13 |
             | 14.90952 |     WNW |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |    0 |    0 |    0 |    0 |
             |--------------------+-----+-----------------------------------------------+---------------------------------|
             | wd14 | wd15 | wd16 | skc | sky0 | sky1 | sky2 | sky3  | sky4  |     rid  | speed  |      ring  |  density  |
             |    1 |    0 |    0 |   . |    0 |    0 |    0 |    0  |    0  | HI3002b  | 53.33  |    (<2nd]  | 11.84449  |
             |------------------------------------------------------------------------------------------------------------|
             |                  _merge              |              peak              |              ringin~l              |
             |             Matched (3)              |                 0              |                     0              |
             +------------------------------------------------------------------------------------------------------------+
        
             +------------------------------------------------------------------------------------------------------------+
          3. | tid |          date_time |      date | year | month | day |      week | hour  | spd  |   vsb  |      temp  |
             |   3 | 01jan2014 02:00:00 | 01jan2014 | 2014 |     1 |   1 | Wednesday |    2  |   5  |  6.08  |         8  |
             |--------------------------+-----------+---------------------------------------------------------------------|
             |       rh | winddir | wd0 | wd1 | wd2 | wd3 | wd4 | wd5 | wd6 | wd7 | wd8 | wd9 | wd10 | wd11 | wd12 | wd13 |
             |   13.926 |     WNW |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |    0 |    0 |    0 |    0 |
             |--------------------+-----+-----------------------------------------------+---------------------------------|
             | wd14 | wd15 | wd16 | skc | sky0 | sky1 | sky2 | sky3  | sky4  |     rid  | speed  |      ring  |  density  |
             |    1 |    0 |    0 |   . |    0 |    0 |    0 |    0  |    0  | HI9019b  | 80.71  | (3rd,4th]  | 2.492961  |
             |------------------------------------------------------------------------------------------------------------|
             |                  _merge              |              peak              |              ringin~l              |
             |             Matched (3)              |                 0              |                     1              |
             +------------------------------------------------------------------------------------------------------------+
        
             +------------------------------------------------------------------------------------------------------------+
          4. | tid |          date_time |      date | year | month | day |      week | hour  | spd  |   vsb  |      temp  |
             |   4 | 01jan2014 03:00:00 | 01jan2014 | 2014 |     1 |   1 | Wednesday |    3  |   1  | 11.04  | 9.1999998  |
             |--------------------------+-----------+---------------------------------------------------------------------|
             |       rh | winddir | wd0 | wd1 | wd2 | wd3 | wd4 | wd5 | wd6 | wd7 | wd8 | wd9 | wd10 | wd11 | wd12 | wd13 |
             | 19.10794 |      NW |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |    0 |    0 |    0 |    0 |
             |--------------------+-----+-----------------------------------------------+---------------------------------|
             | wd14 | wd15 | wd16 | skc | sky0 | sky1 | sky2 | sky3  | sky4  |     rid  | speed  |      ring  |  density  |
             |    0 |    1 |    0 |   . |    0 |    0 |    0 |    0  |    0  | HI9644c  | 55.89  |    (>5th)  | 1.073537  |
             |------------------------------------------------------------------------------------------------------------|
             |                  _merge              |              peak              |              ringin~l              |
             |             Matched (3)              |                 0              |                     2              |
             +------------------------------------------------------------------------------------------------------------+
        
             +------------------------------------------------------------------------------------------------------------+
          5. | tid |          date_time |      date | year | month | day |      week | hour  | spd  |   vsb  |      temp  |
             |   5 | 01jan2014 04:00:00 | 01jan2014 | 2014 |     1 |   1 | Wednesday |    4  |   9  | 11.04  |        10  |
             |--------------------------+-----------+---------------------------------------------------------------------|
             |       rh | winddir | wd0 | wd1 | wd2 | wd3 | wd4 | wd5 | wd6 | wd7 | wd8 | wd9 | wd10 | wd11 | wd12 | wd13 |
             | 13.23812 |     WNW |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |    0 |    0 |    0 |    0 |
             |--------------------+-----+-----------------------------------------------+---------------------------------|
             | wd14 | wd15 | wd16 | skc | sky0 | sky1 | sky2 | sky3  | sky4  |     rid  | speed  |      ring  |  density  |
             |    1 |    0 |    0 |   . |    0 |    0 |    0 |    0  |    0  | HI4034a  | 75.32  | (2nd,3rd]  | 7.488051  |
             |------------------------------------------------------------------------------------------------------------|
             |                  _merge              |              peak              |              ringin~l              |
             |             Matched (3)              |                 0              |                     0              |
             +------------------------------------------------------------------------------------------------------------+
        
        .
        Last edited by Johan Lundqvist; 02 Feb 2022, 13:13.

        Comment


        • #5
          It just dawned on me that this may be homework and as per FAQ should be ignored. Regardless, you're on the right track with twoway connect,
          Code:
          twoway connect density tid if ringinterval==0 || ///
                 connect density tid if ringinterval==1 || ///
                 connect density tid if ringinterval==2 || ///
                 ,legend(label(1 "ringinterval==0") ///
                         label(2 "ringinterval==1") ///
                         label(3 "ringinterval==2"))

          Comment


          • #6
            Oh, I apologize, I was not aware hw should be ignored.
            Thank you so much for your explanation!

            Comment

            Working...
            X