Announcement

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

  • Boxplots over two categorical variables - Adjust box gap and colour

    Hi Statalist

    I use Stata 14.1 on MacOS. I'm stuck with a (probably) simple thing.
    A sample of my dataset can be obtained using the following code:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float std_PHYS byte insee float(id_controle class)
      50.6022 0 1 2
     55.27794 1 1 1
     57.39201 1 1 1
    33.684685 1 1 1
      47.8986 1 1 1
     26.17278 1 1 1
     48.88623 1 1 1
     48.38394 1 1 1
     54.13319 1 1 1
     47.31923 1 1 1
    23.920927 1 1 1
     44.27946 1 1 1
     55.44976 1 1 1
     42.15575 1 1 1
     51.50533 1 1 1
     55.01159 1 1 1
     55.18439 0 2 4
    65.090256 1 2 3
     56.41214 1 2 3
     23.55465 1 3 5
    end

    I want to graph (using graph box) a continuous variable according to two categorical variables. My final goal is to get a graph box like this: large box gap between categories and adjusting colour across the second category.
    Click image for larger version

Name:	essai_boxgap_couleurOK.jpg
Views:	1
Size:	60.6 KB
ID:	1358401



    But, when I use two over() categories, I get gaps as I want but I cannot figure out how to personalize colours of boxes 2,4 and 6.
    Code:
    graph box std_PHYS, over(insee) over(id_controle)  asyvar
    Click image for larger version

Name:	essai_boxgapOK.jpg
Views:	1
Size:	65.4 KB
ID:	1358402



    And when I use one combining variable as follows
    Code:
    graph box std_PHYS, over(class)  asyvar box(1, color(red)) box(2, color(navy)) box(3, color(red)) box(5, color(red)) box(4, color(blue)) box(6, color(green))
    , I'm not able to adjust two different gaps:

    Click image for larger version

Name:	essai_couleurOK.png
Views:	1
Size:	46.7 KB
ID:	1358403


    As shown in the first graph (done with Graph Editor), I guess it's possible using code but I did not figure out how to do that...

    Any idea?
    Thank you very much

  • #2
    So you have the composite variable "class" and all you appear to want is a plot over the variable "id_controle". The first graph should guide you on what to do here. Just include the option -nofill- to omit empty categories that inevitably exist in your desired graph.


    Code:
    graph box std_PHYS, over(class) over(id_controle) asyvar nofill box(1, color(red)) box(2, color(navy)) box(3, color(red)) box(5, color(red)) box(4, color(blue)) box(6, color(green))

    Comment


    • #3
      Thank you very much Andrew
      it works perfectly
      Best

      Comment

      Working...
      X