Announcement

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

  • Chart: Two Y axes

    Hello guys,

    I need to create a chart with 2 Y axes. One axis is the total number of fights and the other total number of days for by time (pre , during, and post) by different group. My data is in a wide format and it is at the student level. The main idea is to see the total number of fights each group was involved (sum) and also the total number of days. I could generate two different charts but I would like to combine them. One Y axis can be bars (fights) and the other lines (days). After reading
    HTML Code:
    http://www.stata-journal.com/sjpdf.html?articlenum=gr0047
    , I learned that I need to collapse my data first in order to create this type of chart. I reshaped and collapsed my data to create the dataset below.



    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte era str5 unit int(fights days)
    1 "prog1" 320 10149
    2 "prog1" 105  1696
    3 "prog1" 153  5368
    1 "prog2"  36  1282
    2 "prog2"   8   194
    3 "prog2"  32   593
    1 "prog3" 118 10289
    2 "prog3"  15  3863   
    3 "prog3" 168 13881
    end
    Thank you in advance,
    Marvin
    Attached Files

  • #2
    No need to respond; I found the answer.

    Code:
    twoway bar fights era, yaxis(1) yscale(range(0) axis(1)) ///
     || line days era , sort yaxis(2) yscale(range(0) axis(2)) ///
     || scatter fights era, mlabel(fights) mlabpos(12) m(none) yaxis(1) ///
     || scatter days era ,mlabel(days) mlabpos(3) m(none) yaxis(2) ///
     || ,legend(order(1 3)) by(unit)
    Thank you!
    Marvin

    Comment


    • #3
      hello guys,

      Sorry to come back to this. I tried to find the answer myself but I couldn't. Although I figured how to create this graph, I need to make some chances to make it look better. I made some changes to improve the graph but i need more. For example I don't know how to edit the secondary Y axis such as making the number horizontal and insert a title. Also I would like to insert a comma separator for numbers in the 1000s. Finally, is there a way to change the color of the bar for pre , during and post (blue red , greenstand Stata colors)?

      One last thing, perhaps the graph looks better without the Y axes since the legend and label tell what they are. How can I remove the Y axis.

      Thank you much!


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input byte era str5 unit int(fights days)
      1 "prog1" 320 10149
      2 "prog1" 105  1696
      3 "prog1" 153  5368
      1 "prog2"  36  1282
      2 "prog2"   8   194
      3 "prog2"  32   593
      1 "prog3" 118 10289
      2 "prog3"  15  3863
      3 "prog3" 168 13881
      end
      Code:
      # delimit ; 
      twoway bar fights era, yaxis(1) yscale(range(0) axis(1)) ///
       || line days era , sort yaxis(2) yscale(range(0) axis(2)) ///
       || scatter fights era, mlabel(fights) mlabpos(12) m(none) yaxis(1) ///
       || scatter days era ,mlabel(days) mlabpos(3) m(none) yaxis(2) ///
       || ,legend(order(1 3)) by(unit, row(1))
           yla(, format("%1.0f") ang(h))
          ytitle("Fights")
          ytitle("Days", axis(2)) 
          xlabel(1"Pre" 2"During" 3"Post") ;
      # delimit cr


      Comment

      Working...
      X