Announcement

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

  • Stacked bar chart: bar colors and xlabel

    Dear Statalist users,
    I would like to have a stacked bar graph like the one that can be found at http://www.surveydesign.com.au/tipsgraphs.html (code is at the end of this message) but I am having trouble customizing it.

    More specifically, there are 2 things I would like to achieve:
    1. Include an xlabel that says "Year/Quarter"
    2. Change the colors of the bars so that all the bars that correspond to "1" are in red tones and all the ones that correspond to "2" are in blue tones.

    Any help is appreciated.
    Thanks!
    Ana

    --------
    Code
    --------
    // Stacked bar graph - reshaping the data

    //creating the data

    clear
    set obs 1000
    egen id = seq(), block(10)
    egen time = seq(), to(10)
    format time %tq
    gen time2=string(time, "%tq")
    gen sector = ceil(3 * runiform())
    bysort id: replace sector=sector[1]
    label define sector 1 "SECTOR 1" 2 "SECTOR 2" 3 "SECTOR 3"
    label values sector sector
    gen sales=exp(rnormal())*1000
    gen sales2=2*sales

    //reshaping the data
    gen obs = _n
    rename sales sales1
    reshape long sales, i(obs) j(which)

    graph bar (sum) sales , over(sector) over(which) ///
    over(time2, label(labsize(vsmall))) asyvars stack legend(size(vsmall))

  • #2
    Thanks for providing a reproducible example. Red and blue colours and the date format you ask for are easy enough, but to me your stacked bar chart looks a horrible mess. After playing with your data I came up with something quite different, but it may interest others even if it is not at all what you want.

    This code presupposes running yours first.

    Code:
    set scheme s1color 
    collapse (sum) sales, by(sector time which)
    replace sales = sales/1000 
    reshape wide sales , i(sector time) j(which)
    label var sales1 "which 1" 
    label var sales2 "which 2" 
    xtset sector time
    xtline sales1 sales2, byopts(row(3) note(units: 000)) recast(connected) yla(, ang(h)) ///
     subtitle(, pos(9) ring(1)  nobexpand bcolor(none) placement(e)) ///
    lcolor(red blue) mcolor(red blue) xla(1/10, format(%tqCY/q)) xtitle("")
    Click image for larger version

Name:	notstacked.png
Views:	1
Size:	13.0 KB
ID:	1331194

    Comment


    • #3
      Thanks Nick. I agree that your plot looks nicer than the one I gave as an example (which is not my plot, but is rather from the website mentioned above). The example was just an example meant for the statalist users to know what I was trying to achieve in terms of colors and xlabel. The actual graph (with other data) that I'm constructing looks less messy than the example.
      In any case, I still don't know how to
      1. Include an xlabel that says "Year/Quarter" (to be placed under the numbers/labels in the x-axis)
      2. Change the colors of the bars so that all the bars that correspond to "1" are in red tones and all the ones that correspond to "2" are in blue tones.

      Any help is appreciated.
      Thanks,
      Ana

      Comment


      • #4
        I answered #1 already. Applying

        Code:
         format(%tqCY/q)
        to a quarterly date will give dates in that format and the result can be seen in the graph displayed.

        On #2 I get separate red and blue colours easily the way I did it. How you get it with the data structure you had I didn't work out.

        But here's a go, starting from the end of your code. It would need some tidying up.

        Code:
        set scheme s1color 
        collapse (sum) sales, by(sector time which)
        replace sales = sales/1000 
        
        egen group = group(sector which) 
        separate sales, by(group) 
        
        graph bar sales?, over(which) over(time) stack nofill ///
        bar(1, color(red*0.2)) bar(3, color(red*0.6)) bar(5, color(red)) ///
        bar(2, color(blue*0.2)) bar(4, color(blue*0.6)) bar(6, color(blue)) ///
        b1title(Year/Quarter)
        N.B. Edited since first posting.
        Last edited by Nick Cox; 17 Mar 2016, 13:34.

        Comment


        • #5
          I'm sorry if it wasn't clear but that is not what I asked… I want to add an xlabel, meaning a label with the words "Year/Quarter" below the dates. Also, you say that the colors are easy to do, but I have no idea how…
          Thanks

          Comment


          • #6
            Then that's a title you want, not a label. Do your readers need to be told that e.g. 1960q1 means "Year/Quarter"? That's surprising. I spend about 1% of my teaching time persuading students to remove redundant axis titles such as "Date", "Year", "Time".

            But try e.g.

            Code:
            b1title(Year/Quarter)
            Edit: Now added to #4.
            Last edited by Nick Cox; 17 Mar 2016, 13:35.

            Comment


            • #7
              Thanks Nick. Like I mentioned before this was just an example. In my actual plot, it's important to have a title. I thought it was called a label because it can also be called an "xlabel" if we are talking of a scatterplot for example.

              Comment


              • #8
                Not in Stata: I think Stata is totally consistent on distinguishing between axis labels and axis titles. Elsewhere, what Stata calls an axis title is often called an axis label, but that's a different story.

                Comment


                • #9
                  For anyone that is interested in doing 2. mentioned above, a good idea may be to do "separate" beforehand as suggested by Martin in a previous post. This worked for me.

                  Below is a "colorful" example based on the discussion in
                  http://statalist.1588530.n2.nabble.c...td4703521.html

                  *****
                  sysuse auto, clear
                  separate trunk, by(foreign)
                  separate mpg, by(foreign)
                  graph bar trunk0 trunk1 mpg0 mpg1, ///
                  over(foreign, label(angle(forty_five))) ///
                  over(rep78) bar(1, color(green)) bar(2, color(red)) bar(3, color(blue)) bar(4, color(yellow)) stack
                  *****

                  Comment


                  • #10
                    Indeed, separate was key in #4 too.

                    Comment

                    Working...
                    X