Announcement

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

  • Bar chart with multiple variables on the x-axis

    Morning all,

    For each person in my dataset, I have the treatment they were given as one variable (with 5 categories) and the potential diagnoses in multiple variables (dichotomous: 0, 1) which are not mutually exclusive. I have created a bar chart showing the percentage of those with diagnosis 1 who had each treatment.

    graph bar (percent) id if diag1==1, over(treatment)

    So far so good. But I want to be able to include the equivalent for each diagnosis in one plot and this is where I am having problems. If the dataset could be reshaped/collapsed so that the diagnoses were in one variable I could just use two over() options in my plot but this doesn't work since my diagnoses are not mutually exclusive. Everything I have found via google and the forum discusses reshaping/collapsing.

    Anyone have any ideas how to do this?

    Laura

  • #2
    If I understand this correctly designplot from SJ may help here. A bug fix for Stata 14 is imminent in SJ 15(2).

    See also here http://www.statalist.org/forums/foru...riptive-tables

    There was a write up in http://www.stata-journal.com/article...article=gr0061
    Last edited by Nick Cox; 11 Jun 2015, 05:45.

    Comment


    • #3
      Thanks Nick: a useful command to know. It is getting me closer to what I am after but still not quite there. Using:

      designplot id treatment diag1 diag2, statistics(N) minway(2) maxway(2) recast(hbar)

      gives me useful summaries for treatment within each diagnosis but it also gives diag1 vs diag2 which I don't want. I have 6 diagnosis variables so it will get messy very quickly. My diagnosis variables can take values of 0 (not diagnosed with this condition) or 1 (diagnosed) so it plots both when I only want it to plot those where it equals to 1. I recoded all my diagnoses variables to be 1 if present or missing otherwise but diag1 does not coexist with any other diagnosis (by design) so it just gives the error "no observations".

      What I'm trying to achieve is the equivalent of typing

      graph bar (percent) id if diag1==1, over(treatment)
      graph bar (percent) id if diag2==1, over(treatment)
      graph bar (percent) id if diag3==1, over(treatment)
      graph bar (percent) id if diag4==1, over(treatment)
      .
      .
      .
      etc

      and getting them all on one plot.
      Last edited by llmiller; 11 Jun 2015, 07:01.

      Comment


      • #4
        I have now managed to create what I need using some jiggery pokery.

        postfile plot str30 treatment str30 diag c1 c2 c3 c4 c5 c1p c2p c3p c4p c5p using treat4diag.dta, replace
        foreach diag in diag1 diag2 diag3 diag4 diag5 diag6 {
        tabcount treatment if `diag'==1, v(1/5) zero matrix(treatment)
        local treatment_1=treatment[1,1]
        local treatment_2=treatment[2,1]
        local treatment_3=treatment[3,1]
        local treatment_4=treatment[4,1]
        local treatment_5=treatment[5,1]
        local treatment_1p=`treatment_1'/(`treatment_1'+`treatment_2'+`treatment_3'+`treatm ent_4'+`treatment_5')
        local treatment_2p=`treatment_2'/(`treatment_1'+`treatment_2'+`treatment_3'+`treatm ent_4'+`treatment_5')
        local treatment_3p=`treatment_3'/(`treatment_1'+`treatment_2'+`treatment_3'+`treatm ent_4'+`treatment_5')
        local treatment_4p=`treatment_4'/(`treatment_1'+`treatment_2'+`treatment_3'+`treatm ent_4'+`treatment_5')
        local treatment_5p=`treatment_5'/(`treatment_1'+`treatment_2'+`treatment_3'+`treatm ent_4'+`treatment_5')
        post plot ("treatment") ("`diag'") (`treatment_1') (`treatment_2') (`treatment_3') (`treatment_4') (`treatment_5') (`treatment_1p') (`treatment_2p') (`treatment_3p') (`treatment_4p') (`treatment_5p')
        }
        postclose plot

        Then use this dataset:

        graph bar (mean) c1p c2p c3p c4p c5p, over(diag)


        I'm sure there is a more elegant way to do this but this works. Thought I'd post it in case others find it useful.

        Thanks Nick for your help in making me rethink how I approached it.

        Laura

        Comment


        • #5
          Another way to do it is to use the saveresults() option of designplot and then ignore the cross-combination you don't want.

          Comment

          Working...
          X