Announcement

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

  • Visualizing data using bar chart?

    Hello! I want to present some data using a bar chart. The statistics of interest is the mean of a binary variable (Mort30d), and I want to present it over a grouping variable (EDLOSGroup).
    The correct statistics are obtained using the following command:
    graph bar (mean) Mort30d, over(EDLOSGroup)
    However, I would like each individual bar to include information about another grouping variable (TriageLevel) using different colors. I tried to search this forum, Youtube and I even spent over an hour with ChatGPT trying to solve this problem. The end result should look something like this:
    Click image for larger version

Name:	bar graph.png
Views:	1
Size:	12.0 KB
ID:	1731639


    On the y axis we have percent of 30-day mortality. On the x axis the groups (EDLOSGroup) and the colors represent the 6 possible values for TriageLevel.
    The above graph was done in Excel by manually calculating the proportions of the different segments of each bar. All my attempts to achieve this in Stata has either resulted in separate bars for the secondary grouping variable - or incorrect results, like the one below:
    graph bar (mean) Mort30d, over(TriageLevel) over(EDLOSGroup)stack asyvars
    Click image for larger version

Name:	bar graph2.png
Views:	1
Size:	18.9 KB
ID:	1731640


    The problem is that I want the bar height to be based only on the mean Mort30d. The colors of the bars should represent the distribution of values in TriageLevel but only for that small part of the dataset that the bar is representing. I hope that the images makes it somewhat clear what I'm trying to accomplish.
    Last edited by LarsJonas Andersson; 26 Oct 2023, 08:02.

  • #2
    I even spent over an hour with ChatGPT trying to solve this problem.

    That must be something! I would recommend spending a few minutes reading FAQ Advice #12 which recommends presenting a data example using the dataex command.


    The above graph was done in Excel by manually calculating the proportions of the different segments of each bar. All my attempts to achieve this in Stata has either resulted in separate bars for the secondary grouping variable - or incorrect results, like the one below:
    graph bar (mean) Mort30d, over(TriageLevel) over(EDLOSGroup)stack asyvars

    General advice would be to rescale the third variable to correspond to values of the other variable and then just plot the rescaled variable. A stacked design may not be the best choice here, but you need a reproducible example to increase your chances of getting other suggestions.

    Comment


    • #3
      I agree with Andrew Musau. A broader comment is that this looks like a goal of mushing two graphs together, a plot of means and a plot summarizing a two-way table of categories. I suggest that it would be simpler and more effective to present them side by side.

      Comment

      Working...
      X