Announcement

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

  • Dotplot grouped by third variable

    Hello Statalisters! I am trying to make a dotplot of a continuous variable over values of a string variable. That part is easy enough, using the "over()" statement. However, I would like to see these all grouped by the levels of a third variable. I don't necessarily want separate plots, though I will settle for that if necessary, but rather to have a small break in the x-axis that distinguishes each group (similar in look to a coefplot after mlogit, but vertical and without the error bars like the plot below)

    Click image for larger version

Name:	Screen Shot 2021-03-16 at 11.30.11 AM.png
Views:	1
Size:	19.7 KB
ID:	1598111

    Using the automobile data as an example, imagine I would like something like a dotplot of mpg over make of automobile (see code below), but with all the foreign cars on the left side of the plot, with a label of "Foreign" under them, and all the domestic cars on the right side of the plot with the word "Domestic" under them. In my actual data, I have 6-10 groups and will be repeating this a few times with the groups changing, so, making this somewhat automated will be very helpful.

    Code:
    sysuse auto, clear
    dotplot mpg, over(make) xlab(,angle(90))
    Last edited by amandacpac; 16 Mar 2021, 11:21.

  • #2
    A dot plot is just a special kind of scatter plot. With this in mind, you can use the more flexible twoway command for this. For your example, I reduce the number of categories (makes of cars) and I place the categories on the y-axis to enhance readability. Additionally, I use labmask from the Stata Journal, authored by Nick Cox.

    Code:
    sysuse auto, clear
    drop in 20/60
    dotplot mpg, over(make) xlab(,angle(90))
    gen Make=cond(foreign==0, _n*5, 10+(_n*5))
    labmask Make, values(make)
    tw (scatter Make mpg if !foreign, ylab(5(5)95 110(5)175, val angle(horiz) ///
    labsize(vsmall))) (scatter Make mpg if foreign, scheme(s2color) ///
    ytitle("Domestic                                Foreign") leg(off))
    Res.:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	33.9 KB
ID:	1598120


    Comment


    • #3
      This is great, but, I have two datasets, one with 6 groups and one with 10 groups. The groupings change based on other analyses, so, I would like to be able to run with new groupings. Most of the code will work ok for that, except the labeling of "Domestic" and "Foreign". Is there a way to automate that to automatically pull in the values of that variable?

      Comment


      • #4
        As a variant on Andrew Musau's example consider

        Code:
        sysuse auto, clear
        drop in 20/60
        
        graph dot (asis) mpg, over(make, sort(1) label(labsize(small))) over(foreign) nofill exclude0 ysc(r(12, .) alt)
        Click image for larger version

Name:	notandrew.png
Views:	1
Size:	92.5 KB
ID:	1598131


        With graph dot I tend to add linetype(line) lines(lc(gs12) lw(vthin)) for various reasons, one being that I've found that the dotted grid doesn't transport well to other software.

        Comment


        • #5
          Most of the code will work ok for that, except the labeling of "Domestic" and "Foreign". Is there a way to automate that to automatically pull in the values of that variable?
          These are just axis titles and you will have to place them manually. However, see

          Code:
          help title_options

          This will allow you to choose where to place the group labels and other options, turning off the defaults.

          Comment


          • #6
            Nick Cox This is perfect! However, is there a way to present this horizontally? So, that the make of car runs along the x-axis and the mpg along the y-axis? I have looked through the documentation and don't see an option for it, but, maybe I'm missing something. If not, I can leave it like this. Thank you!

            Comment


            • #7
              graph dot has an undocumented vertical option which usually makes displays much uglier. That's my guess at why it is undocumented.

              Comment

              Working...
              X