Announcement

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

  • Categorical Overlay on Bar Graphs

    I'm having some trouble finding the right way to make this graph. I want to combine two bar charts and have them overlayed on each other, with the more general categorical variable overlaying the more specific one.

    Here are the variables I am working with
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double Q_RecaptchaScore float(RecaptchaFails recaptchafails)
     .4000000059604645 1 1
     .8999999761581421 0 0
     .8999999761581421 0 0
     .8999999761581421 0 0
      .800000011920929 0 0
                     . . .
      .699999988079071 0 0
     .8999999761581421 0 0
    .10000000149011612 1 1
      .800000011920929 0 0
     .8999999761581421 0 0
     .8999999761581421 0 0
      .800000011920929 0 0
                     1 0 0
      .800000011920929 0 0
                     . . .
      .800000011920929 0 0
                     . . .
     .8999999761581421 0 0
     .8999999761581421 0 0
      .800000011920929 0 0
    .30000001192092896 1 1
     .8999999761581421 0 0
      .699999988079071 0 0
     .4000000059604645 1 1
      .800000011920929 0 0
     .8999999761581421 0 0
      .800000011920929 0 0
     .4000000059604645 1 1
    .30000001192092896 1 1
     .8999999761581421 0 0
                     1 0 0
      .800000011920929 0 0
     .8999999761581421 0 0
     .6000000238418579 0 0
      .800000011920929 0 0
    .10000000149011612 1 1
      .699999988079071 0 0
      .699999988079071 0 0
     .8999999761581421 0 0
                     1 0 0
                     . . .
     .8999999761581421 0 0
     .4000000059604645 1 1
      .800000011920929 0 0
     .8999999761581421 0 0
      .800000011920929 0 0
     .4000000059604645 1 1
                     . . .
      .800000011920929 0 0
                     . . .
     .8999999761581421 0 0
      .800000011920929 0 0
      .800000011920929 0 0
    .10000000149011612 1 1
                     . . .
    .10000000149011612 1 1
                     . . .
     .8999999761581421 0 0
     .8999999761581421 0 0
      .699999988079071 0 0
    .30000001192092896 1 1
     .8999999761581421 0 0
     .8999999761581421 0 0
      .800000011920929 0 0
    .30000001192092896 1 1
      .800000011920929 0 0
                     1 0 0
      .800000011920929 0 0
     .4000000059604645 1 1
      .800000011920929 0 0
     .8999999761581421 0 0
    .10000000149011612 1 1
     .4000000059604645 1 1
      .699999988079071 0 0
                     1 0 0
                     . . .
                     . . .
      .699999988079071 0 0
      .800000011920929 0 0
                     . . .
      .800000011920929 0 0
     .8999999761581421 0 0
     .8999999761581421 0 0
     .8999999761581421 0 0
      .800000011920929 0 0
     .8999999761581421 0 0
     .8999999761581421 0 0
    .30000001192092896 1 1
     .8999999761581421 0 0
      .800000011920929 0 0
      .800000011920929 0 0
      .800000011920929 0 0
     .4000000059604645 1 1
     .8999999761581421 0 0
                     . . .
     .8999999761581421 0 0
      .800000011920929 0 0
      .800000011920929 0 0
      .800000011920929 0 0
    end
    label values RecaptchaFails FraudScoreFails
    label def FraudScoreFails 0 "Valid", modify
    label def FraudScoreFails 1 "Invalid", modify
    Using
    Code:
    graph bar (count) Q_RecaptchaScore, over(RecaptchaFails, label)
    I get a graph that looks like this:

    Click image for larger version

Name:	g1.png
Views:	1
Size:	56.4 KB
ID:	1688787
    Then using this
    Code:
    graph bar (count), over(Q_RecaptchaScore, label)
    I get a graph that looks like this:
    Click image for larger version

Name:	g2.png
Views:	1
Size:	46.6 KB
ID:	1688788

    I've tried using
    Code:
     graph bar (count), over(Q_RecaptchaScore, gap(*0))  over(RecaptchaFails, gap(*0))  stack
    but that leaves the empty values on the valid and invalid sides like this:
    Click image for larger version

Name:	g3.png
Views:	1
Size:	51.2 KB
ID:	1688789
    I've also tried
    Code:
     graph bar (count), over(recaptchafails, gap(*0))  over(Q_RecaptchaScore, gap(*0))  over(RecaptchaFails, gap(*0))
    //and
    graph bar (count), over(Q_RecaptchaScore, gap(*0))  over(RecaptchaFails, gap(*0))  over(recaptchafails, gap(*0))
    //and
    graph bar (count), over(Q_RecaptchaScore)  over(recaptchafails, gap(*0))  by(RecaptchaFails) asyvars
    but the results are similarly ineffective.

  • #2
    I don't follow exactly what you want, but what seems clear to me is that graph bar is quite wrong for your second graph. graph bar has ideas of order but its axis is categorical, not measured.

    It seems that the values that occur are all multiples of 0.1. That being so, I would start with something like

    Code:
    histogram Q_RecaptchaScore, discrete barw(0.08) by(RecaptchaFails) freq fcolor(blue*0.3) lcolor(blue)

    Comment


    • #3
      That's closer, but I'm looking for something more like this where RecaptchaFails isn't split into two different graphs and the two bar graphs are superimposed on each other.
      Click image for larger version

Name:	IMG_1593.jpeg
Views:	1
Size:	876.3 KB
ID:	1688801

      Comment


      • #4
        You can make progress with superimposed histograms.

        Code:
        twoway histogram Q if R == 1, discrete barw(0.08) frequency fcolor(red*0.3) lcolor(red) || histogram Q if R == 0, discrete barw(0.08) frequency fcolor(blue*0.3) lcolor(blue) legend(order(1 "Fail" 2 "Pass")) scheme(s1color)


        from which you can see that to get what you want you need to map your indicator to a new variable, 1 to 0.25, 0 to 0.75, and draw bars in the background with width 0.5 or so. Unless you are being told to do what you sketch, I suspect that readers will find something like the design above easier to follow.

        The canonical principle here is that bar area represents frequency. To be compatible, total and component bars drawn with different bar widths would need to be on consistent scales. Again, the design just above is automatically consistent.
        Last edited by Nick Cox; 10 Nov 2022, 01:47.

        Comment


        • #5
          Thank you! That's what I was looking for

          Comment

          Working...
          X