Announcement

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

  • creating a percentage graph bar

    Dear Statalist

    I would like to make a graph bar but with percentages instead of the count. However, I am interested that those percentages to be 100% for each bar in total. In fact, I would like to mimic the percentage in the table below. Adding the option "percent" to the graph I´ll show below gives the percentages over the whole quantity, which is what I do not want to.
    Is there any simple way to do it?

    Thanks in advance.

    Code:
    tab x1 year, col
    Code:
               |               year
            x1 |      2014       2017       2020 |     Total
    -----------+---------------------------------+----------
             1 |     1,775      2,170      2,756 |     6,701 
               |     27.58      28.21      32.03 |     29.48 
    -----------+---------------------------------+----------
             2 |     4,660      5,522      5,848 |    16,030 
               |     72.42      71.79      67.97 |     70.52 
    -----------+---------------------------------+----------
         Total |     6,435      7,692      8,604 |    22,731 
               |    100.00     100.00     100.00 |    100.00
    Code:
    graph bar (count) x1, over(x2) over(year) asyvar stack blabel(bar, box bfcolor(white) pos(center) format(%4.2f) size(medium)) legend(off) ytitle("") title("from 2014")
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(year x1 x2)
    2014 1 1
    2014 1 1
    2014 2 2
    2014 1 1
    2014 2 2
    2014 2 2
    2014 1 1
    2014 2 2
    2014 2 2
    2014 2 2
    2014 . .
    2014 . .
    2014 . .
    2014 2 2
    2014 2 2
    2014 2 2
    2014 2 2
    2014 2 2
    2014 2 2
    2014 2 2
    2017 . .
    2017 1 1
    2017 2 2
    2017 . .
    2017 2 2
    2017 2 2
    2017 . .
    2017 2 2
    2017 1 1
    2017 1 1
    2017 2 2
    2017 2 2
    2017 2 2
    2017 1 1
    2017 2 2
    2017 2 2
    2017 2 2
    2017 1 1
    2017 1 1
    2017 1 1
    2017 2 2
    2020 2 2
    2020 1 1
    2020 2 2
    2020 1 1
    2020 . .
    2020 . .
    2020 1 1
    2020 1 1
    2020 1 1
    2020 2 2
    2020 2 2
    2020 2 2
    2020 2 2
    2020 2 2
    2020 2 2
    2020 2 2
    2020 1 1
    2020 2 2
    2020 2 2
    2020 2 2
    2020 2 2
    end

  • #2
    In your data example x1 and x2 are always equal and your table command doesn't involve x2 while your graph command does. So, I am a little unclear what you want.

    My prejudices include an idea that stacked designs are oversold. The fact that percents add to 100 is a matter of definition and it can be hard to follow categories on a stacked plot that are rare or non-existent. Legends are also an evil that should be avoided whenever possible. For all those reasons I favour what might be called a multiple bar chart instead.

    See
    gr0061_2 for the latest files, https://www.stata-journal.com/articl...article=gr0066 for the fullest write-up and https://www.statalist.org/forums/for...updated-on-ssc for a quick overview.


    SJ-20-3 gr0066_2 . . . . . . . . . . . . . . . . Software update for tabplot
    (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
    Q3/20 SJ 20(3):757--758
    added new options frame() and frameopts() allowing framing
    of bars and so-called thermometer plots or charts

    SJ-17-3 gr0066_1 . . . . . . . . . . . . . . . . Software update for tabplot
    (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
    Q3/17 SJ 17(3):779
    added options for reversing axis scales; improved handling of
    axis labels containing quotation marks

    SJ-16-2 gr0066 . . . . . . Speaking Stata: Multiple bar charts in table form
    (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
    Q2/16 SJ 16(2):491--510
    provides multiple bar charts in table form representing
    contingency tables for one, two, or three categorical variables



    Here's some token code for your data example.

    Code:
    set scheme s1color 
    tabplot x1 year, percent(year) separate(x1) yreverse showval bar1(fcolor(red*0.2) lcolor(red))  bar2(lcolor(blue) fcolor(blue*0.2))
    Click image for larger version

Name:	tabplot_x1_year .png
Views:	1
Size:	23.2 KB
ID:	1589317

    The plot can be extended to three handles very easily using something like


    Code:
    tabplot x1 x2, by(year) 
    
    tabplot x1 year, by(x2)
    and percents can be defined any way you wish.

    Comment


    • #3
      Dear Nick Cox, thanks for your help. I understand that it is confusing to have x2 in the graph but not in the table. In fact, x2 was just something I did in order to get within each year bar, the differentiation with respect to the categories of x1. This tabplot is everything I need (and more). Thanks for the suggestions.

      Comment


      • #4
        Pleased it helped. Naturally the percents of two exclusive categories sum to 100, and you only need one of them. Hence you could also do something like


        Code:
        egen toshow = mean(100 * (x1 == 1)), by(year)
        
        graph bar (mean) toshow, over(year) ytitle(% of whatever) blabel(total, format(%2.1f))

        Comment

        Working...
        X