Announcement

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

  • Graph bar chart with many variables on x axis

    I am trying to create a graph that looks like this:

    Click image for larger version

Name:	Screen Shot 2017-06-12 at 15.16.51.png
Views:	1
Size:	46.3 KB
ID:	1397329


    In the data set in STATA I have one Variable with the different Maize varieties (DH04, DH02, Duma43, KDV1, Tego WH 1011). I have one variable for each Characteristic (on the x-axis).
    You can see the data structure here:

    Click image for larger version

Name:	Screen Shot 2017-06-12 at 15.19.53.png
Views:	1
Size:	156.9 KB
ID:	1397330


    How can I make the bar chart that is pictured above in STATA?

    Thanks a lot in advance!

  • #2
    Please study http://www.statalist.org/forums/help#stata for how to present examples helpfully here: no screenshots please!

    While you're there stop by http://www.statalist.org/forums/help#spelling

    Comment


    • #3
      My apologies.

      I try to explain the data and the chart I would like to create.

      I have data on 5 different maize varieties (A, B, C, D, E). 16 different characteristics were observed for each of these varieties. Data were collected over 4 seasons and in different sites, so there are several observations per participant.
      I would like to create a bar chart that has the 16 characteristics on the x-axis, and for each characteristic there is a bar for each variety. So characteristic 1 shows 5 bars clustered together, 1 for each variety.

      This is what the data looks like and what dataex told me to copy (I could not include all 16 characteristics variables).

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input double(PARTICIPANT_ID Evaluation_Round VARIETY GERMINATION_CROP_STAND HEIGHT STALK_THICKNESS NUMBER_OF_COBS COB_SIZE BARRENESS_LEVEL YIELD GOOD_TIP_COVER BIOMASS LODGING_RESISTANCE EARLY_MATURING STALK_BORER_RESISTANCE)
      102 2 2 4 3 2 2 2 2 3 4 3 2 . 5
      102 2 2 3 2 2 3 3 3 2 3 3 3 . 3
      102 2 4 2 2 1 3 2 1 1 3 2 1 . 3
      102 2 1 3 2 3 2 2 2 2 3 3 2 . 2
      102 2 3 3 2 2 3 3 2 2 4 3 3 . 4
      102 2 4 3 3 3 3 3 4 4 4 3 3 . 4
      102 2 3 3 3 3 2 2 2 1 2 3 3 . 1
      102 2 5 3 2 2 2 3 2 2 4 4 2 . 3
      102 2 5 4 3 3 2 2 2 3 4 4 4 . 3
      102 2 1 4 3 4 3 2 2 2 4 4 4 . 4
      201 2 4 4 3 3 4 4 3 4 4 3 2 . 3
      201 1 4 3 4 4 3 4 3 4 4 4 4 4 5
      201 1 4 4 2 1 1 2 2 3 3 3 3 2 2
      201 1 2 4 4 4 5 5 4 5 5 4 5 5 4
      201 1 1 5 5 4 5 4 5 5 4 5 5 5 5
      201 2 5 5 3 4 3 3 3 3 4 5 4 . 3
      201 1 3 5 5 5 3 4 3 5 5 4 4 4 4
      201 1 1 5 5 4 4 4 4 2 5 5 5 4 5
      201 1 2 5 4 3 3 3 2 4 5 3 4 3 4
      201 2 1 5 3 4 3 4 4 4 5 4 4 . 4
      201 1 3 5 5 5 4 5 4 5 5 4 4 4 3
      201 1 2 5 4 3 4 3 4 4 3 3 4 3 4
      201 1 4 4 5 5 4 3 2 5 4 4 4 3 3
      201 2 2 5 4 2 3 4 3 4 4 4 2 . 4
      201 2 4 3 5 3 4 4 3 4 5 3 2 . 4
      201 2 2 5 3 3 2 3 3 4 4 3 2 . 3
      201 1 5 3 4 5 3 4 4 4 4 2 3 3 3
      201 2 2 5 5 5 4 4 3 4 5 4 4 . 5
      201 1 1 3 4 4 3 4 5 4 3 2 3 3 3
      201 1 2 5 4 3 4 2 4 4 4 4 4 4 4
      201 1 3 5 4 3 2 3 2 2 4 4 5 5 2
      201 2 3 5 4 4 3 4 3 4 4 4 3 . 4
      201 1 2 5 4 3 4 3 3 2 4 3 3 3 4
      201 1 4 3 4 5 5 4 5 4 4 4 4 5 3
      201 1 1 5 3 3 2 2 1 1 5 5 5 2 5
      201 2 5 5 3 3 3 3 2 3 2 5 5 . 4
      201 1 3 4 4 3 4 3 3 4 3 4 3 4 4
      201 2 3 5 4 3 4 3 3 4 5 4 4 . 5
      201 1 5 5 4 5 4 4 5 5 4 5 5 3 4
      201 2 1 5 4 4 4 5 4 3 4 5 4 . 4
      201 1 5 5 3 5 2 2 3 4 4 4 4 2 4
      201 1 5 5 4 2 3 4 3 2 4 4 4 3 5
      202 1 5 5 2 4 3 2 4 2 3 4 5 4 4
      202 1 2 4 2 3 3 1 2 1 3 3 3 2 3
      202 1 4 4 4 3 3 4 3 4 3 4 3 4 4
      202 1 3 5 2 4 4 3 3 4 4 4 4 2 3
      202 1 1 5 4 5 4 4 3 4 4 4 5 2 4
      202 1 1 3 2 2 2 2 3 1 4 3 4 2 3
      202 1 4 4 1 2 1 1 2 1 2 3 2 1 3
      202 1 3 5 4 4 3 4 4 3 4 3 5 3 4
      202 1 2 3 2 2 1 2 2 1 2 3 2 1 3
      202 1 2 4 3 4 3 4 3 3 4 5 4 4 3
      202 1 2 4 2 2 3 3 3 3 4 4 4 2 4
      202 1 3 3 2 3 2 2 1 1 4 3 2 2 2
      202 1 5 4 5 3 4 3 2 3 5 5 4 4 4
      202 1 3 4 4 3 3 2 3 3 3 4 4 2 3
      202 1 1 4 2 2 3 2 2 2 3 3 2 1 3
      202 1 4 4 4 5 4 3 3 4 3 4 4 3 4
      202 1 5 4 3 3 4 2 3 2 4 4 3 2 2
      202 1 4 2 2 4 3 4 3 3 4 3 3 4 4
      202 1 2 5 4 3 4 2 3 3 4 3 3 2 3
      202 1 5 3 4 3 2 4 3 4 3 3 3 4 4
      202 1 1 5 4 4 4 3 2 4 5 3 3 4 3
      203 1 4 3 4 3 4 3 3 2 3 4 3 4 4
      203 2 2 5 5 4 5 4 5 4 5 4 4 . 4
      203 1 5 5 4 4 4 3 3 4 4 4 4 4 3
      203 2 1 5 4 3 4 3 4 5 3 4 3 . 5
      203 1 1 5 4 4 3 3 4 2 2 5 4 4 4
      203 1 5 4 3 4 4 4 3 4 4 3 4 3 4
      203 1 4 1 2 2 1 1 1 1 1 3 2 1 2
      203 1 1 5 4 4 2 2 2 3 3 4 4 1 3
      203 2 2 5 4 4 4 4 3 4 3 3 4 . 3
      203 1 1 5 4 5 3 3 3 2 3 5 4 3 3
      203 2 3 5 5 5 4 5 5 5 5 5 5 . 4
      203 2 2 5 5 4 3 4 4 4 5 4 3 . 4
      203 2 1 5 3 3 4 2 2 3 2 3 3 . 2
      203 2 3 5 5 5 5 5 5 5 5 5 5 . 4
      203 2 4 5 3 2 2 1 2 2 2 1 4 . 3
      203 2 4 5 5 4 4 5 4 4 5 4 4 . 5
      203 1 3 5 3 2 1 1 1 3 4 3 2 5 5
      203 2 3 2 2 3 2 3 2 2 3 2 3 . 2
      203 1 4 4 4 4 4 3 3 3 3 4 4 4 4
      203 2 1 5 4 4 4 4 4 3 4 4 4 . 5
      203 1 2 5 4 4 2 2 2 2 1 2 3 1 5
      203 1 2 4 3 4 3 4 4 3 4 4 2 3 3
      203 1 5 5 4 4 3 2 2 1 2 3 4 2 3
      203 2 5 5 5 4 4 3 3 3 4 5 5 . 4
      203 1 1 4 4 3 2 1 2 2 3 3 2 4 3
      203 2 5 5 5 5 4 5 4 5 4 4 5 . 4
      203 1 2 5 4 4 4 4 4 4 4 5 4 4 4
      203 1 5 3 4 4 2 1 1 1 2 5 4 2 2
      203 1 2 5 4 4 3 3 4 3 4 5 4 3 4
      203 2 5 2 3 2 2 2 3 2 2 3 2 . 2
      203 2 2 5 4 4 4 3 4 4 5 4 4 . 4
      203 2 1 5 5 5 3 3 3 3 3 5 5 . 4
      203 1 2 2 2 2 2 1 2 1 2 2 4 1 3
      203 2 2 3 2 2 2 2 2 3 3 2 2 . 2
      203 1 3 5 5 4 4 4 4 4 4 4 4 3 5
      203 1 4 5 5 4 4 3 4 3 3 5 4 4 3
      203 2 5 5 5 4 4 5 5 4 4 4 4 . 5
      end
      label values Evaluation_Round Evaluation_Round
      label def Evaluation_Round 1 "Mid season", modify
      label def Evaluation_Round 2 "End season", modify
      label values VARIETY VARIETY
      label def VARIETY 1 "DH04", modify
      label def VARIETY 2 "DH02", modify
      label def VARIETY 3 "Duma43", modify
      label def VARIETY 4 "KDV1", modify
      label def VARIETY 5 "Tego WH 1011", modify
      label values GERMINATION_CROP_STAND GERMINATION_CROP_STAND
      label def GERMINATION_CROP_STAND 1 "E", modify
      label def GERMINATION_CROP_STAND 2 "D", modify
      label def GERMINATION_CROP_STAND 3 "C", modify
      label def GERMINATION_CROP_STAND 4 "B", modify
      label def GERMINATION_CROP_STAND 5 "A", modify
      label values HEIGHT HEIGHT
      label def HEIGHT 1 "E", modify
      label def HEIGHT 2 "D", modify
      label def HEIGHT 3 "C", modify
      label def HEIGHT 4 "B", modify
      label def HEIGHT 5 "A", modify
      label values STALK_THICKNESS STALK_THICKNESS
      label def STALK_THICKNESS 1 "E", modify
      label def STALK_THICKNESS 2 "D", modify
      label def STALK_THICKNESS 3 "C", modify
      label def STALK_THICKNESS 4 "B", modify
      label def STALK_THICKNESS 5 "A", modify
      label values NUMBER_OF_COBS NUMBER_OF_COBS
      label def NUMBER_OF_COBS 1 "E", modify
      label def NUMBER_OF_COBS 2 "D", modify
      label def NUMBER_OF_COBS 3 "C", modify
      label def NUMBER_OF_COBS 4 "B", modify
      label def NUMBER_OF_COBS 5 "A", modify
      label values COB_SIZE COB_SIZE
      label def COB_SIZE 1 "E", modify
      label def COB_SIZE 2 "D", modify
      label def COB_SIZE 3 "C", modify
      label def COB_SIZE 4 "B", modify
      label def COB_SIZE 5 "A", modify
      label values BARRENESS_LEVEL BARRENESS_LEVEL
      label def BARRENESS_LEVEL 1 "E", modify
      label def BARRENESS_LEVEL 2 "D", modify
      label def BARRENESS_LEVEL 3 "C", modify
      label def BARRENESS_LEVEL 4 "B", modify
      label def BARRENESS_LEVEL 5 "A", modify
      label values YIELD YIELD
      label def YIELD 1 "E", modify
      label def YIELD 2 "D", modify
      label def YIELD 3 "C", modify
      label def YIELD 4 "B", modify
      label def YIELD 5 "A", modify
      label values GOOD_TIP_COVER GOOD_TIP_COVER
      label def GOOD_TIP_COVER 1 "E", modify
      label def GOOD_TIP_COVER 2 "D", modify
      label def GOOD_TIP_COVER 3 "C", modify
      label def GOOD_TIP_COVER 4 "B", modify
      label def GOOD_TIP_COVER 5 "A", modify
      label values BIOMASS BIOMASS
      label def BIOMASS 1 "E", modify
      label def BIOMASS 2 "D", modify
      label def BIOMASS 3 "C", modify
      label def BIOMASS 4 "B", modify
      label def BIOMASS 5 "A", modify
      label values LODGING_RESISTANCE LODGING_RESISTANCE
      label def LODGING_RESISTANCE 1 "E", modify
      label def LODGING_RESISTANCE 2 "D", modify
      label def LODGING_RESISTANCE 3 "C", modify
      label def LODGING_RESISTANCE 4 "B", modify
      label def LODGING_RESISTANCE 5 "A", modify
      label values STALK_BORER_RESISTANCE STALK_BORER_RESISTANCE
      label def STALK_BORER_RESISTANCE 1 "E", modify
      label def STALK_BORER_RESISTANCE 2 "D", modify
      label def STALK_BORER_RESISTANCE 3 "C", modify
      label def STALK_BORER_RESISTANCE 4 "B", modify
      label def STALK_BORER_RESISTANCE 5 "A", modify

      I hope this makes sense.
      Kind regards

      Comment


      • #4
        Thanks for the example. I'd advise strongly against a bar chart here. It's really hard to make it readable and useful. Continuing from your code, this may help:


        Code:
        collapse GERMINATION_CROP_STAND-STALK_BORER_RESISTANCE, by(VARIETY)
        rename (GERMINATION_CROP_STAND-STALK_BORER_RESISTANCE) (mean=)
        reshape long mean , i(VARIETY) j(measure) string
        replace measure = lower(subinstr(measure, "_", " ", .))
        replace measure = subinstr(measure, "barreness", "barrenness", .)
        
        graph dot (asis) mean , over(V) over(measure) asyvars exclude0 ///
        legend(pos(3) col(1)) marker(1, ms(Oh)) marker(2, ms(X))       ///
          marker(3, ms(+)) marker(4, ms(Dh)) marker(5, ms(Sh)) linetype(line) ///
          lines(lc(gs12) lw(thin))
        I note that you have 16 characteristics not 12, so the need for a readable graph is even greater.

        Alphabetical order of measures is dopey here, and can undoubtedly be improved on.

        (The order of varieties is what you chose.)
        Click image for larger version

Name:	maize.png
Views:	1
Size:	36.5 KB
ID:	1397356

        Last edited by Nick Cox; 12 Jun 2017, 09:22.

        Comment


        • #5
          Thanks a lot, Nick!! This is very helpful.

          Comment


          • #6
            I didn't spell it out, but here as very often elsewhere it seems to be the differences (between varieties) that are central. What their values are relative to zero is not central, but that's the tacit focus of a bar chart.

            Comment


            • #7
              I have another question: with the same dataset I want to create the graph for each of the four seasons and combine them in one picture. The labels of the vertical axes are quite large though and take up too much space. I'd rather have them only on the left side of the first and the third graph and remove them from the right graphs, so the data has more space. How do I do that? I have tried for some time and couldn't find any solutions.

              My code is:


              Code:
              graph dot (asis) mean if YEAR ==1 & Evaluation_Round ==1, over(VARIETY) over(measure) asyvars exclude0 ylab(2(.5)5) ///
              legend(pos(3) col(1)) marker(1, ms(Oh)) marker(2, ms(X))       ///
              marker(3, ms(+)) marker(4, ms(Dh)) marker(5, ms(Sh)) linetype(line) ///
              lines(lc(gs12) lw(thin))   name(Y1S1, replace) nodraw
              
              graph dot (asis) mean if YEAR ==1 & Evaluation_Round ==2, over(VARIETY) over(measure) asyvars  exclude0 ylab(2(.5)5) ///
              legend(pos(3) col(1)) marker(1, ms(Oh)) marker(2, ms(X))       ///
              marker(3, ms(+)) marker(4, ms(Dh)) marker(5, ms(Sh)) linetype(line) ///
              lines(lc(gs12) lw(thin)) name(Y1S2, replace) nodraw
              
              graph dot (asis) mean if YEAR ==2 & Evaluation_Round ==1, over(VARIETY) over(measure) asyvars exclude0 ylab(2(.5)5) ///
              legend(pos(3) col(1)) marker(1, ms(Oh)) marker(2, ms(X))       ///
              marker(3, ms(+)) marker(4, ms(Dh)) marker(5, ms(Sh)) linetype(line) ///
              lines(lc(gs12) lw(thin)) name(Y2S1, replace) nodraw
              
              graph dot (asis) mean if YEAR ==2 & Evaluation_Round ==2, over(VARIETY) over(measure) asyvars  exclude0 ylab(2(.5)5) ///
              legend(rows(1) size(small) pos(3) col(1)) marker(1, ms(Oh)) marker(2, ms(X))       ///
              marker(3, ms(+)) marker(4, ms(Dh)) marker(5, ms(Sh)) linetype(line) ///
              lines(lc(gs12) lw(thin)) name(Y2S2, replace) nodraw
              grc1leg Y1S1 Y1S2 Y2S1 Y2S2 , leg(Y2S2)

              Comment


              • #8
                Dear all,

                I am trying to create a stack bar chart given the count of outcome /not outcome, but stratified by score and category (Stata 18)

                Something like this:
                Click image for larger version

Name:	20240422 Example.png
Views:	1
Size:	1.29 MB
ID:	1750793















                The furthest I had reached was this (but it is not measured by count, it is by percentage).

                Code:
                 graph bar, over(outcome) over(score) asyvars percentages stack by(category)
                Click image for larger version

Name:	20240422 Graph.png
Views:	1
Size:	157.5 KB
ID:	1750794














                Here the data:


                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input float(outcome score category)
                0 2 3
                0 1 3
                0 0 3
                0 1 2
                0 0 3
                1 0 3
                0 3 1
                0 3 2
                0 2 3
                0 2 3
                0 1 3
                0 2 3
                0 1 4
                0 1 4
                0 1 2
                0 0 2
                0 0 1
                0 0 3
                0 1 2
                0 0 2
                0 1 2
                1 0 2
                0 1 4
                1 1 4
                0 0 4
                0 1 2
                0 2 3
                0 2 3
                0 3 3
                0 4 3
                0 2 3
                0 2 2
                0 0 3
                0 1 4
                0 0 1
                0 1 3
                0 0 3
                0 4 4
                0 2 3
                0 2 3
                0 2 2
                0 1 3
                0 2 4
                0 4 1
                0 1 2
                1 0 3
                0 4 2
                0 5 4
                0 1 3
                0 0 1
                0 2 4
                0 1 2
                0 1 1
                0 0 3
                0 1 3
                0 0 3
                1 3 3
                1 4 3
                0 4 3
                0 2 2
                0 0 3
                0 0 3
                0 2 4
                1 2 3
                0 1 2
                0 0 1
                0 0 4
                0 3 2
                0 2 3
                0 1 2
                0 1 3
                0 0 3
                1 1 2
                0 1 3
                0 1 3
                1 0 4
                0 1 3
                0 5 3
                0 0 3
                0 4 3
                0 0 3
                0 0 3
                0 4 3
                1 1 1
                0 1 3
                0 1 2
                0 2 1
                0 0 1
                0 1 3
                0 3 4
                0 6 4
                0 3 2
                0 1 1
                0 3 1
                1 1 2
                0 2 2
                0 3 1
                0 2 4
                0 0 3
                0 1 3
                end



                Thanks in advance for your help!
                Last edited by Carolina Hincapie; 22 Apr 2024, 09:38.

                Comment


                • #9
                  Try playing with

                  Code:
                  ssc install catplot 
                  
                  catplot outcome category score, stack asyvars recast(bar) legend(order(2 1))

                  Comment


                  • #10
                    Nick Cox Thanks a lot!

                    Comment

                    Working...
                    X