Announcement

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

  • Adding a title to each subgraph using by option

    Hello,

    I am using the following code:

    Code:
     twoway line log_death_year_NS year, c(l) yaxis(1) by(NorthAfrica, yrescale title("My Title")) title("1, 2") ///
     legend(label(1 21) label(2 22) label(3 23)) /// 
     || line log_actors_class2_year_NS year, c(l) yaxis(2) by(NorthAfrica) ///
     || line log_actors_class1_year_NS year, c(l) yaxis(2) by(NorthAfrica)
    And this is the output that I obtain:


    Click image for larger version

Name:	Graph_Problem2.PNG
Views:	1
Size:	128.3 KB
ID:	1653037




    I would like to put the title "1" to the first graph and the title "2" to the second. Instead, with my code I am writing 1,2 and 1,2 (look at the highlighted part). Any idea about how to solve it?


  • #2
    Code:
    label define NorthAfrica 0 "1" 1 "2"
    label values NorthAfrica NorthAfrica

    Comment


    • #3
      Thank you Andrew Musau for your comment. However, I am not able to do it, where or when should I write the code you suggested to me?

      Comment


      • #4
        I think Andrew Musau's point is that the by() option will pick up the value labels of its variable it has any. So, you need to assign those before the graph call.


        Run this sequence to see the point.


        Code:
        . sysuse auto, clear
        . histogram mpg, by(foreign)
        . histogram mpg, by(rep78)
        . label def rep78 1 abysmal
        . label val rep78 rep78
        . histogram mpg, by(rep78)
        In your case it seems that you have a variable with values 0 and 1 but you want to see as text 1 and 2 so adding 1 to that variable (or generating a new variable by adding 1) springs to mind as an alternative.


        Comment


        • #5
          Thank you Nick Cox . Yes, my variable has values 0 and 1. I said 1 and 2 but it's just an example. What I really want is to modify the title of the subgraphs (not the main one).

          One option could be to change the values of the variable. However, in that case my variable has two values, but if it has 5 or 6, I would like to be able to change the name of the subgraphs 5 or 6. Is not possible to do it?
          Last edited by Diego Malo; 05 Mar 2022, 08:22.

          Comment


          • #6
            Run Nick's code in #4 to see what it is doing. The subtitles correspond to the labels of the -by- variable.

            Comment


            • #7
              Diego Malo I am at a loss to understand your replies.

              Andrew Musau explained how to change subgraph titles and I explained why and how that works. Did you try to apply that to your data? We can't do that for you in the absence of example data.

              Comment


              • #8
                Thank you Nick Cox and Andrew Musau . I did what you suggested and it works.

                The only concern I wanted to express with my replies is just it seems strange to me that I cannot modify the title of the subgraphs directly with some option in twoway command.

                Comment


                • #9
                  You can’t alter the data on the fly either. That’s the principle here.

                  You can change titles otherwise— but principles sometimes clash…
                  Last edited by Nick Cox; 06 Mar 2022, 03:37.

                  Comment

                  Working...
                  X