Announcement

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

  • How to create a bar graph over two categorical variables with two separate y axes

    I have the following simple working example using a dataset made available by Stata:

    Code:
    webuse hbp2, clear
    graph bar city age_grp, over(year) over(sex) blabel(bar, format(%4.1f)) intensity(75)
    I have attached the resulting graph as a .png file.

    What I want to do is create a separate axis for city and age_grp. After consulting a different topic posed on this site, I still could not figure it out. I do not wish to treat -year- as a time variable as the analysis I am doing involves two categorical variables like -sex- and -race-. The data that I am actually using in my analysis is much too different to be compared on the same axis.

    Let me know if I need to add or clarify anything to this post.
    Attached Files

  • #2
    Cross-posted at https://stackoverflow.com/questions/...th-two-separat

    Please note our policy about cross-posting, which is that you should tell us about it.

    That said, graph bar is a dead end for what you want. Separate y axes are available only with graph twoway.

    Comment


    • #3
      I will look into and come back to this post and present my solution (if I figure it out...)

      Comment


      • #4
        So -- to abstract slightly -- you want to show means of outcomes y1 and y2 by categorical variables x1 and x2, but y1 and y2 have very different magnitudes or very different units of measurement, making you prefer separate scales for each.

        If they have different units of measurement, that could be tackled by a scatter plot of y1 and y2 with categories distinguished.

        If the problem is very different magnitudes I would wonder about a transformed scale, say logarithmic, and even about a different kind of mean, say geometric mean.

        I much appreciate your giving a reproducible example in #1 but what makes most sense or would work best for you does hinge also on the concrete details of your real data.

        Comment


        • #5
          Hi Nick,

          I have both different units of measurements as well as magnitudes. The actual data I am working with are crop yields measured in g/plant and ton/ha which are wildly different in many cases. For one of my crops, maize, I believe, I get an average yield of 40 g/plant and 3 ton/ha. I am inclined to try transforming the scale but I worry that it may be misleading as I am trying not to compare the two units of measure, rather the average yields in one unit of measure with other crops in that same unit of measure. I guess I could just do two separate plots to make my life easier but I figured it was worth inquiring into. What do you think?

          Comment


          • #6
            If you standardized to (value MINUS mean) / SD or log value MINUS log geometric mean, or whatever, then graph bar (or better graph dot) is available. If you want to show ton/ha and g/plant on two y axes you need graph twoway.

            This sentence I couldn't quite follow; otherwise I think your general implication is very clear.

            I am inclined to try transforming the scale but I worry that it may be misleading as I am trying not to compare the two units of measure, rather the average yields in one unit of measure with other crops in that same unit of measure.

            Comment


            • #7
              Hi Nick,

              Sorry for Cross-posting here from https://www.statalist.org/forums/forum/general-stata-discussion/general/1551350-how-to-remove-a-country-from-bar-graph.


              I just wanted to check is there anyway to remove a state from an existing graph without really affecting the bar gap etc and dropping in the dta file? I need to present my results in a few hours time just realized two states have too small a sample and need to correct more than 50 stata graphs. Will appreciate your help.

              Comment

              Working...
              X