Announcement

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

  • Help to combine graph bar and connected into single graph, by group(s)

    Have been trying without luck to combine the two graph types (bar and connected line) to be overlayed. Both are to be displayed by group (organization) and time period (quarter). This requires the option over() used twice- I can successfully create the graphs separately but when I try: tw (bar...) || (connected ..., c(l)), it just fails at every turn. Thanks for any help.

  • #2
    Are you able to specify what fails? May you have a data and code example?

    Comment


    • #3
      Yes, thank you. Here is the code I used:
      Code:
      tw (bar /*(asis)*/ totpos, o(facilityname, label(labsize(tiny) angle(45)) gap(*.2)) o(hy_period, label(labsize(vsmall)) gap(*2)) asyvars bargap(0) legend(row(1) size(tiny) pos(6) ring(0) region(fc(none) lc(none)) bm(small)) yaxis(1)) || (connected posperc hy_period, /*c(l)*/ yaxis(2))
      Error msg. says: too few variables specified ...
      The bar graph is meant to show raw counts of a variable (range: 0-1,000's), and the connected points are the percentages of each of the same numbers as a proportion of the total for that period, both are for 9 6-month intervals between 2018 and 2023 (variable hy_period).
      The bars are grouped by facilityname with a bar for each of 5 facilities (there would be a group displayed for each of the 9 time periods). Then the connected points should be overlaid over the top (or over the bars) of the bar groupings showing percentages for each facility at each of the 9 time points (the point symbols to have different shapes). The legend would show color boxes for the 5 facilities represented in both bars and connected points. The y-axis on the left would show a scale for the counts, while a second y-axis to the right would show a scale for the percents.
      I am able to produce the two graphs separately without a problem.
      Last edited by Straso Jovanovski; 18 May 2023, 10:35.

      Comment


      • #4
        [CODE]
        tw (bar /*(asis)*/ totpos, o(facilityname, label(labsize(tiny) angle(45)) gap(*.2)) o(hy_period, label(labsize(vsmall)) gap(*2)) asyvars bargap(0) legend(row(1) size(tiny) pos(6) ring(0) region(fc(none) lc(none)) bm(small)) yaxis(1)) || (connected

        The code fails because you're confusing graph by combining syntax for graph bar and for twoway bar.

        One way to see why the syntax failed at the first hurdle is to note that
        twoway bar needs two variables.

        I can't test the first part because -- despite requests in #2 and #3 -- there is no data example, but I guess that what worked was


        Code:
        graph bar (asis) totpos, o(facilityname, label(labsize(tiny) angle(45)) gap(*.2)) o(hy_period, label(labsize(vsmall)) gap(*2)) asyvars bargap(0) legend(row(1) size(tiny) pos(6) ring(0) region(fc(none) lc(none)) bm(small)) yaxis(1)
        but you can't just cut
        (asis) and slap twoway in front.

        Although it surprises many users,
        graph bar and twoway bar don't have very much connection beyond sharing some options. There certainly isn't an easy mapping between the commands. graph bar (with the exception of (asis)) is based on a two-step process, a reduction to quantities to be shown and then a graphing of those quantities. twoway bar is based on showing quantities already calculated and therefore existing as variables in the dataset.

        Your starting point should be

        Code:
         
        twoway bar totpos hy_period || connect posperc hy_period
        but even a poor translation of the rest is too hard for me to guess at without seeing a data example. What one
        over() option does for you might be done in various different ways.

        Comment

        Working...
        X