Announcement

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

  • clustered bar graph

    Hi All,
    I need to create a graph similar to this one. So I have two groups (C and G) and 3 or 4 different variables.
    Do you have any suggestion?
    Thank you


  • #2
    Your picture is not visible, so we can't see what you want to do.

    Comment


    • #3
      I am sorry. I am attaching to you.
      I need to create a bar graph with three or four different variables (e.g. divatten2 dif2 curvecoherence2 CNDA__Mean_RT_012) and 2 different groups (MVCs and no MVCs or C and G in the example).
      However, when I use this command, the order is not good: For example, I would like to have the two observations of var1 (MVC and no MVCs) next to each other...
      graph bar divatten2 dif2 curvecoherence2 CNDA__Mean_RT_012 if bargraph3=="Last follow-up", over(accident, relabel(2 "MVCs" 1"No MVCs" ))

      Thank you
      Attached Files

      Comment


      • #4
        I don't want to work with your complicated variable names here. But here's an example of how to do this using the built-in auto file as an example.

        Code:
         clear*
        sysuse auto
          // THIS GRAPH IS WHAT YOU DON'T WANT.
        graph bar mpg headroom trunk, over(foreign) name(unwanted, replace)
          // YOU NEED TO RESHAPE YOUR DATA TO SEPARATE THE CLUSTERS
        preserve
        foreach v of varlist mpg headroom trunk {
             separate `v', by(foreign)
        }
        keep *0 *1
        rename *# *_#
        gen seq = _n
        reshape long @_0 @_1, i(seq) j(vble) string
        rename _0 domestic
        rename _1 foreign
        // THIS GRAPH IS WHAT YOU WANT
        graph bar domestic foreign, over(vble) name(wanted, replace)
        restore

        Comment


        • #5
          Dear Schechter, Thank you so much for your help. I would be able to do that without your help. The graph looks pretty nice now. I am just trying to change the names of variables in the Y axis. Thank you!

          Comment


          • #6
            See also statplot from SSC by Eric Booth and myself, which is focused on this need.

            Code:
             
            sysuse auto
            statplot mpg headroom trunk , over(foreign)

            Comment


            • #7
              Hello!

              I am also trying to create a graph like the above.
              I have created percentage summary variables using :

              by change_anaemia, sort: egen pc_stunt= mean(100*stunted)


              Code:
               clear*
              sysuse auto
              // THIS GRAPH IS WHAT YOU DON'T WANT.
              graph bar mpg headroom trunk, over(foreign) name(unwanted, replace)
              // YOU NEED TO RESHAPE YOUR DATA TO SEPARATE THE CLUSTERS
              preserve
              foreach v of varlist mpg headroom trunk {
              separate `v', by(foreign)
              }
              keep *0 *1
              rename *# *_#
              gen seq = _n
              reshape long @_0 @_1, i(seq) j(vble) string
              rename _0 domestic
              rename _1 foreign
              // THIS GRAPH IS WHAT YOU WANT
              graph bar domestic foreign, over(vble) name(wanted, replace)
              restore
              [/QUOTE]

              When I reshape my data however I get the below error

              keep *0 *1

              . rename *# *_#

              . gen seq = _n

              . reshape long @_0 @_1, i(seq) j(vble) string
              (note: j = Bi Lang c c3a c3b c3c c3d c3e c3f c3g c3h c3i c3j c3k c3l c3m c3n d e f f122a field g g1221
              > 2a g4b g4d h i merge oui pc_acute pc_low pc_malaria pc_stunt precision si)

              variable g4b_1 type mismatch with other @_1 variables
              r(198);



              Would somebody be able to tell me what I am doing wrong?

              Best wishes,

              Isobel

              Comment


              • #8
                Isobel:

                Short answer is No, or Not easily. You have not given us a data example we can use to see your problem. Also, this seems to be about a problem with reshape long long before you get to trying a graph command.

                But type mismatch means something is numeric and something else supposedly comparable is string.

                Comment


                • #9
                  Thank you Nick for your help!

                  https://www.statalist.org/forums/for...ered-bar-chart

                  Comment


                  • #10
                    Clyde Schechter:

                    I was reading your answer above:

                    // THIS GRAPH IS WHAT YOU DON'T WANT. graph bar mpg headroom trunk, over(foreign) name(unwanted, replace) // YOU NEED TO RESHAPE YOUR DATA TO SEPARATE THE CLUSTERS preserve foreach v of varlist mpg headroom trunk { separate `v', by(foreign) } keep *0 *1 rename *# *_# gen seq = _n reshape long @_0 @_1, i(seq) j(vble) string rename _0 domestic rename _1 foreign // THIS GRAPH IS WHAT YOU WANT graph bar domestic foreign, over(vble) name(wanted, replace) restore

                    I have a similar problem, but am far less experienced. I am analysing a survey with related questions (Variables: q1-q5). The responses are binary yes/no, and can be broken up by responders' job (variable occ - there are 5 different occupations). I also want to make a bar chart with clusters of each question broken up by occupation:

                    Click image for larger version

Name:	statalist_q1.png
Views:	1
Size:	94.2 KB
ID:	1448920



                    I am trying to reshape this data as you suggested, but I am a little confused:

                    Code:
                    foreach V of varlist q1-q5 {
                        separate `V', by(occ) 
                        }
                    It's the next bit that I don't quite understand. Could you please explain it?

                    Thank you!

                    Comment

                    Working...
                    X