Announcement

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

  • Box plot editing

    I have created box plots for three distinct groups and I want to distinguish them from each other. I do not wish to differentiate the groups based on box colors, but rather based on the presence of diagonal or dotted lines within the box plots. I would like to have a black-and-white box plot. Can you assist me with this, please?"

  • #2
    Click image for larger version

Name:	Graphh.jpg
Views:	1
Size:	46.5 KB
ID:	1751204

    This is the image

    Comment


    • #3
      It's hard to do this by editing without minor mess.

      I greatly prefer a direct code solution.

      Here is an example with two groups. The code arises from a mix of principle and experiment.

      Code:
      sysuse auto, clear
      
      foreach f in 0 1 { 
      
      su mpg if foreign == `f', detail 
      
      local upq`f' = r(p75)
      local UPQ`f' : di %2.0f r(p25)
      local med`f' = r(p50)
      local MED`f' : di %2.0f r(p50)
      local loq`f' = r(p25)
      local LOQ`f' : di %2.0f r(p25)
      
      }
      
      graph box mpg, over(foreign) vertical aspect(1) ///
      text(`upq0' 1 "`UPQ0'" ) text(`med0' 1 "`MED0'" ) text(`loq0' 1 "`LOQ0'") /// 
      text(`upq1' 61 "`UPQ1'" ) text(`med1' 61 "`MED1'" ) text(`loq1' 61 "`LOQ1'")

      FWIW, the standard abbreviation for kilograms is kg, not Kg. It's a matter of taste but once the units have been explained on the vertical axis, I wouldn't myself repeat them for every instance.

      Diagonal or dotted lines are sometimes asked for here, but I am not playing on that one. You'd get a much clearer result by direct labelling.

      Post a proper data example using dataex and I will make suggestions for code.

      Comment


      • #4
        This code applies a strategy discussed at https://journals.sagepub.com/doi/pdf...867X1001000112

        Clearly in your case you want everything black, but that is an easy fix.

        The code assumes groups coded by integers 1 up; it is easy to get there with egen, group().

        There is enormous scope for different takes here.

        Direct labelling beats legends here, and usually.

        With short variable names and no labels, vertical alignment would work just as well. Often with longer text you really need to go horizontal.

        As a token demonstration that you can go further in this framework I show how you can add means too.

        But also -- a different story -- with 3 groups you have space for much more detail, such as quantile-box plots.

        Code:
        * fake data 
        clear
        set obs 300
        egen group = seq(), block(100)
        label def group 1 frog 2 toad 3 new
        label def group 1 frog 2 toad 3 newt 
        label val group group 
        set seed 2803 
        gen y = rnormal(group, 1)
        
        preserve 
        
        * moderately general code follows 
        * what you might most obviously need to change: 
        *    outcome variable name -- here y 
        *    group variable name -- here group 
        *    ytitle -- here "whatever"
        *    some numbers if categories not coded by successive integers or # groups is not 3 
        *    display format for median and quartiles 
        
        statsby mean=r(mean) min=r(min) loq=r(p25) med=r(p50) upq=r(p75) max=r(max), by(group) clear: summarize y, detail 
        
        gen LOQ = strofreal(loq, "%3.2f")
        gen MED = strofreal(med, "%3.2f")
        gen UPQ = strofreal(upq, "%3.2f")
        
        gen group2 = group + 0.12
        
        twoway  rspike min loq group, horizontal lc(stc1) ///
            ||  rspike max upq group, horizontal lc(stc1) ///
            ||  rbar med loq group, barw(0.2) horizontal lcolor(stc1) fcolor(none) ///
            ||  rbar med upq group, barw(0.2) horizontal lcolor(stc1) fcolor(none) ///
            ||  scatter group2 loq, ms(none) mlab(LOQ) mlabc(stc2) mlabpos(12) ///
            ||  scatter group2 upq, ms(none) mlab(UPQ) mlabc(stc2) mlabpos(12) ///
            ||  scatter group2 med, ms(none) mlab(MED) mlabc(stc2) mlabpos(12) ///
            ||  scatter group mean, ms(Dh) msize(large) mlabc(black) /// 
                yla(1/3, valuelabel noticks) xtitle("whatever") legend(off)
                
        restore

        Click image for larger version

Name:	3boxes.png
Views:	1
Size:	35.6 KB
ID:	1751238

        Comment


        • #5
          Click image for larger version

Name:	BOX Plot.jpeg
Views:	1
Size:	74.6 KB
ID:	1751241

          Sir, I want to put dots or diagonal lines inside the box plot. How to do this. This is my query

          Comment


          • #6
            Sir, I want to put dots or diagonal lines inside the box plot. How to do this. This is my query. I am not able to edit it as per my desire

            Comment


            • #7
              As said in #3 I don’t support such patterns either in principle or in practice by suggesting code.

              Comment


              • #8
                No problem.Thank You

                Comment

                Working...
                X