Announcement

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

  • Grouped bar chart with secondary Y-axis

    I created this grouped bar chart using this command

    graph bar (mean) A_MEAN top_4_transition_share, over(leave_rank)
    Click image for larger version

Name:	grouped_bar.png
Views:	1
Size:	40.5 KB
ID:	1749095

    How can I add a secondary Y-axis to this plot? The red series is average annual salary in $ and the light blue series is probability (0-1) variable. I need to add another Y-axis with a different scale.

    I tried twoway bar, but I could not allow the option over() to divide the plot into 3 categories.

  • #2
    Short answer: You can't get further with graph bar. It doesn't support dual y axes.

    In contrast the lack of an over() option to twoway bar is a minor barrier. Just calculate what you want to show with egen, mean() first.

    Comment


    • #3
      I tried this twoway bar command:
      twoway bar A_MEAN leave_rank, yaxis(1) by(leave_rank) || bar top_4_transition_share leave_rank, yaxis(2) by(leave_rank)

      Click image for larger version

Name:	grouped_bar_2.png
Views:	1
Size:	26.5 KB
ID:	1749102

      I do not know why the two series are not plotted properly. I collapsed my data to the averages level.

      Click image for larger version

Name:	Screenshot 2024-04-06 at 7.07.54 PM.png
Views:	1
Size:	64.4 KB
ID:	1749103

      Comment


      • #4
        I can see three problems in your code.

        The by() option doesn't help.

        You're doing nothing to stop occlusion of one outcome's bars by the other outcome's bars.

        You need to insist on base 0 in each case.

        Here is some token code, Please note the use of dataex. Giving us data as a screenshot is not as helpful as you hope. FAQ #12 explains.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float(var1 var2 var3)
        1  79450.5 .2519406
        2 57310.77 .2501602
        3  32472.5 .2300236
        end
        
        gen var1_left = var1 - 0.2 
        gen var1_right = var1 + 0.2 
        
        twoway bar var2 var1_left, yla(0(2e4)8e4, axis(1)) yaxis(1) base(0) barw(0.4) || bar var3 var1_right, yaxis(2) yla(0(0.05)0.25, axis(2)) barw(0.4) base(0) xla(1/3) xtitle(var1)

        Comment


        • #5
          Thank you so much Dr. Cox!

          Comment

          Working...
          X