Announcement

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

  • Combining two line charts into one


    Hi, I want to combine a line plot that shows the percent of health levels >1, with one that shows the percent for male and female sex separately.

    I'd like to know if it's possible to show the three lines in the same plot, instead of just combing the two using the graph combine command.

    Thanks in advance for your help!

    Here is the dataex:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(mathLevel sex) int year
    4 1 2018
    3 2 2018
    4 1 2018
    1 2 2018
    1 2 2018
    2 1 2018
    3 2 2018
    1 2 2018
    1 2 2018
    1 1 2018
    1 2 2018
    3 1 2018
    3 2 2018
    3 2 2018
    3 1 2018
    1 2 2018
    2 1 2018
    3 2 2018
    2 1 2018
    2 2 2018
    2 2 2018
    3 1 2018
    3 2 2018
    3 2 2018
    2 2 2018
    2 2 2018
    2 2 2018
    1 1 2018
    1 2 2018
    3 2 2018
    4 2 2018
    3 1 2018
    4 2 2018
    2 2 2018
    1 1 2018
    3 2 2018
    2 2 2018
    4 1 2018
    1 2 2018
    4 1 2018
    3 2 2018
    4 2 2018
    1 1 2018
    3 2 2018
    1 2 2018
    3 2 2018
    1 2 2018
    2 2 2018
    3 2 2018
    1 2 2018
    1 2 2018
    1 2 2018
    4 2 2018
    4 1 2018
    4 2 2018
    1 1 2018
    4 2 2018
    3 1 2018
    4 2 2018
    4 2 2018
    3 2 2018
    3 2 2018
    2 2 2018
    1 1 2018
    3 1 2018
    1 2 2018
    3 1 2018
    3 2 2018
    1 1 2018
    2 1 2018
    1 1 2018
    . 2 2018
    2 2 2018
    1 1 2018
    2 2 2018
    3 1 2018
    2 2 2018
    3 2 2018
    1 2 2018
    1 1 2018
    2 2 2018
    2 2 2018
    2 1 2018
    2 2 2018
    3 1 2018
    1 1 2018
    4 2 2018
    3 2 2018
    2 1 2018
    3 2 2018
    3 2 2018
    4 1 2018
    1 1 2018
    3 1 2018
    1 2 2018
    1 2 2018
    3 2 2018
    3 2 2018
    3 2 2018
    4 2 2018
    end
    label values mathLevel A009
    label def A009 1 "Very good", modify
    label def A009 2 "Good", modify
    label def A009 3 "Fair", modify
    label def A009 4 "Poor", modify
    label values sex X001
    label def X001 1 "Male", modify
    label def X001 2 "Female", modify
    label values year YEAR
    label def YEAR 2018 "2018", modify

  • #2
    If it is a line plot, what is the percentage being plotted against? What is the "x" variable, so to speak? If it is year, then your data extract is not very useful, since all the year values are 2018.

    Be that as it may, the core technique is a combination of two things:
    • twoway line allows you to plot multiple "y" variables against the same "x" variable. See help line
    • to get the setup needed for the above, you can use the separate command to create individual performance variables for males and females; something like
      Code:
      separate mathLevel, by(sex) veryshortlabel
    Last edited by Hemanshu Kumar; 09 Jul 2023, 08:08.

    Comment


    • #3
      Originally posted by Hemanshu Kumar View Post
      If it is a line plot, what is the percentage being plotted against? What is the "x" variable, so to speak? If it is year, then your data extract is not very useful, since all the year values are 2018.

      Be that as it may, the core technique is a combination of two things:
      • twoway line allows you to plot multiple "y" variables against the same "x" variable. See help line
      • to get the setup needed for the above, you can use the separate command to create individual performance variables for males and females; something like
        Code:
        separate mathLevel, by(sex) veryshortlabel
      Hi Hemanshu, thanks for your quick reply.

      I am sorry that the dataex is not helpful. The years range from 2012-2022.
      I am not very familiar with the two-way line command, so it'd very helpful if you could give me the base code to get started with. Thank you.

      Comment


      • #4
        Consider this

        Code:
        gen byte notverygood = (mathLevel > 1)
        separate notverygood, by(sex)
        
        collapse (mean) notverygood*, by(year)
        label var notverygood1 "Male"
        label var notverygood2 "Female"
        label var notverygood "All"
        
        twoway line notverygood1 notverygood2 notverygood year

        Comment


        • #5
          Originally posted by Hemanshu Kumar View Post
          Consider this

          Code:
          gen byte notverygood = (mathLevel > 1)
          separate notverygood, by(sex)
          
          collapse (mean) notverygood*, by(year)
          label var notverygood1 "Male"
          label var notverygood2 "Female"
          label var notverygood "All"
          
          twoway line notverygood1 notverygood2 notverygood year
          Hi Hemanshu, Thanks so much indeed. I was able to make the chart using this method:
          Click image for larger version

Name:	Screenshot 2023-07-09 at 3.09.07 PM.png
Views:	1
Size:	844.9 KB
ID:	1719862

          Comment

          Working...
          X