Announcement

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

  • Plotting multiple binary variables for multiple observations

    Hi all,

    I have data similar to the table below.
    A B C D
    Var1 1 0 0 1
    Var2 0 0 1 1
    Var3 1 0 1 0
    Var4 0 1 1 0
    Say A-D are schools; Var 1- 4 are facilities that the school might or might not have (coded as 1 and 0 respectively). I want to make a plot of this table with each school on the y-axis and each facility on the x-axis with 1 plotted as a bar with some colour and 0 plotted as a bar with no colour (or a different colour), but on the same line. essentially not four different bars for each school, but one single bar that changes colour. For example, if 1 is green and 0 is red, school A should be plotted as GRRG. Is there any way to do this in STATA? Thanks in advance.

  • #2
    You can get what you want by installing heatplot from SSC. A variant I sometimes see are punch card charts (see https://www.statalist.org/forums/for...bers-and-graph) .

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str4 variables byte(A B C D)
    "Var1" 1 0 0 1
    "Var2" 0 0 1 1
    "Var3" 1 0 1 0
    "Var4" 0 1 1 0
    end
    
    rename  (A B C D) value=
    gen id=_n
    reshape long value, i(id) j(which) string
    encode variables, gen(var)
    encode which, gen(cat)
    heatplot value i.cat i.var, colors(grey%25 gray%90) ///
    ylab(, noticks nogrid) xlab(,noticks nogrid) plotregion(margin(zero)) ///
    leg(order(1 "No" 2 "Yes") subtitle("") size(4))
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	18.5 KB
ID:	1750730

    Comment


    • #3
      Thanks a lot Mr.Musau..that was super helpful!

      Comment


      • #4
        Note that red and green should never be plotted together. Difficulty in distinguishing red and green is a common type of colour-blindness.

        See e.g. https://www.nature.com/articles/nmeth.1618

        I would much favour the punch card variant myself. I have also seen similar plots that look like pieces on a Go board.

        Comment


        • #5
          Code:
          clear
          input str10 variables byte(has1 has2 has3 has4)
          "facility_1" 1 0 0 1
          "facility_2" 0 0 1 1
          "facility_3" 1 0 1 0
          "facility_4" 0 1 1 0
          end
          reshape long has , i(variables) j(school)
          encode variables , gen(facility)
          label define has_lb 0 "not" 1 "has"
          label values has has_lb
          separate school, by(has) veryshortlabel
          scatter school0 school1 facility, ///
             msymbol(Oh O) mcolor(black black) msize(vlarge vlarge) ///
             ytitle(school)                                         ///
             yscale(range(0.9 4.1)) xscale(range(0.9 4.1))
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	63.0 KB
ID:	1750918
          ---------------------------------
          Maarten L. Buis
          University of Konstanz
          Department of history and sociology
          box 40
          78457 Konstanz
          Germany
          http://www.maartenbuis.nl
          ---------------------------------

          Comment


          • #6
            Naturally the success of any plot will depend on

            how many schools and how many facilities are to be found in the real full dataset

            whether school identifier or facility identifier is informative -- if not sorting on either or both variables may help identification of coarse or fine structure in the data.

            Comment


            • #7
              Thank you all! much appreciated!

              Comment

              Working...
              X