Announcement

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

  • How to force a line in the graph bar command?

    Hello Statalist community,

    I could need a little help regarding the graph bar command. I'm running the following code to create my graph:

    Code:
    gen n = 1
    foreach var of varlist $p {
        local graphtitle: variable label `var'
        graph hbar (count) n, over(`var', relabel(1 SD 2 D 3 N 4 A 5 SA)) ///
        bar(1, fcolor(blue) lcolor(black)) ///
        title("`graphtitle'", size(small) color(black)) ///
        ytitle(" ") ylabel(0(500)2500) ymtick(0(100)2400) ///
        scheme(s1color) nodraw ///
        name(g_`var', replace)
    }
    graph combine g_p1 g_p2 g_p3 g_p4 g_p5 g_p6, /// 
        title("Parents attitudes toward education") ///
    subtitle("SD='Strongly Disagree', D='Disagree', N='Neutral', A='Agree', SA='Strongly Agree'", ///
        size(vsmall) color(black)) ///
        b1title("Number of Respondents") ///
        scheme(s1color) ///
        name(combinedp, replace)
        graph export "${Graphs}/p_combined_w5.png", replace as(png)
        graph export "${Graphs}/p_combined_w5.ps", replace as(ps) mag(160) /// 
            logo(off) orientation(landscape)    
    drop n
    The graph this command creates looks like this: (I hope the scaling works. The graph looks huge in the preview)
    Click image for larger version

Name:	p_combined_w5.png
Views:	1
Size:	200.9 KB
ID:	1463103


    What I need your help for is the following: If you look at the first two graphs, there's no yline at the 2,500 mark. At the third graph there is. Is there any command how I can force this line to be drawn? It drives me nuts that there are minor differences between these six graphs.

    Btw, if you have any suggestions how I can make this graph look even better, please feel free to tell me your ideas. I'm glad to learn!

    Thanks for your help guys, I really appreciate it!

    Cheers
    Jonas

  • #2
    Solved it myself. For those who care, I did it with a single line of extra code
    Code:
    yline(2500, lstyle(background) lcolor(gs13))
    Sorry for the redundant question. I asked too fast and missed this simple line of code...

    Comment


    • #3
      Rather than having to manually draw the line at a specific location, it is possible that
      Code:
      ylabel(0(500)2500, gmax)
      would have taken care of the problem. In the output of help axis label options you'll find an explanation of what's going on - briefly, since your bars didn't get anywhere near the maximum value, graph was doing what it thought was a favor for you.

      Comment


      • #4
        Note that tabplot (Stata Journal) will plot these data without so much repetition. You'll need to restructure the data. Also -- evidently English is not your first language - you need a possessive in the graph title: Parents' or Parent's depending what the questions were.

        Comment


        • #5
          Nick Cox , thanks for the correction and your hint regarding the tabplot command. I'll use it to visualise the relationship between my ordinal response and ordinal predictor variable. When I'm a bit more accustomed to the tabplot command I'll see what I can do in regards to the graph I presented above. Thanks for letting me know!

          Comment


          • #6
            I can explain more but I would need to see a data example. Meanwhile this may give flavour.


            Code:
            clear 
            
            set obs 10 
            set seed 2803 
            
            forval j = 1/6 { 
                 gen y`j' = runiformint(1, 5)
            }
            
            list 
            
            label define grade 1 "Strongly disagree" 
            label val y* grade 
            
            preserve 
            gen long id = _n
            reshape long y, i(id) j(question) 
            
            tabplot y question, showval yreverse 
            
            restore

            Comment


            • #7
              Nick Cox , Thank you for your offer! I'm more than curious to see what else could be done with my dataset. Especially in regards to plotting the relationship between two ordered categorical variables. In the dataset bellow I included the variables used to create the graph above (p1-p6) and an example variable (sorigin) I'd like to use to visualise the relationship between the ordinal variables. I'm looking forward to see what magic you can do with the tabplot command. I'm ready to learn some more.

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input long id byte(p1 p2 p3 p4 p5 p6 sorigin sex)
              4010913 5 5 3 1 1 1 3 2
              4010918 5 3 1 1 1 5 3 1
              4010927 5 4 2 1 1 2 5 2
              4010934 3 2 1 1 1 1 2 2
              4010952 5 5 2 2 3 3 6 2
              4010955 5 5 1 1 1 1 2 1
              4010963 3 2 2 1 1 1 2 1
              4010968 5 3 1 1 1 2 2 1
              4010971 3 3 1 1 3 4 6 2
              4010972 3 4 1 3 3 2 3 2
              4010975 5 4 1 2 2 1 4 1
              4010982 5 5 1 1 5 5 3 2
              4010988 5 5 1 1 1 2 6 2
              4010990 5 4 1 1 1 1 6 1
              4010991 4 3 1 3 3 3 . 2
              4010994 5 4 1 1 2 3 6 2
              4011007 4 2 1 1 1 3 3 2
              4011008 3 3 3 2 1 3 2 2
              4011013 5 4 1 1 2 1 5 2
              4011022 5 5 1 1 2 1 6 2
              4011029 5 3 1 1 1 3 5 1
              4011033 3 3 1 1 1 1 5 1
              4011037 5 5 1 1 1 2 3 1
              4011038 5 4 1 1 1 2 4 1
              4011048 2 2 2 1 1 1 1 1
              4011055 3 4 1 1 1 2 2 2
              4011056 5 4 1 1 1 1 5 1
              4011063 4 4 1 1 1 1 6 2
              4011064 3 4 1 1 1 4 2 1
              4011070 5 5 1 1 1 3 5 2
              4011071 5 5 1 1 1 1 5 2
              4011078 5 5 1 1 2 2 2 2
              4011083 4 4 3 2 2 3 1 1
              4011085 5 2 1 1 1 1 2 1
              4011089 3 3 1 1 3 3 2 2
              4011091 5 4 2 1 1 2 4 1
              4011093 5 5 1 1 1 1 6 1
              4011101 5 4 3 2 1 2 6 1
              4011105 5 5 1 1 1 1 5 2
              4011106 5 3 1 3 1 2 6 1
              end
              label values p1 en511
              label values p2 en511
              label values p3 en511
              label values p4 en511
              label values p5 en511
              label values p6 en511
              label def en511 2 "rather disagree", modify
              label def en511 3 "half and half", modify
              label def en511 4 "rather agree", modify
              label def en511 5 "completely agree", modify
              label def en511 1 "completely disagree", modify
              label values sorigin gorigin
              label def gorigin 1 "DISLE", modify
              label def gorigin 2 "FIELC", modify
              label def gorigin 3 "FIEIC", modify
              label def gorigin 4 "FIWHC", modify
              label def gorigin 5 "FHEIC", modify
              label def gorigin 6 "MPRI", modify
              label values sex tsex
              label def tsex 1 "male", modify
              label def tsex 2 "female", modify
              Thanks Jonas

              Comment


              • #8
                Thanks for the data example. There is only a little more to say. Technique is exemplified by

                Code:
                preserve 
                
                reshape long p, i(id) j(question) 
                
                tabplot p question, showval name(G1, replace) 
                
                tabplot p question, showval by(sex, note("")) name(G2, replace) percent(sex question) 
                
                restore

                Comment


                • #9
                  Nick Cox
                  Many thanks for the example code customised to my dataset. Straight from the start it looks promising! I'll look around and see what else I can do with the command. Thanks again for your input, Nick.

                  Comment

                  Working...
                  X