Announcement

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

  • How to label discrete variables on histogram

    I'm doing a histogram of continuous variable (quallife), discrete by two categories in a binary variable (gender), this is my code (so it looks like two histograms next to each other):

    histogram quallife, discrete by(gender)

    It produces a nice histogram but I want to relabel the groups so that they don't appear as 1 and 2, but instead as 'male' and 'female'.


    I tried this but STATA says 'option relabel not allowed':

    histogram quallife, discrete by(gender, relabel(1"Male" 2 "Female"))


    I somehow did this correctly at some point but accidentally lost my code and I've been going crazy for days trying to figure out how to do it!

    Any help is really appreciated

    Thank you

  • #2
    Code:
    label def gender 1 Male 2 Female 
    label val gender gender
    and then re-issue your histogram command.

    Comment


    • #3
      Thank you! But I have already done this and it still doesn't work

      Comment


      • #4
        If your labels in gender are defined as Nick suggested in #2, then your following code should return a histogram with gender labels instead 1, 2
        Code:
         histogram quallife, discrete by(gender)
        Roman

        Comment


        • #5
          This is what I did to define the labels:

          label define genderlabel 1 "Male" 2 "Female"
          label values gender genderlabel

          Is that right?

          Then I used the code you sugested Roman:

          histogram quallife, discrete by(gender)

          But they were not labelled, male and female, just labelled as 1 and 2

          Thank you for your help

          Comment


          • #6
            Code:
            . sysuse auto, clear
            (1978 Automobile Data)
            
            . histogram rep78, by(foreign) discrete
            shows that if value labels are defined they are echoed automatically on histogram panels. So, and I agree with Roman Mostazir here, it is hard to see what is going on. I suspect some small confusion, say that you have different versions of the dataset open at once in different Statas, or different versions of the same variable in a dataset. Those guesses may seem far-fetched but the principle is clear: this really should work.

            Comment


            • #7
              Apologise I see my error, going off what Nick said it should be this:

              label define gender 1 "Male" 2 "Female"
              label value gender gender

              Is that right? What does the second command do? I have also labelled the variable itself

              label variable gender "Gender in 2 Categories"

              (it was a newly recoded variable from another variable)

              Comment


              • #8
                And Nick to your post - I agree, it seems really weird as I was using the first set of commands before, and I'm sure I then used that code to correctly label the histogram, but suddenly it just wouldn't work

                Comment


                • #9
                  Now that I changed it to the commands above, it works. And I don't have any different version of the dataset open....weird

                  Thank you again for your help!

                  Comment


                  • #10
                    Still hard to know for certain what you were doing wrong, but confusing label val and label var is my new top guess.

                    Comment

                    Working...
                    X