Announcement

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

  • Editing individual markers in graph dot

    Hello list readers.

    I'm constructing a dot chart in Stata using the graph dot command. I'd like to be able to edit individual markers, so as to change their size, colour, as appropriate. However if I change one, all change. The following example is taken from the auto data often used for illustration purposes.

    sysuse auto, clear
    graph dot (count), over(rep78) over(foreign)

    Is it possible to edit individual markers, or will I have to construct the graph differently? Thank you for any advice you can offer.


  • #2
    What you're plotting is (implicitly) the values of one variable, so if one dot is changed, all are. If you plot two or more variables, their colours will differ.

    Code:
    . bysort foreign rep78: gen count = _N
    
    . separate count, by(foreign) veryshortlabel
    
                  storage   display    value
    variable name   type    format     label      variable label
    -------------------------------------------------------------------------------
    count0          byte    %9.0g                 Domestic
    count1          byte    %9.0g                 Foreign
    
    . graph dot (mean) count0 count1, over(rep78) over(foreign) legend(off)
    See also https://www.stata-journal.com/sjpdf....iclenum=gr0049

    Comment


    • #3
      Thanks very much for the advice and reference, that all makes sense and is very helpful.
      On a follow-up point, I have been trying to ensure that all dots/bars are equally spaced within classification categories, irrespective of use of the over() and by() options. This seems to work fine with over(), but when I introduce by() all by groups are given the same depth on the graph, so that by groups with more entries are "squashed" together more than those with fewer entries. For example:

      Code:
      sysuse auto, clear
      graph dot (count), over(headroom,label(labsize(small)) )  over(rep78,label(labsize(small)))  nofill  ysize(11) xsize(8)
      Here all the lines are equally spaced, whereas when I try:

      Code:
      sysuse auto, clear
      graph dot (count), over(headroom,label(labsize(small)) )  over(rep78,label(labsize(small))) ///
      by(foreign, col(1)     note("") imargin(0)) nofill  ysize(11) xsize(8) subtitle(, ring(9) pos(9) nobexpand size(small))
      the 19 lines for Domestic take up the same space as the 10 lines for foreign.
      Ideally I'd like to produce something akin to the second plot, where all markers are individually editable, so maybe graph dot may not be the best option for me. Thanks again to any informed readers who can share their experiences.

      Comment


      • #4
        In #3 you want -- yet do not want -- nofill. I don't see an easy way out of that.

        Comment


        • #5
          If you can live with labels that combine categories, you can use egen's group function to bypass using the -by()- option.

          Code:
          sysuse auto, clear
          egen group = group (foreign rep78), label
          set scheme s1color
          graph dot (count), over(headroom,label(labsize(small)) ) over(group) nofill  ysize(11) ///
          xsize(8) subtitle(, ring(9) pos(9) nobexpand size(small))
          In future, please upload the graphs that your codes generate, in .png format, as I do below. This will better illustrate your issue without having to run the codes.

          Click image for larger version

Name:	Graph.png
Views:	1
Size:	97.3 KB
ID:	1471688

          Comment


          • #6
            Thanks for coming back to me Nick. Instead of having 50% of the plot (with 19 rows) for domestic, and 50% of the plot (with 10 rows) for foreign, I'd like to have one plot with all 29 rows relatively equally spaced out, so that the relative sizes of the subgraphs vary accordingly. I did look at a traditional scatterplot, but would need more than one set of labels for the appropriate axis, stacked alongside/under each other.

            Comment


            • #7

              Thanks to everyone who read for their advice. If it came to it, extra labels could be added by text if so desired.

              On a followup, I'm wondering if it is possible to code the attributes of the markers using additional variables/local macros, which are then called from the graph dot command? In the example below, I manually specify that the first group (domestic) should have orange markers and the second (foreign) purple. I then create a variable (colour) which holds this information, and would like to use the contents of this variable to specify the colour of each marker. This would be useful for when there are lots of points on the graph. However to date I haven't worked how to code this so that stata inserts the actual colour rather than the variable name. If anyone can advise that would again be very much appreciated. Thanks again.

              Code:
              sysuse auto, clear
              
              bysort foreign rep78: gen count = _N
              
              separate count, by(foreign) veryshortlabel
              
              collapse (mean) count0 count1, by(rep78 foreign)
              graph dot (mean) count0 count1, over(rep78) over(foreign) legend(off) marker(1, mcolor(orange)) marker(2, mcolor(purple))


              Code:
               
              gen colour="orange" if count0!=.
              replace colour="purple" if count1!=.
              tab colour
              
              /*
                   colour |      Freq.     Percent        Cum.
              ------------+-----------------------------------
                   orange |          6       60.00       60.00
                   purple |          4       40.00      100.00
              ------------+-----------------------------------
                    Total |         10      100.00
              
              */
              
              local colour "colour"
              tab `colour'
              
              graph dot (mean) count0 count1, over(rep78) over(foreign) legend(off) marker(1, mcolor(`colour')) marker(2, mcolor(`colour'))
                  /*(note:  named style colour not found in class color, default attributes used)
                  (note:  named style colour not found in class color, default attributes used)
                  (note:  named style colour not found in class color, default attributes used)
                  (note:  named style colour not found in class color, default attributes used)
                  */
              
              gen colour_1="orange" if colour=="orange"
              gen colour_2="purple" if colour=="purple"
              
              local colour_1 "colour_1"
              tab `colour_1'
              local colour_2 "colour_2"
              tab `colour_2'
              
              graph dot (mean) count0 count1, over(rep78) over(foreign) legend(off) marker(1, mcolor(`colour_1')) marker(2, mcolor(`colour_2'))
                  /*(note:  named style colour_1 not found in class color, default attributes used)
                  (note:  named style colour_1 not found in class color, default attributes used)
                  (note:  named style colour_2 not found in class color, default attributes used)
                  (note:  named style colour_2 not found in class color, default attributes used)
                  */
              Attached Files

              Comment

              Working...
              X