Announcement

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

  • Graph bar - Set different colors within an -over()- option

    Hi there,

    Suppose we're working with these data,

    Code:
    use http://www.stata-press.com/data/r13/gsem_cfa, clear
    
    reshape long att, i(id) j(atribute)
    So we can get this graph,

    Code:
    graph bar , over(att, lab(labsize(vsmall)))
    Click image for larger version

Name:	example.png
Views:	1
Size:	67.8 KB
ID:	1496175



    Is there any way to change the color of each bar? I'd like to make them green, gren*0.5, yellow, red*0.5, and red, in order to get something like a gradient for this Likert scale.

    Thank you in advance,

  • #2
    Use the option asyvars -- after which options like bar(1, bcolor(magenta)) may be used to specify what you want.

    All documented at help graph bar

    Comment


    • #3
      Note that red and green together are strongly advised against as being unfriendly to many who can't distinguish them easily. My own view here is that the names are evocative and the order informative, so why use colour too?

      Comment


      • #4
        Thank you Nick!

        I wanted to post a minimal example, but I realized it doesn't focus on the key issue I'm trying to solve. I need to set different colors for each point of the Likert scale in a graph like the following,

        Note: I'm benefiting from -splitvallabels- by Nick Winter and Ben Jann, ssc install splitvallabels.

        Code:
        use http://www.stata-press.com/data/r13/gsem_cfa, clear
        
        reshape long att, i(id) j(attribute)
        
        splitvallabels att, length(10)
        graph bar , by(attribute, note(" "))                 ///
                    over(att, lab(labsize(vsmall)) relabel(`r(relabel)'))
        Click image for larger version

Name:	bar - likert1.png
Views:	4
Size:	105.1 KB
ID:	1496284

        To make the graph compatible with previous work, I should label each bar of the Likert scale in each graph. Then, I cannot produce something like this,

        Code:
        graph bar , by(attribute, note(" "))                 ///
                    over(att, lab(labsize(vsmall))) asyvars  ///
                    leg(stack pos(6) r(1) si(vsmall))        ///
                    bar(1, fcolor(green) lw(none))           ///
                    bar(2, fcolor(green*0.5) lw(none))       ///
                    bar(3, fcolor(yellow) lw(none))          ///
                    bar(4, fcolor(red*0.5) lw(none))         ///
                    bar(5, fcolor(red) lw(none))
        Click image for larger version

Name:	bar - likert2.png
Views:	2
Size:	80.8 KB
ID:	1496282

        Reason for colors from green to red is also related to compatibility with previous work. Otherwise, I would use one color or the scheme -plotplainblind- written by Daniel Bischof to be friendly with colorblind people.

        Thank you.
        Attached Files

        Comment


        • #5
          Sorry, but I can't follow if you have a question in there or are just summarizing (which would be more than fine).

          Comment


          • #6
            Thanks Nick.

            I wonder if I can use green to color every bar that represents "strongly agree", use green*0.5 to color every bar that represents "agree", and so forth, in the following graph:

            Click image for larger version

Name:	bar - likert1.png
Views:	4
Size:	105.1 KB
ID:	1496325
            Attached Files

            Comment


            • #7
              Dear Emanual,

              I suppose this is what you are looking for:
              Code:
              use http://www.stata-press.com/data/r13/gsem_cfa, clear
              reshape long att, i(id) j(attribute)
              splitvallabels att, length(10)
              graph bar , by(attribute, note(" "))                 ///
              over(att, lab(labsize(vsmall))) asyvars  ///
              leg(stack pos(6) att(1) si(vsmall))        ///
              bar(1, fcolor(green) lw(none))           ///
              bar(2, fcolor(green*.8) lw(none))       ///
              bar(3, fcolor(green*.6) lw(none))          ///
              bar(4, fcolor(green*.4) lw(none))         ///
              bar(5, fcolor(green*.2) lw(none))
              gr export bar_greens.png
              which will produce this figure:
              Click image for larger version

Name:	bar_greens.png
Views:	1
Size:	76.8 KB
ID:	1496333

              Last edited by ericmelse; 02 May 2019, 13:31.
              http://publicationslist.org/eric.melse

              Comment


              • #8
                statplot (SSC) might be helpful too.

                Comment


                • #9
                  Thanks Eric,

                  I want this graph:

                  Click image for larger version

Name:	bar - likert1.png
Views:	1
Size:	143.0 KB
ID:	1496337

                  Comment


                  • #10
                    Originally posted by Nick Cox View Post
                    statplot (SSC) might be helpful too.
                    Thank you Nick!

                    Comment


                    • #11
                      Dear Emanual,
                      Possibly you are interested in visualizing (your) Likert reponses as stacked percentages. The benefit is that you can inspect the responses of your items. I suppose that is of interest to single out those items that somehow accumulated (un-)expected responses. It does take some lines of code to get the data in the format required for this visualisation. Note also that the code is example specific so you have to edit it to meet your own needs.
                      Code:
                      * Example to create stacked Likert responses as percentages
                      use http://www.stata-press.com/data/r13/gsem_cfa, clear
                      
                      keep att*
                      save Attitudes , replace
                      
                      * CODE TO CREATE THE STACKED BARS OF ITEM RESPONSES (as %)
                      forvalue i = 1(1)5 {
                      * Load the data (repeatedly)
                      use Attitudes , clear
                      contract att`i' if !missing(att`i')
                      gen id=_n
                      egen total = total(_freq)
                      gen pc = 100 * _freq / total
                      * initialise to sum of negative categories and half any neutral category
                      egen start = total(pc * (id == 1) + pc * (id == 2) + 0.5 * pc * (id == 3))
                      * negate start
                      replace start = -start
                      * bar starts and ends
                      gen end = start + sum(pc)
                      replace start = start + sum(pc[_n-1]) if _n > 1
                      replace id=`i'    // id must be increased to append
                      * attename individual items for the Likert attesponse code
                      rename att`i' likert
                      save "Attitudes_att`i'.dta" , replace    // Individual files to append
                      }
                      
                      * APPEND ITEM RESPONSE FILES
                      use "Attitudes_att1" , clear
                      forvalue i = 2(1)5 {
                      append using "Attitudes_att`i'.dta"
                      }
                      * Define labels
                      forvalue i = 1(1)5 {
                      label define lbl_R `i' "R`i'" , add
                      label value id lbl_R  
                      }
                      
                      compress
                      save "Attitudes_att_Stack.dta" , replace
                      use "Attitudes_att_Stack.dta" , clear
                      
                      * Create the stacked bar graph of Likert responses as % (assuming 1 & 2 are negation codes)
                      twoway rbar start end id if likert == 1, barw(0.8) bfcolor(maroon*0.6) blcolor(maroon*.6) ///
                      || rbar start end id if likert == 2, barw(0.8) bfcolor(maroon*0.4) blcolor(maroon*.4)     ///
                      || rbar start end id if likert == 3, barw(0.8) bfcolor(gs11) blcolor(gs11)          ///
                      || rbar start end id if likert == 4, barw(0.8) bfcolor(blue*0.4) blcolor(blue*.4)   ///
                      || rbar start end id if likert == 5, barw(0.8) bfcolor(blue*0.6) blcolor(blue*.6)   ///
                      graphregion(margin(b-2 t-1)) yline(0, lc(gs12) lw(thin)) yscale(noextend)     ///
                      legend(order(5 "Strongly Agree" 4 "Agree" 3 "Neither" 2 "Disagree" 1 "Strongly disagree") ///
                      col(5) pos(6) region(lc(white)) bmargin(b-2) symxsize(*.5) symysize(*.6) size(*.8))    ///
                      xlabel(1(1)5, valuelabel labs(*.9)) yla(-60(10)60, labs(*.9) angle(0))  ///
                      ytitle(Likert response (%), margin(l-1 r+.5)) xtitle(Fictional math abilities data, margin(t+2)) 
                      gr export bar_Likert.png
                      which will produce this figure:
                      Click image for larger version

Name:	bar_Likert.png
Views:	1
Size:	92.9 KB
ID:	1496341


                      Note that the subject was discussed before on the Statalist here and here.
                      http://publicationslist.org/eric.melse

                      Comment


                      • #12
                        Eric's links as I see them look (a) the same (b) corrupted slightly. This link works for me. Some of the advice, unsurprisingly, is don't do that, do something else instead.

                        https://www.statalist.org/forums/for...kert-type-data

                        Comment

                        Working...
                        X