Announcement

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

  • Graph Bar with two variables with identical categories

    Hello,

    I have two variables (var1, var2) with three identical categories (suppose, oppose, neither).
    I would like to create a bar graph showing the frequencies of each category for both of the variables.

    The x-axis should show the three categories.
    Two bars should be shown in each segment of the x-axis (representing the categories) reporting the frequency of individuals in this category for each of the two variables, resulting in 6 bars.
    It should look something like this (but this dataset has a different data structure):


    I searched for a solution, but ado´s in the ssc (like catplot or tabplot by Nick Cox) do not help.
    I appreciate any advice.

  • #2
    http://www.statalist.org/forums/foru...in-a-bar-chart

    Comment


    • #3
      Here's another way to do it. You need to install designplot (SJ) first.

      You can do that easily by clicking on the first given download location (at the time of writing gr0061_1) after

      Code:
      search designplot

      I am supposing that there is a natural order support, neither, oppose (or words or phrases of similar meaning in a language other than English):

      Code:
      clear
      set obs 100
      set seed 2803
      forval j = 1/3 {
          gen rnd`j' = runiform()
      }
          
      gen var1 = cond(rnd1 < .1, 1, cond(rnd1 < 0.2, 2, 3))  
      gen var2 = cond(rnd2 < .4, 1, cond(rnd1 < 0.5, 2, 3))  
      gen var3 = cond(rnd3 < .7, 1, cond(rnd1 < 0.8, 2, 3))  
      
      label define mylabel 1 support 2 neither 3 oppose
      label val var* mylabel
      
      gen data = 1
      label var data "interesting data?"
      designplot data var?, stat(count) min(1) max(1) variablelabels
      designplot data var?, stat(count) min(1) max(1) variablelabels recast(hbar)
      Click image for larger version

Name:	designplot2.png
Views:	1
Size:	12.1 KB
ID:	1357280



      Note the small trick of defining a constant outcome or response variable but never showing it explicitly.

      designplot was written up in http://www.stata-journal.com/article...article=gr0061 Access requires subscription or one-off payment of USD 11.75 until Q4 2017.

      Alternatively, http://www.statalist.org/forums/foru...riptive-tables is easily accessible to get the flavour.

      I deliberately don't give the vertical bar chart here. Experiment with

      Code:
      designplot data var?, stat(count) min(1) max(1) variablelabels recast(bar)
      shows that even with short value labels (here at most 7 characters!) there are problems of text overlapping.

      Comment


      • #4
        Thank you very much, Nick. Yes, I am trying to create a horizontal plot but haven´t said so to keep it simple.
        So, thanks a lot! I will look into it!

        Comment


        • #5
          Is it not exactly what I was looking for. I hoped that the frequency for support for TTIP (var1) would be below the bar for support for Free Trade (var2). Below these bars would be two bars for the frequency of those in the neither category of TTIP and FreeTrade and then two bars for those who oppose TTIP and those who oppose Free Trade.

          Anyway, this is already very helpful and good enough. Thank you very much.
          I played around with it a little more, following your article in the Stata Journal.
          The graphics scheme is plotplainblind by Bischof (https://danbischof.com/2016/01/16/up...igure-schemes/)
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	56.4 KB
ID:	1357289

          Last edited by Alexander Wuttke; 20 Sep 2016, 05:55. Reason: added URL to scheme

          Comment


          • #6
            This is the code I have used
            Code:
            set scheme plotplainblind
            gen data = 1 
            designplot     data freetrade_tri att_ttip_1_tri , stat(count) min(1) max(1) variablelabels recast(hbar) ///
                        blabel(total, format(%2.0f) size(vsmall)) ///adding row-wise count-numbers
                        ylabel(, nolabels noticks) yscale(alt) ///removing axis labels
                        t1title("") title("")

            Comment


            • #7
              Dear Stata forum members,

              I am looking to create a bar graph to present values from two variables (e.g. fat intake on day 1 and fat intake on day 2) for each of the IDs (100 observations) in my study. I want to show the variation in fat intake on day 1 and 2 for each individual as well as the variation between individuals.

              I have created a graph with this code that is close to what I am looking for: graph bar (asis) fat_1 (asis) fat_2, over (ID) stack xalternate yscale(range(0 180)) yline (20, lwidth (thick) lcolor (green) lpattern (solid))

              the y line shows the average fat intake for all of the IDs.


              I was hoping to also be able to show the following:
              1) mean of fat_1 and fat_2 for each bar (i.e. each ID) by a solid circle symbol on each bar
              2) to show the bars in a descending order based on the average of each bar

              Also would really appreciate your input on whether you think the stack bar is the best option for showing the variability in fat intake for each ID?


              As my data is confidential I created a fake one:

              fat_1 fat_2 ID
              5.13 24.52 "t22"
              11.79 22.16 "y33"
              14.56 15.18 "l44"
              16.29 25.18 "m55"
              15.48 26.08 "h58"
              16.21 6.67 "t55"
              14.54 16.55 "n22"
              17.58 52.83 "t66"
              16.34 25.83 "r77"
              19.64 16.13 "z99"


              Thanks very much for considering my question. This the closest topic that I found to my question, please let me know if I should have posted this as a new topic.

              All the best,
              Aysha

              Comment

              Working...
              X