Announcement

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

  • Stacked Bar Chart: Date Labels dont work

    Hello everyone,

    I created the stacked bar chart attached using the variables and the code below:

    Code:
    graph bar, over(party) over(quarter_created, label(angle(45))) missing stack asyvars
    My problem is that the variable on the x-axis doesn't take the labels I assigned to it, but number instead. It is in float format and has date-labels for each quarter, which I assigned using this code:

    Code:
    gen date_created = date(created_at, "YMD hms")
    format date_created %td
    gen quarter_created = qofd(date_created)
    format quarter_created %tqCCYY-!Qq
    Where created_at originally is in string format. Example: "2008-05-15 11:26:35"
    And quarter_created is in float format with labels like: "2008q3"

    I would be very happy if you could help me with my problem.
    Attached Files

  • #2
    Wrong description. Your date variable doesn't have value labels attached at all, but a date display format, which graph bar won't honour. That's a bug, or at least a limitation. You could assign each date its own value label, but that still leaves problems.

    You would be immensely better off with twoway bar instead when you could just use fewer axis labels. Indeed the categorisation does not work well either, but that is another story.

    Give some example data, and I will illustrate.

    Comment


    • #3
      Thank you so much for your answer! I have been trying to use twoway bar on my own but didn't manage to get the results above with the legend and colors. Here is some random sample data from my project (thereby it's not really important if the x-axis is in percent of all observations or just the total number of observations per quarter created. To make it easier I also included the quarter variable without the labels.

      Code:
      quarter_created    party_id    quarter_created_nolabels
      2011-Q4    partyC    207
      2009-Q2    partyC    197
      2018-Q2    partyE    233
      2009-Q2    partyF    197
      2010-Q2    partyF    201
      2009-Q2    partyE    197
      2013-Q2    partyH    213
      2017-Q2    partyG    229
      2018-Q2    partyB    233
      2016-Q4    partyB    227
      2017-Q4    partyB    231
      2017-Q3    partyH    230
      2014-Q2    partyB    217
      2016-Q2    partyB    225
      2011-Q3    partyC    206
      2017-Q1    partyH    228
      2008-Q3    partyF    194
      2015-Q3    partyH    222
      2009-Q2    partyF    197
      2009-Q2    partyE    197
      2013-Q2    partyG    213
      2017-Q2    partyE    229
      2012-Q4    partyH    211
      2018-Q1    partyE    232
      2009-Q4    partyG    199
      2009-Q1    partyG    196
      2008-Q4    partyH    195
      2014-Q2    partyC    217
      2013-Q1    partyE    212
      2009-Q4    partyB    199
      2008-Q4    partyF    195
      2008-Q3    partyH    194
      2009-Q4    partyC    199
      2016-Q4    partyB    227
      2014-Q4    partyH    219
      2011-Q2    partyH    205
      2009-Q2    partyC    197
      2009-Q2    partyE    197
      2010-Q2    partyH    201
      2009-Q4    partyE    199
      2017-Q2    partyB    229
      2014-Q3    partyH    218
      2015-Q1    partyB    220
      2018-Q1    partyE    232
      2017-Q1    partyH    228
      2009-Q2    partyH    197
      2016-Q4    partyC    227
      2018-Q1    partyE    232
      2018-Q1    partyF    232
      2012-Q3    partyC    210
      Thank you very much!
      Last edited by Felix Meier; 15 Nov 2018, 02:10.

      Comment


      • #4
        Thanks for the data example, but please use dataex (FAQ Advice #12)

        I use tabplot (Stata Journal) which must be installed first.

        https://www.statalist.org/forums/for...updated-on-ssc is perhaps the best way in if this command is new to you.

        Here's my code, most of which is just creating your data example.

        Code:
        clear 
        input str7 (quarter_created    party_id)    quarter_created_nolabels
        2011-Q4    partyC    207
        2009-Q2    partyC    197
        2018-Q2    partyE    233
        2009-Q2    partyF    197
        2010-Q2    partyF    201
        2009-Q2    partyE    197
        2013-Q2    partyH    213
        2017-Q2    partyG    229
        2018-Q2    partyB    233
        2016-Q4    partyB    227
        2017-Q4    partyB    231
        2017-Q3    partyH    230
        2014-Q2    partyB    217
        2016-Q2    partyB    225
        2011-Q3    partyC    206
        2017-Q1    partyH    228
        2008-Q3    partyF    194
        2015-Q3    partyH    222
        2009-Q2    partyF    197
        2009-Q2    partyE    197
        2013-Q2    partyG    213
        2017-Q2    partyE    229
        2012-Q4    partyH    211
        2018-Q1    partyE    232
        2009-Q4    partyG    199
        2009-Q1    partyG    196
        2008-Q4    partyH    195
        2014-Q2    partyC    217
        2013-Q1    partyE    212
        2009-Q4    partyB    199
        2008-Q4    partyF    195
        2008-Q3    partyH    194
        2009-Q4    partyC    199
        2016-Q4    partyB    227
        2014-Q4    partyH    219
        2011-Q2    partyH    205
        2009-Q2    partyC    197
        2009-Q2    partyE    197
        2010-Q2    partyH    201
        2009-Q4    partyE    199
        2017-Q2    partyB    229
        2014-Q3    partyH    218
        2015-Q1    partyB    220
        2018-Q1    partyE    232
        2017-Q1    partyH    228
        2009-Q2    partyH    197
        2016-Q4    partyC    227
        2018-Q1    partyE    232
        2018-Q1    partyF    232
        2012-Q3    partyC    210
        end 
        
        replace party_id = substr(party_id, -1, 1)
        set scheme s1color 
        tabplot party_id quarter_created_nolabels, xasis xla(, noticks ang(v) format("%tqCY!Qq")) xtitle("") bfcolor(none) showval ytitle("")
        I suggest that this is a more direct display which avoids a confusing multi-colour stacking with the need to decode using a legend or key and much mental back and forth. I have no idea what the data show: perhaps the full dataset is enormously larger and there is some reason why you use coy labels B C D E F G H.


        Click image for larger version

Name:	party.png
Views:	1
Size:	20.8 KB
ID:	1470508

        Comment


        • #5
          Thank you again! The graph you suggest is interesting, though I still believe that a stacked bar chart would be better. This is the case since the main information of the graph is the height of the bars (in my data it means when politicians entered twitter and whether this happened just before the elections). Which party they belong to is just a sidenote and interesting, but not super important. For this reason it'd be great to have a stacked bar chart with coloring by party and height related to how many people entered in which quarter. Is there any way to do this in Stata? The total dataset is not super large and only consists of 460 observations.

          Comment


          • #6
            Here's something to get you started:

            Code:
            replace party_id = substr(party_id, -1, 1)
            drop quarter_created 
            rename quarter quarter 
            format quarter %tq 
            contract party_id quarter, zero  
            
            separate _freq, by(party_id) 
            mvencode _freq*, mv(0) override 
            
            gen bar1 = _freq1
            
            forval j = 2/6 {
                local J = `j' - 1 
                gen bar`j' = bar`J' + _freq`j' 
            } 
            
            twoway bar bar1 quarter || /// 
            rbar bar1 bar2 quarter  || /// 
            rbar bar2 bar3 quarter  || /// 
            rbar bar3 bar4 quarter  || /// 
            rbar bar4 bar5 quarter  || /// 
            rbar bar5 bar6 quarter  || /// 
            , legend(order(1 "B" 2 "C" 3 "E" 5 "F" 6 "G"))

            Comment

            Working...
            X