Announcement

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

  • Graph bar - Centering one graph when using -by()-

    Hi everyone,

    I'm making a graph for a variable with three categories that resembles the following:

    Code:
    sysuse auto, clear
    
    scatter mpg weight, by(foreign, total)
    Click image for larger version

Name:	3 graphs_by.png
Views:	1
Size:	142.7 KB
ID:	1497624


    I'd like to place the graph of the third category in the center of the other two graphs, so I can have something like this:

    Click image for larger version

Name:	3 graphs_combine.png
Views:	1
Size:	166.6 KB
ID:	1497625


    The last graph results from this code,

    Code:
    sysuse auto, clear
    
    scatter mpg price, name(gr2)
    scatter weight price, name(gr3)
    graph combine gr2 gr3, iscale(1) graphr(margin(zero)) name(gr2and3)
    
    scatter length price, fxsize(60) name(gr1)
    graph combine gr1 gr2and3, col(1)
    This code was posted by David Harrison back in February 2005 in the former Statalist, https://www.stata.com/statalist/arch.../msg00557.html

    As you can see, this is a combination of three graphs, each one with different variables (mpg, weight, and length) against price.

    My question: How can I get something similar when ploting one variable with three categories?

    Thank you.
    Last edited by Emanuel Agu; 09 May 2019, 15:22.

  • #2
    One possibility would be to produce three separate graphs using the if clause to restrict each to the different values of your categorical variable. Then combine ..., row(1) the first and third, then combine ..., col(1) the second with the combination of the first and third. You're going to have to do something to get the horizontal and vertical scales to be the same.

    I'm not the expert with the graph commands and it would take me more time than I have to spare to work out the details. Perhaps someone more facile than I either (a) knows an alternative or (b) can more easily flesh out my ideas with working code.

    Comment


    • #3
      Originally posted by William Lisowski View Post
      One possibility would be to produce three separate graphs using the if clause to restrict each to the different values of your categorical variable. Then combine ..., row(1) the first and third, then combine ..., col(1) the second with the combination of the first and third. You're going to have to do something to get the horizontal and vertical scales to be the same.

      I'm not the expert with the graph commands and it would take me more time than I have to spare to work out the details. Perhaps someone more facile than I either (a) knows an alternative or (b) can more easily flesh out my ideas with working code.
      Thank you William! That's a good idea.

      Comment

      Working...
      X