Announcement

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

  • Creating graphs from two distinct categorical variables

    Dear All,

    I tried several time using different options, over(), by() but couldn't create a bar graph that I wanted. I have 2 categorical variables, occupation_before and occupation_after; both of them have same categories as shown in the sample dataset below. I wanted to create a bar where I can easily see the frequency of occupation before and after (in one chart). I tried using catplot, but I didn't find a way for two categorical variables with same categories.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(occubefore occuafter)
    2 6
    2 2
    6 2
    2 1
    1 1
    2 2
    1 2
    2 2
    3 3
    1 1
    1 1
    1 2
    3 3
    1 1
    6 6
    2 2
    1 2
    6 4
    1 1
    2 2
    4 4
    1 1
    1 1
    1 1
    1 1
    1 1
    1 4
    2 2
    1 1
    2 2
    2 1
    1 1
    1 1
    1 1
    1 1
    1 1
    5 6
    1 1
    1 1
    1 6
    1 1
    4 6
    3 2
    1 1
    5 6
    1 6
    1 1
    3 3
    1 1
    4 1
    2 1
    3 3
    2 2
    2 2
    1 1
    2 2
    2 6
    2 2
    1 1
    2 2
    4 4
    1 1
    1 1
    6 6
    1 1
    1 2
    1 1
    1 1
    1 1
    1 1
    2 6
    2 2
    2 5
    1 4
    1 1
    2 2
    2 6
    1 1
    1 1
    1 1
    6 6
    1 1
    3 3
    1 1
    1 1
    1 1
    1 1
    1 1
    1 6
    3 3
    1 1
    1 1
    1 1
    end
    label values occubefore occubefore
    label def occubefore 1 "Agriculture", modify
    label def occubefore 2 "Business", modify
    label def occubefore 3 "Government service", modify
    label def occubefore 4 "Private employee", modify
    label def occubefore 5 "Daily wage labourer", modify
    label def occubefore 6 "Others", modify
    label values occuafter occuafter
    label def occuafter 1 "Agriculture", modify
    label def occuafter 2 "Business", modify
    label def occuafter 3 "Government service", modify
    label def occuafter 4 "Private employee", modify
    label def occuafter 5 "Daily wage labourer", modify
    label def occuafter 6 "Others", modify
    I would appreciate your help in this. Also I wanted to perform similar with pie charts as well. Let me know if there is a way to do it. Thank you in advance.


  • #2
    Pie charts? Just say no. You don't show what you tried with catplot (SSC, as you are asked to explain), but that won't be very successful for this kind of dataset.

    I'd recommend rather tabplot (Stata Journal). See e.g. https://www.statalist.org/forums/for...updated-on-ssc

    Code:
    tabplot occuafter occubefore, showval ytitle("") xtitle("") xsc(alt) bfcolor(green*0.3)
    Click image for larger version

Name:	matrixplot.png
Views:	1
Size:	17.9 KB
ID:	1450903


    Clearly the x axis labels need some work. Edit them to go on two or three lines, not one.

    You can get probabilities or percents if you want.

    An advantage of this plot is that you can talk about the principal diagonal (showing instances of no change).

    Comment


    • #3
      Here's a second take. I fixed the value labels in advance. I just had to make one change in the Editor, to move the vertical axis title. Whenever a graph has some table flavour, it can seem natural, or at least conventional, to move the horizontal axis stuff to the top. More on that within https://www.stata-journal.com/sjpdf....iclenum=gr0053

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input double(occubefore occuafter)
      2 6
      2 2
      6 2
      2 1
      1 1
      2 2
      1 2
      2 2
      3 3
      1 1
      1 1
      1 2
      3 3
      1 1
      6 6
      2 2
      1 2
      6 4
      1 1
      2 2
      4 4
      1 1
      1 1
      1 1
      1 1
      1 1
      1 4
      2 2
      1 1
      2 2
      2 1
      1 1
      1 1
      1 1
      1 1
      1 1
      5 6
      1 1
      1 1
      1 6
      1 1
      4 6
      3 2
      1 1
      5 6
      1 6
      1 1
      3 3
      1 1
      4 1
      2 1
      3 3
      2 2
      2 2
      1 1
      2 2
      2 6
      2 2
      1 1
      2 2
      4 4
      1 1
      1 1
      6 6
      1 1
      1 2
      1 1
      1 1
      1 1
      1 1
      2 6
      2 2
      2 5
      1 4
      1 1
      2 2
      2 6
      1 1
      1 1
      1 1
      6 6
      1 1
      3 3
      1 1
      1 1
      1 1
      1 1
      1 1
      1 6
      3 3
      1 1
      1 1
      1 1
      end
      
      label values occubefore occubefore
      label def occubefore 1 "Agriculture", modify
      label def occubefore 2 "Business", modify
      label def occubefore 3 `" "Government" "service" "', modify
      label def occubefore 4 `" "Private" "employee" "', modify
      label def occubefore 5 `" "Daily wage" "labourer" "', modify
      label def occubefore 6 "Others", modify
      label values occuafter occubefore
      
      set scheme s1color 
      tabplot occuafter occubefore, showval ytitle("After") ysc(titlegap(0)) yla(, labsize(small)) ///
      xtitle("Before") xsc(alt) xla(, labsize(small)) bfcolor(green*0.3)
      Click image for larger version

Name:	matrixplot.png
Views:	1
Size:	26.0 KB
ID:	1451006

      Comment

      Working...
      X