Announcement

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

  • bar chart multiple global ifs

    Hello,

    I am working on my masters thesis and am having some troubles visualizing the data as I would like. I have 15 variable cat_1 to cat_15 that are binary variables 0/1, and I would like to plot them on one single bar graph but only for when the value is 1, not 0. At the moment I have these 15 separate graphs:

    graph bar (count) if cat_1 == 1, over(year)
    graph bar (count) if cat_2 == 1, over(year)
    graph bar (count) if cat_3 == 1, over(year)
    graph bar (count) if cat_4 == 1, over(year)
    graph bar (count) if cat_5 == 1, over(year)
    graph bar (count) if cat_6 == 1, over(year)
    graph bar (count) if cat_7 == 1, over(year)
    graph bar (count) if cat_8 == 1, over(year)
    graph bar (count) if cat_9 == 1, over(year)
    graph bar (count) if cat_10 == 1, over(year)
    graph bar (count) if cat_11 == 1, over(year)
    graph bar (count) if cat_12 == 1, over(year)
    graph bar (count) if cat_13 == 1, over(year)
    graph bar (count) if cat_14 == 1, over(year)
    graph bar (count) if cat_15 == 1, over(year)

    Any tips as to how to get these onto the same plot?

    Thanks

  • #2
    Some ideas:

    1. If you are plotting frequencies over time, I would recommend a line graph.
    2. With that many series, you are likely to run into the spaghetti problem. See https://journals.sagepub.com/doi/10....6867X211025838.

    No data example, so I create some data and use fabplot from the Stata Journal below.

    Code:
    clear
    set obs 5000  
    set seed 04292023
    forval i=1/15{
        if inlist(`i', 2, 3, 7, 9, 13){
            gen cat_`i'=runiform(0, 1)<0.6
        }
        else{
            gen cat_`i'= runiform(0,1)<0.8
        }
    }
    gen year=runiformint(2000, 2021)
    
    *START HERE
    preserve
    collapse (sum) cat_*, by(year)
    rename cat_* total*
    reshape long total, i(year) j(which)
    set scheme s1mono
    *findit fabplot to install
    fabplot line total year, by(which, col(3)) ytitle("Frequency") xtitle("")
    restore
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	184.8 KB
ID:	1711705


    Last edited by Andrew Musau; 29 Apr 2023, 06:51.

    Comment


    • #3
      It seems unlikely that the variables (questions) are best ordered as they come, but as Andrew Musau rightly flags, we have no data example to play with.

      Comment

      Working...
      X