Announcement

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

  • Mergining multiple overlapping histograms with same x and y axis

    Hello,

    I overlayed two categorical variables and created an overlapping histogram. I have 6 categorical variables and created 3 overlapping histograms that have the same x-axis and y-axis. How do I merge them into one histogram with the same x-axis and y-axis? grc1leg package is not working. I use STATA 17 btw.

    Please let me know.

    Thanks,
    Ty

  • #2
    From your post, I don't understand whether you are creating a single graph with multiple panels or creating a single graph with multiple overlaid histograms. Your mention of -grc1leg- suggests multiple panels. Depending on why -grc1leg- did not work for you, my enhanced version of -grc1leg-, -grc1leg2- might solve your problem. -grc1leg2- has a detailed help file comparing alternative approaches to creating a multi-panel graph with a single legend. When all the panels of a multi-panel graph have the same x- and y-axes, as in your case, -grc1leg2- options facilitate moving those axis titles from the individual panels to appear across all panels on the composite graph. Here is an example from the help file in which only the x-axis is shared across panels and thus is eligible to be moved to the lower margin of the combined graph.

    Click image for larger version

Name:	grc3.png
Views:	1
Size:	83.3 KB
ID:	1661423

    The program is available here:
    Code:
    view net describe grc1leg2, from("http://digital.cgdev.org/doc/stata/MO/Misc")
    If instead your objective is to overlay two histograms on a single axis, you might be interested in -hist_overlay-:
    Code:
    view net describe hist_overlay, from("http://digital.cgdev.org/doc/stata/MO/Misc")
    If you have Stata version 15, -hist_overlay- exploits its translucent color options to produce a graph like this example from the help file:
    Code:
    sysuse auto
    
     hist_overlay mpg, frequency over(foreign) verbose
    which produces the following two overlaid histograms within a single panel: (Overlaying pink on light blue displays as purple, allowing the viewer to distinguish the frequencies of domestic and foreign cars within a given bin.).

    Click image for larger version

Name:	hist2.png
Views:	1
Size:	40.2 KB
ID:	1661424

    If your 6 categories could be meaningfully grouped into pairs, you could make three such overlaid histograms and then combine them with -grc1leg2- to have a common legend and axes.

    (The strategy employed by -hist_overlay- could in principle be extended to three categories, with yellow as the third color. But I did not attempt that generalization, believing that overlaying three colors would produce an ugly result.)

    Other approaches that might be relevant for you are discussed in Nick Cox 's various Stata tips and programs on graphing. In the help file to -grc1leg2-, I reference this one:
    Cox, Nicholas J. (2020) "Stata tip 139: The by() option of graph can work better than graph combine", The Stata Journal, Vol 20, Number 4, pp. 1016–1027.
    https://journals.sagepub.com/doi/pdf...36867X20976341

    For other approaches and ideas, see also Nick Cox 's following programs on SSC

    Code:
    search tabplot
    search fabplot
    search strippolot
    search floatplot
    Last edited by Mead Over; 25 Apr 2022, 10:28.

    Comment

    Working...
    X