Announcement

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

  • Graphing problem

    I collapsed my data for it to include count, mean and a dummy variable for the category they belong to.

    My aim was to create a bar graph similar to what:
    Code:
      graph bar mean, over(dummy)
    would return but with number of observations on top. Hence I used a two way graph with the code:
    Code:
     twoway (bar mean dummy) (scatter mean dummy, m(i) mlabel(count) mlabposition(12))
    However the result from the two has numbers on the X-axis and not the categorical labels. Any way I can add labels to it?

  • #2
    This happened because the data was collapsed, but you don't need to collapse the data to get a - graph bar (mean) numvar, over(catvar) - .
    Best regards,

    Marcos

    Comment


    • #3
      Originally posted by Marcos Almeida View Post
      This happened because the data was collapsed, but you don't need to collapse the data to get a - graph bar (mean) numvar, over(catvar) - .
      Is there a way I can get the count of observations on top of the bar graphs using this code?

      Comment


      • #4
        #3 "this code" I guess means that in #1. I think you need to clarify what your variables are.

        Isn't mlabel(count) doing precisely what you ask?

        Comment


        • #5
          Originally posted by Nick Cox View Post
          #3 "this code" I guess means that in #1. I think you need to clarify what your variables are.

          Isn't mlabel(count) doing precisely what you ask?


          mlabel count is getting the numbers on the top of the bar graph - yes. But because I am using the twoway option I have to make specific dummies for the categorical values. Which are aptly shown in the x-axis. I want to show the labels instead of the numbers and I am confused as to how

          Comment


          • #6
            Sorry, but I don't understand. I think we need a data example. Your syntax in #1 names one x axis variable (indeed that is as many as are allowed) and so the references to specific dummies makes no sense to me without an example.

            Please read and act on https://www.statalist.org/forums/help#stata

            Comment


            • #7
              Originally posted by Nick Cox View Post
              Sorry, but I don't understand. I think we need a data example. Your syntax in #1 names one x axis variable (indeed that is as many as are allowed) and so the references to specific dummies makes no sense to me without an example.

              Please read and act on https://www.statalist.org/forums/help#stata



              I think perhaps a picture would help. Here is the graph I get using the code in #1. Moving on I would like to change the x-axis to represent the labels of country instead of the numeric value assigned to it. (ie each country has a specific numeric value)

              Click image for larger version

Name:	Graph.png
Views:	1
Size:	13.6 KB
ID:	1550194

              [
              Last edited by Rabi Uddin; 30 Apr 2020, 06:02.

              Comment


              • #8
                Is there a way I can get the count of observations on top of the bar graphs using this code?
                Please see the examples below and check whether this is what you want:

                Code:
                sysuse auto
                graph bar (mean) mpg, over(foreign) blabel(bar)
                graph bar (count) mpg, over(foreign) blabel(bar)
                Best regards,

                Marcos

                Comment


                • #9
                  Orherwise put: #3 was asking a question already answered. Your real question is in #7. You haven't given a data example despite being asked to do that in #5. (Again, please read and act on the link given.)

                  If the x-axis variable used for the graph in #7 was created with egen ... group(...), label then all you need to do is have an extra option on your twoway call

                  Code:
                  xla(1/10, valuelabel)
                  But if you didn't do that then you need to reissue your egen command but this time using the label option. See the help on egen.

                  That said, your graph is problematic:

                  1 Touching bars don't really match the statistical logic so a barw(0.7) or something similar is needed to underline that bars are for distinct categories.

                  2. You really won't have space to show country names legibly or at least comfortably with vertical bars so you should probably switch to horizontal bars.

                  3. I guess that originally you had country names as a string variable. That being so the result of egen, group() is to sort on alphabetical name order which is rarely the best choice.

                  4. Your bars start at about 0.75 which is evidently the mean for country 8, except that no count appears. Most users of bar charts would expect a base at zero. In this case, and most such, as the means are between 0.75 and 1 a dot chart might work much better.

                  Comment


                  • #10
                    Originally posted by Nick Cox View Post
                    Orherwise put: #3 was asking a question already answered. Your real question is in #7. You haven't given a data example despite being asked to do that in #5. (Again, please read and act on the link given.)

                    If the x-axis variable used for the graph in #7 was created with egen ... group(...), label then all you need to do is have an extra option on your twoway call

                    Code:
                    xla(1/10, valuelabel)
                    But if you didn't do that then you need to reissue your egen command but this time using the label option. See the help on egen.

                    That said, your graph is problematic:

                    1 Touching bars don't really match the statistical logic so a barw(0.7) or something similar is needed to underline that bars are for distinct categories.

                    2. You really won't have space to show country names legibly or at least comfortably with vertical bars so you should probably switch to horizontal bars.

                    3. I guess that originally you had country names as a string variable. That being so the result of egen, group() is to sort on alphabetical name order which is rarely the best choice.

                    4. Your bars start at about 0.75 which is evidently the mean for country 8, except that no count appears. Most users of bar charts would expect a base at zero. In this case, and most such, as the means are between 0.75 and 1 a dot chart might work much better.


                    Hi Nick, I implemented the:
                    Code:
                    xla(1/10, valuelabel)
                    And it worked perfectly well. Thank you for your help! (here and all the other posts of yours I have been coming across!)

                    Comment

                    Working...
                    X