Announcement

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

  • Removing blank space at end of graph

    I am hoping for some advice in editing a graph. I used this code to generate the graph and manually edited the labels in the graph editor. I wanted to remove the space after the last bin but couldn't find a way to do this. Test.gph

    egen testvar = cut(Offdayrange), at(0, 1, 8, 32, 90, 180, 365, 730, 3000) icodes
    table testvar, contents(min Offdayrange max Offdayrange)
    hist testvar, freq bin(8)

    Any advice is appreciated.

  • #2
    Can you provide the graph as a png file (& post data with dataex & code with code delimiters—see FAQ 12).
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      I've attached the graph as a png file. I'd prefer to make up some dummy data. I'll dig up my notes about sending code.
      Attached Files

      Comment


      • #4
        You might be able to reduce the excess with the -xscale(range())- option. Replace the # below with the number smaller than the last blank tick:

        Code:
        hist testvar, freq bin(8) xscale(range(#))
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          Alternatively you could try to reduce the right margin of the plot region:

          Code:
          sysuse auto
          hist weight, freq 
          hist weight, freq plotregion(margin(r-5))
          Stata/MP 14.1 (64-bit x86-64)
          Revision 19 May 2016
          Win 8.1

          Comment


          • #6
            Thanks Carole.

            I came up with a strategy but it didn't work as intended in auto dataset. What I wanted to do seems to work in this dataset example below , by doing the following. I don't know the code equivalent. Doing this via code would be preferable.

            In the graph editor, select X axis properties.
            Select Axis rule.
            Select Range/delta
            For minimum vale insert 0
            For maximum value insert 7
            For delta select 1

            Below is a dummy dataset. I couldn't work out how to send code via code delimiters.

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float testvar
            0
            6
            4
            5
            3
            0
            3
            1
            7
            0
            0
            0
            5
            2
            end

            Comment


            • #7
              I don't think you need a work-around here. You just need to spell out the discrete option. You can tell from the tick positions that Stata isn't understanding the data perfectly, and that's why.

              While doing that I would myself rotate the graph to make the axis labels more readable (and lose the ticks, which aren't helpful). Lacking a data example, as urged by Carole J. Wilson, I just faked some similar data. (EDIT: One was just posted, but mine has labels, needed here for full flavour.) (I would work on your ambiguous bin boundaries, e.g. 3, 6, 12 months, although a statement somewhere that upper limits are inclusive would fix that.)


              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input float(freq time)
              68 1
               6 2
               8 3
              14 4
               7 5
               9 6
              11 7
               8 8
              end
              label values time time
              label def time 1 "Same day", modify
              label def time 2 "1-7 days", modify
              label def time 3 "8-30 days", modify
              label def time 4 "1-3 months", modify
              label def time 5 "3-6 months", modify
              label def time 6 "6-12 months", modify
              label def time 7 "1-2 years", modify
              label def time 8 ">2 years", modify
              
              
              histogram time [fw=freq], discrete horizontal scheme(s1color) ///
              yla(1/8, valuelabel ang(h) noticks) ysc(reverse) frequency bfcolor(ltblue) blcolor(blue) ///
              xtitle(number of patients)
              Click image for larger version

Name:	horizhist.png
Views:	1
Size:	21.9 KB
ID:	1460568


              Comment


              • #8
                Nick,

                Many thanks. The code worked perfectly

                I included a data example via datex in the e-mail immediately preceding yours. However, I still haven't worked out how to post executable code? Is this don ervia datex or some other means?
                Last edited by Bob Green; 02 Sep 2018, 04:58.

                Comment


                • #9
                  You already know what you need to know. dataex emits code to input data and as a convenience gives you CODE delimiters so that the forum software applies them as mark-up. You can use the same trick on any code copied and pasted to between CODE delimiters.

                  https://www.statalist.org/forums/help#stata (Carole's reference in #2) explains about CODE delimiters. You can type them directly or use the # button in the Editor here.

                  Comment


                  • #10
                    Thanks Nick. I had been trying to use dataex to get delimiters for code or adding the code delimiters in stata itself; and obviously getting errors.

                    Comment


                    • #11
                      Hello, is there any way I can suppress the free space below the zero in the graph. I have used
                      Code:
                        ylabel(0(.1) 1) xlabel(15(10) 100)
                      , but it doesn't help. Thanks.

                      Attached Files

                      Comment


                      • #12
                        No, you can't omit space that way.. Stata needs the space to show the red curve you asked for in the command you don't show us, and to a lesser extent the blue curve. The way you can omit space depends on instructing Stata explicitly where the red (or blue) curve should not be shown

                        In fact, backing up, both red and blue curves seem implausible fits. It looks as if your data must be zero or positive in which case curves that go negative at the slightest temptation are qualitatively wrong. For example, cubic and quartic polynomials would be bad ideas.

                        I guess this is something medical and that people over about 82 are few in number or sufficiently tough not to suffer from the condition in question.

                        EIther way, unless the point is to show that the curves are poor choices, you possibly need something else, say a spline or other smoother.

                        That's quite a lot of speculation, but any or all of (1) more context (2) a data example (3) the code you used would be a help.

                        Comment


                        • #13
                          Hi Nick, Thanks for your comment.

                          "In fact, backing up, both red and blue curves seem implausible fits. It looks as if your data must be zero or positive in which case curves that go negative at the slightest temptation are qualitatively wrong. For example, cubic and quartic polynomials would be bad ideas."
                          I do agree it should not go negative, I wonder why they do so. Is it because of quadratic and cubic polynomials?

                          Yes, maybe I should try out spline or other smoother for better fit.

                          The context is different, but your speculation rightly captures the situation.

                          The code is something like below

                          Code:
                          reg y_dum over30 AgeMinus30 InteractionL if age<50  , cluster(age)
                          predict Pl`y'`E'
                          reg y_dum  over30 AgeMinus30 InteractionL agesq InteractionQ , cluster(age)
                          predict Psq`y'`E'
                          reg y_dum over30 AgeMinus30 InteractionL agesq InteractionQ age3 InteractionC, cluster(age)
                          predict Pc`y'`E'
                          
                          graph twoway (scatter binM age , xline(30)) ///
                          (line Pl2017`E' age if age<=30, lcolor(black) lpattern(solid) ) ///
                          (line Pl2017`E' age if age>30 & age<50 , lcolor(black) lpattern(solid)) ///
                          (line Psq2017`E' age if age<=30  , lcolor(red) lpattern(longdash)) ///
                          (line Psq2017`E' age if age>30 , lcolor(red) lpattern(longdash)) ///
                          (line Pc2017`E' age if age<=30 , lcolor(blue) lpattern(dash)) ///
                          (line Pc2017`E' age if age>30 , lcolor(blue) lpattern(dash)), ///
                          legend(off)  /*xlabel(15(2)45)*/ name(graph`j', replace) title("rate (`E'), 2016-17 ", size(3)) ytitle("mean ") ylabel(0(.1) 1) yscale(range(0 1)) xlabel(15(10) 100) xtitle(age)    graphregion(color(white) margin(zero))

                          Comment

                          Working...
                          X