Announcement

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

  • Bar Graph with multiples variables and categories

    Hi, everyone!
    I need to perform a bar graph with 2 variables containing 4 categories each.
    The 2 variables are treat1 and treat2.
    The categories were labeled as: 1 == "lowdose" 2 == good_treat 3 == "overdose" 4 == nd.
    I want a vertical bar graph demonstrating the percentage of treat1 and treat2 for each category (lowdose, good_treat, overdose and nd).
    That is, the 4 categories on x axis and each category with 2 bars (treat1 and treat2 percentages).

    I'm really sorry! I've searched it on previous posts about the same issue (like "how to graph bar of categorical variables", "A simple graph bar" and "(possibly) simple bar graph comparing two variables (not one variable over the other)", but I didn't make it as I wish using those informations.

    That is an example of my data:
    list treat1 treat2

    +---------------------+
    | treat1 treat2 |
    |---------------------|
    1. | 1 2 |
    2. | 2 4 |
    3. | 2 2 |
    4. | 2 2 |
    5. | 4 2 |
    |---------------------|
    6. | 4 1 |
    7. | 4 3 |
    8. | 1 2 |
    9. | 1 2 |
    10. | 1 2 |
    |---------------------|
    11. | 4 1 |
    12. | 3 4 |
    13. | 2 4 |
    14. | 4 2 |
    15. | 4 4 |
    |---------------------|
    16. | 2 2 |
    17. | 2 4 |
    18. | 4 2 |
    19. | 2 2 |
    20. | 2 2 |
    |---------------------|
    21. | 2 1 |
    22. | 4 2 |
    23. | 4 3 |
    24. | 2 1 |
    25. | 1 2 |
    |---------------------|
    26. | 3 1 |
    27. | 2 2 |
    28. | 2 2 |
    29. | 1 2 |
    30. | 1 2 |
    |---------------------|
    31. | 2 2 |
    32. | 2 1 |
    33. | 3 4 |
    34. | 2 3 |
    35. | 1 2 |
    |---------------------|
    36. | 2 2 |
    37. | 2 2 |
    38. | 1 1 |
    39. | 4 2 |
    40. | 2 2 |
    |---------------------|
    41. | 2 4 |
    42. | 4 2 |
    43. | 4 1 |
    44. | 2 3 |
    45. | 2 2 |
    |---------------------|
    46. | 4 3 |
    47. | 2 2 |
    48. | 2 2 |
    49. | 3 4 |
    50. | 2 1 |
    |---------------------|

    label values treat1 origin
    label def origin 1 "lowdose", modify
    label def origin 2 "good_treat", modify
    label def origin 3 "overdose", modify
    label def origin 4 "nd", modify

    label values treat2 origin
    label def origin 1 "lowdose", modify
    label def origin 2 "good_treat", modify
    label def origin 3 "overdose", modify
    label def origin 4 "nd", modify

    I thank in advance for the help!

  • #2
    Have you looked at https://www.stata.com/support/faqs/d...ary-variables/ ?
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thanks for your data example, which I have cleaned up here. Please note the use of dataex (FAQ Advice #12). You can run this script directly (copy and place into a do-file editor window), but you need first to install the files for two commands.


      Code:
      ssc install catplot
      ssc install tabplot


      catplot is just a wrapper for graph hbar, but I understand its way of showing percent reductions better.

      tabplot gives you something you don't ask for, but is surely crucial too, the joint distribution of the variables.

      I've subverted your request for vertical bar charts, partly, as if you make the value labels civilised, you run out of space very quickly with vertical bars.


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(treat1 treat2)
      1 2
      2 4
      2 2
      2 2
      4 2
      4 1
      4 3
      1 2
      1 2
      1 2
      4 1
      3 4
      2 4
      4 2
      4 4
      2 2
      2 4
      4 2
      2 2
      2 2
      2 1
      4 2
      4 3
      2 1
      1 2
      3 1
      2 2
      2 2
      1 2
      1 2
      2 2
      2 1
      3 4
      2 3
      1 2
      2 2
      2 2
      1 1
      4 2
      2 2
      2 4
      4 2
      4 1
      2 3
      2 2
      4 3
      2 2
      2 2
      3 4
      2 1
      end
      label values treat1 origin
      label values treat2 origin
      label def origin 1 "low dose", modify
      label def origin 2 "good treatment", modify
      label def origin 3 "overdose", modify
      label def origin 4 "nd", modify
      scheme s1color
      
      tabplot treat1 treat2 , percent showval name(G0, replace)
      
      catplot treat1, name(G1, replace) percent
      catplot treat2, name(G2, replace) percent
      graph combine G1 G2, name(G1_2, replace)
      
      gen id = _n
      reshape long treat, i(id) j(treatment)
      label val treat origin
      
      catplot treat treatment, name(G3, replace) percent(treatment)
      catplot treatment treat, asyvars name(G4, replace) percent(treatment)


      Last edited by Nick Cox; 20 May 2020, 01:38.

      Comment


      • #4
        Hi, Nick!

        I really thank you!
        Your help was very important to me!

        This is the output, in case other people are also interested in this type of graph.

        Attached Files
        Last edited by Zilda Braid; 20 May 2020, 10:51.

        Comment

        Working...
        X