Announcement

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

  • Clustered Bar Chart Question from a Novice

    I'm trying to create a clustered bar chart for my thesis. For the x axis, I want to have my categorical variable (3 bars for school format - in-person, hybrid, and virtual) and a fourth bar that is the overall mean of all respondents.

    This is the code I have so far:
    graph bar (mean) covidconcernscale, over(schooltype_w2)

    The edited graph output screenshot is below.
    Click image for larger version

Name:	image_31335.png
Views:	1
Size:	61.1 KB
ID:	1716360

    This has everything I need except the fourth bar with the overall mean concern level. Is it possible to add that?
    Last edited by Olivia Gruwell; 07 Jun 2023, 10:59.

  • #2
    Sorry, not sure why it posted my screenshot twice. *fixed it*

    Comment


    • #3
      So, looking at the documentation, I can't find a good way to do this using the graph bar interface on its own. There may absolutely be a way to do this that I'm not aware of, and perhaps someone else will jump in with a simple solution.

      In the mean time, my less than ideal (but working) solution is to just calculate every relevant mean manually, then plot those means. Obviously I don't have access to your data and you didn't provide any example data. As an alternative, I load the built-in automobile data set. Now, let's say I want to create a bar graph of "gear_ratio", with means for the two categories of the "foreign" variable (domestic and foreign), and the overall mean for gear ratios. I make new columns representing the mean for each bar, then create a bar graph using those columns.

      Code:
      sysuse auto, clear
      egen domestic_mean = mean(gear_ratio) if foreign == 0
      egen foreign_mean = mean(gear_ratio) if foreign == 1
      egen overall_mean = mean(gear_ratio)
      graph bar (mean) domestic_mean foreign_mean overall_mean
      Hopefully this code will help you get started.
      Last edited by Daniel Schaefer; 07 Jun 2023, 12:56.

      Comment


      • #4
        Here is some technique growing out of a trick documented at https://journals.sagepub.com/doi/pdf...867X1401400117

        Code:
        sysuse auto, clear 
        
        capture frame drop graphsandbox 
        
        frame put mpg foreign, into(graphsandbox)
        
        frame graphsandbox { 
            expand 2, gen(new)
            replace foreign = 2 if new 
            label def foreign 0 Domestic 1 Foreign 2 Either 
            label val foreign foreign 
            graph dot mpg, over(foreign) exclude0  ysc(alt r(19 26)) yla(20/25) ///
            blabel(bar, size(medlarge) format(%2.1f)) linetype(line) lines(lc(gs12) lw(thin))
        } 
        
        
        d foreign
        Click image for larger version

Name:	dotchart5.png
Views:	1
Size:	18.7 KB
ID:	1716374

        Details, mostly optional:

        1. The use of frames makes change to the dataset temporary as the last command shows: foreign is as it was.

        2. For data like yours, the issue is arguably comparing means with each other (6 7 8 or so), not with zero. Given that, dot charts are (I suggest) a better use of space than bar charts with bars all starting at zero. (Dot charts can be vertical too with the undocumented vertical option.)

        3. Even with dot charts, although it is not documented, extra labels are allowed showing values as text and you can tune size and number of decimal places.

        4. With dot charts I find that the default grid often does not port well to other software and in any case I like solid lines as a grid so long as they are thin and have a subtle colour.

        5. It may be only personal whim, but if a graph has table flavour, horizontal axis at the top looks good to me. More at https://journals.sagepub.com/doi/pdf...867X1201200314

        Comment

        Working...
        X