Announcement

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

  • Code for retiving valualabel in a graph

    I have a dataset containing values for different sector. The variable describing the sector (settore) has "value" from 1 to 5 and "value label" describing the sector ("Agricoltura" "Costruzioni" "Industria" "Servizi" "Totale Economia").
    I want to produce the 5 graphs with a "foreach" or "forvalue" loop, with each title corresponding to the "value label" as well as saving with the name of the sector.

    The code:
    Code:
    forvalues i=1(1)5 {
        twoway bar DLVA_lavva DLVA_capva DLVA_dltfp anno if settore==`i', barwidth(0.7 0.7 0.7) || line dlva anno  if settore==`i', ///
        yaxis(2) xlabel(1996(2)2022) ytitle("", axis(2)) legend(pos(6) row(2)) title(`i')
        graph save "Graph" "Graph_`i'.gph", replace
    Works well in producing the 5 graphs, but puts the title and the name as "value" (1 to 5) not "value label".

    How can I retrive the "value label" with a code like this?

    Thanks,
    M

  • #2
    Replace

    title(`i')
    with

    Code:
    title("`:lab (settore) `i''")

    Comment


    • #3
      Without sight of a graph or a data example or your use of a public dataset. I am guessing a little but this may help

      Code:
      forvalues i=1/5 {
          local mytitle : label (settore) `i'
      
          twoway bar DLVA_lavva DLVA_capva DLVA_dltfp anno if settore==`i', barwidth(0.7 0.7 0.7) ///
          || line dlva anno  if settore==`i', ///
          yaxis(2) xlabel(1996(2)2022) ytitle("", axis(2)) legend(pos(6) row(2)) title("`mytitle'")
      
          graph save "Graph" "Graph_`i'.gph", replace
      }
      That said, have you considered this?

      Code:
        twoway bar DLVA_lavva DLVA_capva DLVA_dltfp anno if settore==`i', barwidth(0.7 0.7 0.7) ///
          || line dlva anno, by(settore)  ///
          yaxis(2) xlabel(1996(2)2022) ytitle("", axis(2)) legend(pos(6) row(2))
      That said, your design just superimposes bars for three variables against year and so there is a major risk of later bars occluding earlier bars unless values follow particular inequalities,

      The bars won't be stacked, if that is what you are seeking.

      This thread surely needs a cross-reference to https://www.statalist.org/forums/for...graph-in-stata The indications are that clear bar graphs are unlikely with this kind of data.

      EDIT: Andrew Musau's suggestion on your question is essentially identical.
      Last edited by Nick Cox; 29 Jan 2025, 03:02.

      Comment


      • #4
        Originally posted by Andrew Musau View Post
        Replace



        with

        Code:
        title("`:lab (settore) `i''")
        This works fine (both for the title and for the name of the saved file). Thank you.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          Without sight of a graph or a data example or your use of a public dataset. I am guessing a little but this may help

          Code:
          forvalues i=1/5 {
          local mytitle : label (settore) `i'
          
          twoway bar DLVA_lavva DLVA_capva DLVA_dltfp anno if settore==`i', barwidth(0.7 0.7 0.7) ///
          || line dlva anno if settore==`i', ///
          yaxis(2) xlabel(1996(2)2022) ytitle("", axis(2)) legend(pos(6) row(2)) title("`mytitle'")
          
          graph save "Graph" "Graph_`i'.gph", replace
          }
          That said, have you considered this?

          Code:
          twoway bar DLVA_lavva DLVA_capva DLVA_dltfp anno if settore==`i', barwidth(0.7 0.7 0.7) ///
          || line dlva anno, by(settore) ///
          yaxis(2) xlabel(1996(2)2022) ytitle("", axis(2)) legend(pos(6) row(2))
          That said, your design just superimposes bars for three variables against year and so there is a major risk of later bars occluding earlier bars unless values follow particular inequalities,

          The bars won't be stacked, if that is what you are seeking.

          This thread surely needs a cross-reference to https://www.statalist.org/forums/for...graph-in-stata The indications are that clear bar graphs are unlikely with this kind of data.

          EDIT: Andrew Musau's suggestion on your question is essentially identical.
          I think your suggestion is a refinement and works as well as Andrew's.

          As for the second part of your reply, we discussed the graph two days ago. Variables are from "genstack" so they do not overlaps and are indeed stacked.

          I really appreciate your help. Thank you.

          Marco

          PS. I don't know why the last two lines of your reply are not visible in your #3, but they do appear when I quote #3.
          Click image for larger version

Name:	Graph_Totale Economia.jpg
Views:	1
Size:	102.0 KB
ID:	1771760

          Comment


          • #6
            It's your graph. I think the result in #5 is hard work to digest!

            Comment


            • #7
              Originally posted by Nick Cox View Post
              It's your graph. I think the result in #5 is hard work to digest!
              I know, but changing the colours is difficult with this code.
              I already have recorded some style changes to apply to all the graph via the graph editor. Final graphs would be someting like the following:
              Click image for larger version

Name:	Graph_Totale Economia.jpg
Views:	1
Size:	103.4 KB
ID:	1771764

              Comment


              • #8
                I don't want to repeat myself, but to me the major problem is not the colours at all, but the bar design.

                I doubt that any reader whatsoever is willing to read the graph year by year and decode the patterns one at a time. I could be very wrong, and just projecting my inclinations.

                Either way, that then leaves the question of whether people can grasp overall patterns in fluctuations of the components. For example, some components are zero or negligible in some years; that becomes clear when you look at individual years, but I find it hard to weigh in thinking about total patterns.

                There are at least three social solutions here.

                1. You're under instruction to produce a graph of a certain kind. Noted, but there may be an issue ahead depending on whether someone else will be evaluating this graph (a reviewer, an examiner, someone yet higher up; I have no idea).

                2. Ask around locally, and see whether people in your field regard this design as clear and better than alternatives.

                3. The attitude is that the graph shows the data in principle and it's up to people to decide for themselves how much time to spend on it in practice. Some large fraction of graphs seem to be produced with this attitude.

                Comment


                • #9
                  On a different level, there is no sense in which my code in #3 is a refinement of Andrew Musau's in #2. It is exactly the same idea, implemented differently: Look up the value label and use it as a title. People might prefer one style to another, which is fine by me (and I suspect Andrew too).

                  Comment


                  • #10
                    Originally posted by Nick Cox View Post
                    People might prefer one style to another, which is fine by me (and I suspect Andrew too).
                    Absolutely!

                    Comment

                    Working...
                    X