Announcement

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

  • Graphing with 3 variables (two on one axis)

    Hello all,

    I'm trying to graph some raw data of two variables man hours (y-axis) and cumulative production (x-axis).
    This was working just fine:

    Code:
     twoway line mhpp cpn
    Now, I need to overlay the same data from a few different companies. To make the comparison stronger, I wanted to add in time as another variable (ideally on the x-axis). For instance:

    input str8 Company float ID str9 Time double mhpp float cpn
    "Ford" 1 "01/1941" 10 100
    "Ford" 1 "02/1941" 9 160
    "Ford" 1 "03/1941" 8 190
    "Goodyear" 2 "01/1941" . .
    "Goodyear" 2 "02/1941" . .
    "Goodyear" 2 "03/1941" 9 200
    "Buick" 3 "01/1941" . .
    "Buick" 3 "02/1941" 7 150
    "Buick" 3 "03/1941" 6.5 200

    In the above data, Ford is producing for the entire period, whereas Goodyear starts production two months later. Ideally, the graph would plot the mhpp on the y-axis, the cpn and time on the x-axis (so that I can see when companies start production at later dates and how their production compares in Time).

    Does anyone have any ideas on how to do this?

  • #2
    There are a limited number of ways of showing a third dimension in a two-dimensional graph. 2 x-axes are out of the question unless there is a perfect correlation between the to be x-variables. In the following, labels are one way to add a third dimension.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str8 Company float ID str9 Time double mhpp float cpn
    "Ford"     1 "01/1941"  10 100
    "Ford"     1 "02/1941"   9 160
    "Ford"     1 "03/1941"   8 190
    "Goodyear" 2 "01/1941"   .   .
    "Goodyear" 2 "02/1941"   .   .
    "Goodyear" 2 "03/1941"   9 200
    "Buick"    3 "01/1941"   .   .
    "Buick"    3 "02/1941"   7 150
    "Buick"    3 "03/1941" 6.5 200
    end
    
    twoway (line mhpp cpn if ID==1) (line mhpp cpn if ID==3) ///
    (scatter mhpp cpn if ID==1, mc(none) mlab(Time) ///
    mlabpos(12)) (scatter mhpp cpn if ID==3, mc(none) mlab(Time) ///
    mlabpos(12) scheme(s1color) legend(order(1 "Ford" 2 "Buick")) ///
    xsc(r(95 210)) ysc(r(. 10.2)))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	53.5 KB
ID:	1580046

    Comment


    • #3
      Hi Andrew,

      Thanks for the response!

      So, I used:

      Code:
      twoway (line mhpp cpn if Fullid==77) (line mhpp cpn if Fullid==69) (line mhpp cpn if Fullid==38) (line mhpp cpn if Fullid==29) (line mhpp cpn if Fullid==110) (scatter mhpp cpn if Fullid==77, mc(none) mlab(Time) mlabpos(12)) (scatter mhpp cpn if Fullid==69) (scatter mhpp cpn if Fullid==38)(scatter mhpp cpn if Fullid==29) (scatter mhpp cpn if Fullid==110), scheme(slcolor) legend(order(1 "Ford" 2 "Douglas" 3 "Consolidated San Diego" 4 "Consolidated Ft. Worth" 5 "North American" )) xsc(r(95 210)) ysc(r(. 10.2))
      I couldn't fit all the data points in the dataex example I gave. My problem now is that I've got five different companies over 72 months—so the marker labels overlap in a way that makes the graph pretty unintelligible. Is there a way to hide all the marker labels? Or only include the first one?
      Attached Files

      Comment


      • #4
        You can show the first and the last time-periods. The markers should be invisible, they are there just to show the labels. However, I think a bigger problem beyond the labels is the spaghetti problem, see https://journals.sagepub.com/doi/abs...36867X19893641 for some techniques to overcome this. Here is how to show only first and last time-periods.

        Code:
        drop if missing(cpn)| missing(mhpp)
        bys Fullid (Time): gen tag= cond(!missing(mhpp) & !missing(cpn) ///
        &_n==1, 1, cond(!missing(mhpp) & !missing(cpn) &_n==_N, 1, 0))
        In each of the scatter plots, you need to add "& tag, mc(none) mlab(Time) mlabpos(12)", e.g.,

        Code:
        twoway (scatter mhpp cpn if Fullid==69 & tag, mc(none) mlab(Time) mlabpos(12)) ///
        (scatter mhpp cpn if Fullid==38 & tag, mc(none) mlab(Time) mlabpos(12))
        Also, add ranges that make sense for your graph

        Code:
        xsc(r(-800 8700)) ysc(r(. 23)))
        Finally, it's scheme s1color (number 1, not letter l).



        Comment


        • #5
          Hi Andrew,

          So, I gave this a go:
          Code:
          drop if missing(cpn)| missing(mhpp)
          bys Fullid (Time): gen tag= cond(!missing(mhpp) & !missing(cpn) ///
          &_n==1, 1, cond(!missing(mhpp) & !missing(cpn) &_n==_N, 1, 0))
          
          twoway (line mhpp cpn if Fullid==69 & tag, mc(none) mlab(Time) mlabpos(12)) ///
          (line mhpp cpn if Fullid==38 & tag, mc(none) mlab(Time) mlabpos(12)) ///
          (line mhpp cpn if Fullid==77 & tag, mc(none) mlab(Time) mlabpos(12)) (line mhpp cpn if Fullid==29 & tag, mc(none) mlab(Time) mlabpos(12)) ///
          (line mhpp cpn if Fullid==110 & tag, mc(none) mlab(Time) mlabpos(12)), scheme(s1color) legend(order(1 "Ford" 2 "Douglas" 3 "Consolidated San Diego" 4 "Consolidated Ft. Worth" 5 "North American" )) xsc(r(-800 8700)) ysc(r(. 23))
          But this ends up making my data look linear. I've attached the before shot (non-linear) to show what the data actually looks like. Plus, the lines don't actually start at different points as I had aimed to do in the first place...I'm not sure how to get that back.
          Attached Files

          Comment


          • #6
            #4, the tag is attached to the scatter plots, not the line graphs.

            Code:
            drop if missing(cpn)| missing(mhpp)
            bys Fullid (Time): gen tag= cond(_n==1, 1, cond(_n==_N, 1, 0))
            
            twoway(line mhpp cpn if Fullid==69) (line mhpp cpn if Fullid==38) ///
            (line mhpp cpn if Fullid==77) (line mhpp cpn if Fullid==29) ///
            (line mhpp cpn if Fullid==110) ///
            (scatter mhpp cpn if Fullid==69 & tag, mc(none) mlab(Time) mlabpos(12)) ///
            (scatter mhpp cpn if Fullid==38 & tag, mc(none) mlab(Time) mlabpos(12)) ///
            (scatter mhpp cpn if Fullid==77 & tag, mc(none) mlab(Time) mlabpos(12)) ///
            (line mhpp cpn if Fullid==29 & tag, mc(none) mlab(Time) mlabpos(12)) ///
            (scatter mhpp cpn if Fullid==110 & tag, mc(none) mlab(Time) mlabpos(12)), ///
            legend(order(1 "Ford" 2 "Douglas" 3 "Consolidated San Diego" 4 "Consolidated Ft. Worth" 5 "North American")) ///
            scheme(s1color) xsc(r(-800 8700)) ysc(r(. 23))

            Comment


            • #7
              Andrew,

              You've been most helpful, thank you. I've got it sorted now.

              Comment

              Working...
              X