Announcement

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

  • Change order of bars using catplot

    I have a dataset where the unit of observations is individuals (ISSP Environment IV). I would like to make a graph of the distribution of answers to a likert-scale question (variable 'v17', which has four levels (1-4)) by country. Drawing on a post by Nick Cox, I have written the following code:

    catplot, over(v17) over(country) percent(country) asyvars stack

    This works well, see attached picture. However, I would like to change the order of the bars. Specifically, they should be ordered by the mean value of v17. With the bar command, that would be achieved with sort(v17), but this option seems not supported by catplot. Is there a way to change the order with catplot? Alternatively, is there another way of creating this kind of graph with another command? It seems like a rather common way to display survey data with categorical values.
    Click image for larger version

Name:	skepsis2020.png
Views:	3
Size:	42.0 KB
ID:	1764805
    Attached Files

  • #2
    Thanks for your interest in catplot.


    sort(v17) does not to my understanding do what you want with say

    Code:
    graph bar (percent)
    or

    Code:
    graph bar (count)
    Please show me code that does this if you think I am wrong. Otherwise I suggest using myaxis from the Stata Journal to sort countries as you wish. Here is some technique.

    Absent a data example, I faked something of similar complexity.

    For the original announcement of myaxis as added to SSC see https://www.statalist.org/forums/for...e-or-graph-use

    For the published paper see Speaking Stata: Ordering or ranking groups of observations - Nicholas J. Cox, 2021 (sagepub.com)


    Code:
    clear
    set obs 26
    gen country = word(c(ALPHA), _n)
    expand 4
    bysort country : gen v17 =  _n
    set seed 314159
    gen freq = runiformint(2, 10)
    expand freq
    set scheme stcolor
    
    * you start here
    catplot, over(v17) over(country) percent(country) asyvars stack name(G1, replace)
    
    myaxis country2=country, sort(mean v17) descending
    
    catplot, over(v17) over(country2) percent(country2) asyvars stack ysize(9) legend(row(1) pos(12)) name(G2, replace)


    Otherwise you've more problems than you say:

    1. You may want to change graph size and shape so that each country name is more easily visible. (Changing the text size is another possibility.)

    2. Value labels for your outcome need to be slimmed down. The informative text is mostly truncated.
    Click image for larger version

Name:	hakon.png
Views:	1
Size:	40.5 KB
ID:	1764821

    Comment


    • #3
      Many thanks for your swift and very helpful reply, Nick! This solved the issue perfectly. Hope the example will be of help to other users as well. I had looked at your myaxis article already, but I didn't manage to adapt it for my purpose.

      By the way, I use the graph bar code for the simpler task of plotting mean values, and then sorting countries by that same value is of course straight-forward:
      Code:
      graph hbar (mean) v17, over(country, sort(v17))
      All best, HÃ¥kon

      Comment


      • #4
        Thanks for the report!

        Comment

        Working...
        X