Announcement

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

  • Stack bar graph with cumulative on secondary axis as line graph

    Click image for larger version

Name:	Wanted graph.png
Views:	1
Size:	279.8 KB
ID:	1712224
    I want to make the following graph using Stata. Could anyone please help
    Attached Files
    Last edited by Ijaz Ali; 04 May 2023, 03:04.

  • #2
    Would you post a Stata data file in an Excel forum? Please back up, read https://www.statalist.org/forums/help#stata and then use datex to post your data.

    Comment


    • #3
      Code:
      clear
      input int Year str3 Month int(stack_var_1 stack_var_2 stack_total_labels) long second_y double(stack_var_2_labels stack_var_1_labels)
      2022 "Jan"  1407  508  1915   1915                .27                .73
      2022 "Feb" 10048 3491 13539  15454                .26                .74
      2022 "Mar" 13310 5898 19208  34662                .31                .69
      2022 "Apr"  6339 5394 11733  46395                .46                .54
      2022 "May"  6583 5949 12532  58927                .47                .53
      2022 "Jun"  8460 6291 14751  73678                .43                .57
      2022 "Jul"  3534 4287  7821  81499                .55                .45
      2022 "Aug"  3730 6850 10580  92079                .65                .35
      2022 "Sep"  2351 6106  8457 100536                .72                .28
      2022 "Oct"  2370 7440  9810 110346                .76                .24
      2022 "Nov"  2013 6763  8776 119122                .77                .23
      2022 "Dec"  2138 7125  9263 128385                .77                .23
      2023 "Jan"  2257 6709  8966 137351                .75                .25
      2023 "Feb"  1728 6365  8093 145444                .79                .21
      2023 "Mar"  1698 5905  7603 153047                .78                .22
      2023 "Apr"  1204 4185  5389 158436                .78                .22
      end

      Comment


      • #4
        Thanks for posting data. You appear to be asking for something like the first graph below; in my view that would be fairly useless unless you plotted components and cumulatives on different scales, which I don't recommend. But separate panels might help, as in the second graph below.

        Code:
        clear
        input int Year str3 Month int(stack_var_1 stack_var_2 stack_total_labels) long second_y double(stack_var_2_labels stack_var_1_labels)
        2022 "Jan"  1407  508  1915   1915                .27                .73
        2022 "Feb" 10048 3491 13539  15454                .26                .74
        2022 "Mar" 13310 5898 19208  34662                .31                .69
        2022 "Apr"  6339 5394 11733  46395                .46                .54
        2022 "May"  6583 5949 12532  58927                .47                .53
        2022 "Jun"  8460 6291 14751  73678                .43                .57
        2022 "Jul"  3534 4287  7821  81499                .55                .45
        2022 "Aug"  3730 6850 10580  92079                .65                .35
        2022 "Sep"  2351 6106  8457 100536                .72                .28
        2022 "Oct"  2370 7440  9810 110346                .76                .24
        2022 "Nov"  2013 6763  8776 119122                .77                .23
        2022 "Dec"  2138 7125  9263 128385                .77                .23
        2023 "Jan"  2257 6709  8966 137351                .75                .25
        2023 "Feb"  1728 6365  8093 145444                .79                .21
        2023 "Mar"  1698 5905  7603 153047                .78                .22
        2023 "Apr"  1204 4185  5389 158436                .78                .22
        end
        
        gen mdate = monthly(Month + " " + strofreal(Year), "MY") 
        format mdate %tm_Mon_YY 
        twoway bar stack_var_1 mdate, xtitle("") || rbar stack_var_1 stack_total_labels mdate ///
        || line second_y mdate, legend(order(3 "cumulative" 2 "2" 1 "1") col(1) ring(0) pos(3)) ///
        name(G1, replace)
        
        twoway bar stack_var_1 mdate, xtitle("") || rbar stack_var_1 stack_total_labels mdate ///
        , legend(order(2 "2" 1 "1") col(1) ring(0) pos(1)) ytitle(1 and 2) ///
        name(G2, replace) xscale(off) 
        
        line second_y mdate, ytitle("cumulative") xtitle("") name(G3, replace)
        
        graph combine G2 G3, xcommon col(1) imargin(small)
        Click image for larger version

Name:	cumulative_G1.png
Views:	1
Size:	23.7 KB
ID:	1712335

        Click image for larger version

Name:	cumulative_G4.png
Views:	1
Size:	25.5 KB
ID:	1712336

        Comment


        • #5
          Thanks Nick Cox for your suggestions. Can we consider the second y axis for commutative variable as I shown in the graph attached? You are right that different scales lead to suppress the important variables but if we use the second y axis.

          Comment


          • #6
            Surely, that is another choice. Feel free to experiment.

            Comment


            • #7
              but I am unable to put that on secondary axis. Could you please help me in it?

              Comment


              • #8
                Sorry, no. I don’t suggest code for designs I don’t recommend.

                Comment

                Working...
                X