Announcement

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

  • -catplot- proportional bar graph problem

    Dear Statalist,

    I want to make a graph describing stack quartile constitution bar graph total to 100%.
    My dataset consists of
    area: area code 31 areas in total
    quartile: social development quartile 1-Q1 2-Q2 3-Q3 4-Q4
    q_num: number of sub-areas in different quartiles
    percent: quartile composition

    This is my dataset google drive link
    https://drive.google.com/file/d/0Bz83KppKhKQ6WjlmRUVBUzlTNUU/view?usp=sharing


    I tried the command below:
    catplot quartile area [fw=q_num], asyvars stack percent(quartile) legend(rows(1) stack)
    and only get this graph:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	13.4 KB
ID:	1414750


    My problems:
    1)the x-axis percent is not equal to 100,
    2)the layout of areas label are too condensed, anyway to extend the y-axis space?
    3)I want to change the order of areas according to the ranking of the proportion of first by quartile=4 and then by quartile=3, quartile=2, consequently, since not every area has quartile=4, quartile=3, quartile=2.


    I am appreciated anyone can help me out~
    Many thanks!

  • #2
    Thanks for posting the dataset. See FAQ Advice #12 for advice to use dataex (SSC) and to explain where user-written programs you mention come from. Here catplot is from SSC.

    Here is your data to save anyone downloading it.

    Code:
    clear
    input byte area str1 quartile float(q_num percent)
    34 `"1"' 23 21.90476226806641
    34 `"2"' 15 14.2857141494751
    34 `"3"' 31 29.5238094329834
    34 `"4"' 36 34.28571319580078
    11 `"1"' 18 100
    50 `"1"' 10 25
    50 `"2"' 7 17.5
    50 `"3"' 12 30
    50 `"4"' 11 27.5
    35 `"1"' 28 33.33333206176758
    35 `"2"' 42 50
    35 `"3"' 14 16.66666603088379
    62 `"1"' 11 12.64367771148682
    62 `"2"' 9 10.34482765197754
    62 `"3"' 15 17.24137878417969
    62 `"4"' 52 59.77011489868164
    44 `"1"' 56 45.5284538269043
    44 `"2"' 31 25.2032527923584
    44 `"3"' 33 26.82926750183106
    44 `"4"' 3 2.439024448394775
    45 `"1"' 19 17.27272796630859
    45 `"2"' 17 15.45454502105713
    45 `"3"' 27 24.54545402526856
    45 `"4"' 47 42.72727203369141
    52 `"1"' 6 6.818181991577148
    52 `"3"' 5 5.681818008422852
    52 `"4"' 71 80.68181610107422
    46 `"1"' 4 19.0476188659668
    46 `"2"' 6 28.5714282989502
    46 `"3"' 11 52.38095092773438
    13 `"1"' 36 20.93023300170898
    13 `"2"' 50 29.06976699829102
    13 `"3"' 59 34.30232620239258
    13 `"4"' 27 15.69767475128174
    23 `"1"' 52 39.39393997192383
    23 `"2"' 57 43.18181991577148
    23 `"3"' 19 14.39393901824951
    23 `"4"' 4 3.030303001403809
    41 `"1"' 36 22.64151000976563
    41 `"2"' 27 16.98113250732422
    41 `"3"' 35 22.0125789642334
    41 `"4"' 61 38.36478042602539
    42 `"1"' 29 28.15534019470215
    42 `"2"' 30 29.12621307373047
    42 `"3"' 23 22.33009719848633
    42 `"4"' 21 20.38834953308106
    43 `"1"' 28 22.95081901550293
    43 `"2"' 27 22.13114738464356
    43 `"3"' 33 27.04918098449707
    43 `"4"' 34 27.86885261535645
    15 `"1"' 28 27.7227725982666
    15 `"2"' 27 26.73267364501953
    15 `"4"' 18 17.8217830657959
    32 `"1"' 56 52.8301887512207
    32 `"2"' 32 30.18867874145508
    32 `"3"' 17 16.03773498535156
    32 `"4"' 1 .9433962106704712
    36 `"1"' 16 16.1616153717041
    36 `"2"' 36 36.3636360168457
    36 `"3"' 37 37.37373733520508
    36 `"4"' 10 10.1010103225708
    22 `"1"' 20 33.33333206176758
    22 `"2"' 24 40
    22 `"3"' 13 21.66666603088379
    22 `"4"' 3 5
    21 `"1"' 50 50
    21 `"2"' 19 19
    21 `"3"' 23 23
    21 `"4"' 8 8
    64 `"1"' 5 22.72727203369141
    64 `"2"' 8 36.3636360168457
    64 `"3"' 4 18.18181800842285
    63 `"1"' 8 17.39130401611328
    63 `"2"' 5 10.8695650100708
    63 `"3"' 9 19.56521797180176
    63 `"4"' 24 52.17391204833984
    61 `"1"' 17 15.88785076141357
    61 `"2"' 27 25.23364448547363
    61 `"3"' 44 41.12149429321289
    61 `"4"' 19 17.75700950622559
    37 `"1"' 31 22.14285659790039
    37 `"2"' 42 30
    37 `"3"' 48 34.28571319580078
    37 `"4"' 19 13.5714282989502
    31 `"1"' 16 88.88888549804688
    31 `"2"' 2 11.11111068725586
    14 `"1"' 20 16.80672264099121
    14 `"2"' 30 25.21008491516113
    14 `"3"' 48 40.33613586425781
    14 `"4"' 21 17.64705848693848
    51 `"1"' 25 13.81215476989746
    51 `"2"' 31 17.12707138061523
    51 `"3"' 46 25.41436386108398
    51 `"4"' 79 43.64640808105469
    12 `"1"' 11 68.75
    12 `"2"' 3 18.75
    12 `"3"' 2 12.5
    54 `"1"' 1 1.369863033294678
    54 `"2"' 4 5.479452133178711
    54 `"3"' 6 8.219178199768066
    54 `"4"' 62 84.93150329589844
    65 `"1"' 25 25.51020431518555
    65 `"2"' 49 50
    65 `"3"' 24 24.48979568481445
    53 `"1"' 6 4.651162624359131
    53 `"2"' 13 10.07751941680908
    53 `"3"' 31 24.03100776672363
    53 `"4"' 79 61.24031066894531
    33 `"1"' 27 30
    33 `"2"' 41 45.55555725097656
    33 `"3"' 20 22.22222137451172
    33 `"4"' 2 2.222222328186035
    end
    
    
    set scheme s1color
    
    * #1
    catplot quartile area [fw=q_num], asyvars stack percent(area) legend(rows(1) stack)
    
    *  #2
    gen group = area >= 40
    catplot quartile area [fw=q_num], asyvars stack percent(area) legend(rows(1) stack) ///
    by(group, note("")) nofill subtitle("", fcolor(none))
    
    * #3
    tabplot quartile area [fw=q_num], percent(area) subtitle(% in each area) ///
    yreverse  xla(, labsize(small)) showval(mlabsize(vsmall) format(%2.0f))
    
    #4
    fillin area quartile
    replace q_num = 0 if q_num == .
    
    egen total = total(q_num), by(area)
    gen prop = q_num / total
    
    forval j = 1/4 {
        egen rank`j' = rank(-prop) if quartile == "`j'"
        bysort area (rank`j') : replace rank`j' = rank`j'[1]
    }
    
    egen area2 = group(rank4 rank3 rank2 rank1 area)
    labmask area2, values(area)
    
    tabplot quartile area2 [fw=q_num], percent(area) subtitle(% in each area) ///
    yreverse  xla(, labsize(small)) showval(mlabsize(vsmall) format(%2.0f)) ///
    xtitle(area)
    Your most obvious problem is using the wrong variable for percent(). See #1 above.

    The crowding problem can be relieved with a trick: see #2 above. Note from help graph bar that what you call the x-axis is in this case the y-axis!

    In my view you get a clearer graph with tabplot (SJ), which you must install from the updated files.

    Code:
    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
    See #3 for a first result.

    Your sorting is harder work but I think you see what you want. See #4. You need to install labmask (SJ) for that to work.

    Code:
    . search labmask, sj
    
    Search of official help files, FAQs, Examples, SJs, and STBs
    
    SJ-8-2  gr0034  . . . . . . . . . .  Speaking Stata: Between tables and graphs
            (help labmask, seqvar if installed) . . . . . . . . . . . .  N. J. Cox
            Q2/08   SJ 8(2):269--289
            outlines techniques for producing table-like graphs
    Note that your percent variable seems inconsistent. You owe me about 20 minutes trying to find a bug in my code that was down to the data.

    Code:
    . egen totalpc = total(percent), by(area)
    
    . tabdisp area if totalpc != 100, c(totalpc)
    
    ----------------------
         area |    totalpc
    ----------+-----------
           15 |   72.27723
           52 |   93.18182
           64 |   77.27273
    ----------------------
    Click image for larger version

Name:	quartile_tabplot.png
Views:	1
Size:	28.5 KB
ID:	1414774

    Comment


    • #3
      Dear Nick,
      It is so nice of you. Thank you for pointing dataex (SSC) issues as well.
      It is a stunning graph that can illustrate my research interests. But I think I prefer a graph like this shown below,
      a stack 100% bar graph.
      Where the age categories are the area code, while different colour indicates different quartiles.
      Click image for larger version

Name:	fetch?id=1335276&d=1460469792&type=full.png
Views:	2
Size:	12.5 KB
ID:	1414838


      Many thanks for your help!

      Zhicheng
      Attached Files

      Comment


      • #4
        I seriously recommend a field test with people who might be consumers of such a graph.

        Otherwise good luck on making that legible and intelligible with 31 areas!

        Comment


        • #5
          Hi Nick,
          I learn to modify several options and make the graph meeting my requirement.
          Thank you for your help again and I am sorry for the inappropriate requests made here.

          Comment

          Working...
          X