Announcement

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

  • General question about graphs : How can I truncate the graph to better see the variations on a histogram?

    Hi everyone,

    I've come to ask a rather general question, following the excellent suggestion provided by Maarten Buis in a post to which he replied and advice me to use the -twoway spike- command for these type of data.

    How can I "zoom in", if I can call it that, on the period I'm interested in (i.e. more or less from 0 to 25,000) and not take the whole scale on the x-axis please?

    Here's the code I've written (I'm obviously open to any suggestions for improving my code, thank you).


    Code:
    forvalues j = 1/2 {
    bys power_p`j' : egen count_p`j' = count(power_p`j') if tariff_2
    replace count_p`j' = . if count_p`j' == 0
    }
    
        preserve
    
    
        bys power_p1 power_p2: keep if (_n==1)
    
            // First Period
               # delimit;
               twoway spike count_p1 power_p1, lwidth(thick) lcolor(green%25)
               ytitle("No. of Households", orient(horizontal))
               ylabel(, nogrid format(%9.0fc))
               xtitle("Contracted Power (in Watts)")
               subtitle("{bf}1{sup:st} Period", pos(11) size(2))
               scheme(white_w3d)
               legend(off)
               name(distribution_period_p1, replace)
               ;
               # delimit cr
              
              
            // Second Period  
               # delimit;
               twoway spike count_p2 power_p2, lwidth(thick) lcolor(blue%10)
               ytitle("No. of Households", orient(horizontal))
               ylabel(, nogrid format(%9.0fc))
               xtitle("Contracted Power (in Watts)")
               //xlabel(0(5000)25000, labsize(tiny) alternate nogrid format(%9.0fc))
               subtitle("{bf}2{sup:nd} Period", pos(11) size(2))
               scheme(white_w3d)
               legend(off)
               name(distribution_period_p2, replace)
               ;
               # delimit cr
              
              
               graph combine distribution_period_p1 distribution_period_p2,                ///
               cols(1) ycommon title("{bf}Households - Distribution of Contracted Powers on:", pos(11) size(2.75))    ///
               scheme(white_w3d) name(distribution_power_p1_p2, replace)
              
               graph export "../figures/hist_cont_powers_1st_2nd_Period.png", replace
               graph export "../figures/hist_cont_powers_1st_2nd_Period.pdf", replace
              
              
        restore
    Sorry Nick Cox, I have still to change my x-axis label to "Contracted Powers (in kW)"...
    Click image for larger version

Name:	hist_cont_powers_1st_2nd_Period.png
Views:	1
Size:	63.7 KB
ID:	1728587


    Nice continuation,

    Michael
    Last edited by Michael Duarte Goncalves; 29 Sep 2023, 09:46.

  • #2
    check to see if you have very large values for the variables, which usually is the reason you get that look.

    can use xlabel to restrict the range -

    , xlabel(0(10000)30000)

    Comment


    • #3
      Restricting the axis labels won't restrict the range of the data shown. An if condition is needed.

      Comment


      • #4
        Hi George Ford, Hi Nick Cox,

        Thank you both for you suggestions.
        I will have a look on the data, and try the if condition.

        Best,

        Michael
        Last edited by Michael Duarte Goncalves; 02 Oct 2023, 01:08.

        Comment

        Working...
        X