Announcement

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

  • Twoway connected graph with two legends

    Dear all,
    I am trying to plot the audit cycles of companies for small, medium and large companies over the years depending on the government ideology (leftwing, center or rightwing).
    I want to compare the development of the audit cycles over the years depending on the government ideology. So far, I have tried to use the following command:
    Code:
    twoway connected cycle_bc cycle_mc cycle_sc year, xlabel(2000(1)2020, labsize(vsmall)) /*
    */ legend(label(3 "Rightwing government") label(2 "Leftwing government") label(1 "Center government") size(medium)) /*
    */ lcolor(gs7 red black) mcolor(gs7 red black) mlcolor(gs7 red black) ytitle("Exam quota", size(small)) /*
    */ xtitle("Years") title("Exam quotas companies (in years)", size(medium))
    With this command, I get the following graph:
    Click image for larger version

Name:	combined_cycles.png
Views:	1
Size:	14.2 KB
ID:	1655841

    The lowest scatter plot refers to big companies, the medium one to medium companies and the upper one to small companies. What I am trying to do is to turn the scatterplot black in years with rightwing government, red in years with leftwing governments and grey in years with center governments.
    So far, the legend is mixing up the government ideology with the company sizes and the colors are also mixed up for the different government ideologies.

    Is there a way to generate two legends? One legend with the different company sizes and one with the different government ideologies?
    Or is there any other command that is working better?

    Thank you very much in advance for your help, any help is highly appreciated!

    Thanks a lot,
    Chris
    Last edited by Christian Traub; 23 Mar 2022, 12:19.

  • #2
    Originally posted by Christian Traub View Post

    Is there a way to generate two legends? One legend with the different company sizes and one with the different government ideologies?
    Or is there any other command that is working better?

    Just include the company size as text within the plot. With two legends, you will end up causing more confusion. Paraphrasing Nick Cox, if you can lose the legend, the better. No data example, so provide one if you need code suggestions or other alternatives (see FAQ Advice #12 for details).

    Comment


    • #3
      Andrew Musau
      Thanks a lot for your reply and sorry for not providing more detail!
      The data looks like the following:
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float year double(cycle_bc cycle_mc cycle_sc) str20 policy_colour
      2010 4.91 15.29 32.24 "Rightwing government"
      2011 4.57 16.33 34.12 "Rightwing government"
      2012 4.53 15.22 36.69 "Leftwing government"
      2013 4.68 14.02  32.3 "Leftwing government"
      2014 4.52 13.57 31.41 "Leftwing government"
      2015 4.48 12.76 29.28 "Leftwing government"
      2016 4.52 12.91  29.8 "Leftwing government"
      2017 4.26 12.48 27.64 "Center government"   
      end
      How do I insert the company size as a text within the plot?

      What I am trying to code is that the lines for small, medium and big companies turn black in years with a rightwing government (before 2012), red in years with a leftwing government (2012-2016), and grey in years with a center government (after 2016).
      Do you have any suggestions how I can code this?

      Thank you very much in advance for your time and help!

      Comment


      • #4
        Here is some technique. In principle you could plot 9 variables, and not just 3.


        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float year double(cycle_bc cycle_mc cycle_sc) str20 policy_colour
        2010 4.91 15.29 32.24 "Rightwing government"
        2011 4.57 16.33 34.12 "Rightwing government"
        2012 4.53 15.22 36.69 "Leftwing government"
        2013 4.68 14.02  32.3 "Leftwing government"
        2014 4.52 13.57 31.41 "Leftwing government"
        2015 4.48 12.76 29.28 "Leftwing government"
        2016 4.52 12.91  29.8 "Leftwing government"
        2017 4.26 12.48 27.64 "Center government"   
        end
        
        foreach x in sc mc bc { 
            su cycle_`x' if year == 2017 
            local `x' = r(min)
            
        }
        
        reshape long cycle_ , i(year) j(which) string 
        
        separate cycle_, by(policy_colour)
        sort which year 
        list 
        
        set scheme s1color 
        
        local opts1 ms(none) mlabpos(12) mlabsize(medium)
        local opts2 ms(none) mlabpos(3) mlabsize(medium)
        
        twoway connected cycle_3 year, c(L) mc(black) lc(black) || connected cycle_2 year, c(L) legend(off) mc(red) lc(red) || scatter cycle_1 year, mc(gs8) ytitle(whatever it is) xla(2010/2017) yla(, ang(h)) xsc(r(. 2018.2)) ///
        || scatteri 38 2010.5 "right-wing", `opts1' mlabc(black) /// 
        || scatteri 38 2014   "left-wing", `opts1'  mlabc(red)   /// 
        || scatteri 38 2017   "center",   `opts1'  mlabc(gs12)  /// 
        || scatteri `sc' 2017.2 "small",    `opts2'  mlabc(blue)  ///
        || scatteri `mc' 2017.2 "medium",   `opts2'  mlabc(blue)  ///
        || scatteri `bc' 2017.2 "big",      `opts2'  mlabc(blue)


        Click image for larger version

Name:	leftrightcenter.png
Views:	1
Size:	30.1 KB
ID:	1656102

        Comment


        • #5
          Nick Cox
          Thank you very much for your answer, time and help! It works perfectly now, I deeply appreciate your help!!

          Comment

          Working...
          X