Announcement

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

  • plotting mean y values by multiple group in one graph

    Dear all,

    I have a snippet of my panel
    id Year y Cat1 Cat2 Cat3 Cat4
    1 2000 45 1 0 0 0
    1 2001 56 1 0 0 0
    1 2002 23 1 0 0 0
    1 2003 56 1 0 0 0
    1 2004 45 1 0 0 0
    2 2000 45 0 1 0 0
    2 2001 90 0 1 0 0
    2 2002 35 0 1 0 0
    2 2003 79 0 1 0 0
    2 2004 32 0 1 0 0

    Ofcourse my full panel has N=30 and T=5 .

    I want to find the mean values of y by Cat1, up to Cat4, when each Cat takes the value of 1. And then I want to plot in one graph these mean values of y against the X axis that will contain the labels Cat1,..., Cat4.

    Is that possible to do that in Stata?

    If it is not clear, please let me know!

    Many thanks in advance!

  • #2
    This may help. Naturally there are no such values for Cat3 and Cat4 in your data example.

    Code:
    clear 
    input id    Year    y    Cat1    Cat2    Cat3    Cat4
    1    2000    45    1    0    0    0
    1    2001    56    1    0    0    0
    1    2002    23    1    0    0    0
    1    2003    56    1    0    0    0
    1    2004    45    1    0    0    0
    2    2000    45    0    1    0    0
    2    2001    90    0    1    0    0
    2    2002    35    0    1    0    0
    2    2003    79    0    1    0    0
    2    2004    32    0    1    0    0
    end
    
    gen Mean = . 
    forval j = 1/4 { 
        su y if Cat`j' == 1, meanonly 
        replace Mean = r(mean) in `j'
    }
    
    gen Cat = _n in 1/4 
    
    scatter Mean Cat , xla(1/4)

    Comment


    • #3
      Many many thanks Nick! If I want to have a bar chart instead of a scatter, I am not sure how to do that. I used this

      graph bar Mean Cat, xla(1/4)

      but I get the error message that

      xlabels(1/4) not allowed, xaxis1 does not exist. I am so close!

      Is it also possible to have the names Cat1,...,Cat4 on the X axis in a vertical position?

      Comment


      • #4
        Your problem is explained in the help for graph bar. graph bar is not considered to have an x axis. Do please try to read the help.

        Otherwise

        Code:
        twoway bar Mean Cat, xla(1 "Cat1")
        is a start on another way to do it. The help for axis labels explains about getting the text vertical, which is usually a bad idea.

        If you have different labels to apply, it's a good idea to explain more about the real problem. But longer labels go with an idea that

        Code:
        graph hbar Mean, over(Cat)
        would work better but only after

        Code:
        label def Cat  1 "something longer" 2 "needed here"
        label val Cat Cat
        Last edited by Nick Cox; 02 Apr 2024, 07:10.

        Comment


        • #5
          Thank you very much indeed!

          Comment


          • #6
            My bad. Now it worked!
            Last edited by John Andrews; 02 Apr 2024, 07:24.

            Comment

            Working...
            X