Announcement

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

  • How to construct a time-series graph with new photovoltaic adoptions with data at hand?

    Hi everyone,

    I have to construct a time-series plot in which I would like to have on the y-axis the new PV adoptions, by month. On the x-axis, the date from January 2021 to November 2023.
    Basically, I have data in which I observe electricity tariff from each ID, their contract types. etc. I want to plot the new PV adopters by month, but I don't know how to proceed, as this is the first time I want to plot time-series.

    Here is a small -dataex- :

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long id str19 id_date double(date_contract_start date_contract_end date_pv_install)
    1059 "1059_18889_21639" 18889 21639 22973
    1059 "1059_21640_21682" 21640 21682 22973
    1059 "1059_21683_21700" 21683 21700 22973
    1059 "1059_21701_22066" 21701 22066 22973
    1059 "1059_22067_22431" 22067 22431 22973
    1059 "1059_22432_22461" 22432 22461 22973
    1059 "1059_22462_22645" 22462 22645 22973
    1059 "1059_22646_22676" 22646 22676 22973
    1059 "1059_22677_22735" 22677 22735 22973
    1059 "1059_22736_22888" 22736 22888 22973
    1059 "1059_22889_22964" 22889 22964 22973
    1059 "1059_22965_23010" 22965 23010 22973
    1059 "1059_23011_."     23011     . 22973
    1089 "1089_18791_19904" 18791 19904 21875
    1089 "1089_19905_20119" 19905 20119 21875
    end
    format %td date_contract_start
    format %td date_contract_end
    format %td date_pv_install
    I want a graph in the same vein as the one attached. Could anyone please give me an insight about it?

    Thank you in advance.
    Michael
    Click image for larger version

Name:	PVAdopters_ExampleGraph.png
Views:	1
Size:	57.1 KB
ID:	1738314

  • #2
    You can use -twoway line- or -twoway connect- with the date variable on the x-axis. For example:

    Code:
    sort date_contract_start
    set seed 1234
    gen PV = _n + rnormal()
    twoway connect PV date_contract_start, xlabel(#13, format(%dCCYY))

    Comment


    • #3
      Hi Scott Merryman,

      Thank you very much for your suggestion, help and time. I have just one question, please, about the chunk of code provided:
      • Could you explain me why you use -rnormal()- in the chunk below, please? Or is it just an illustrative example?
      Code:
       gen PV = _n + rnormal()
      Thank you very much for your help and time!
      Have a lovely day.

      Best,
      Michael
      Last edited by Michael Duarte Goncalves; 29 Dec 2023, 02:07.

      Comment


      • #4
        Yes, is was just illustrative since sample data provided only had an ID and date variables.

        Comment


        • #5
          Nice! Thank you so much Scott.
          I tried your code and works perfectly well.

          All the best,
          Michael

          Comment


          • #6
            Hi again,

            I have a small problem with my labels:

            I made a -twoway connected- plot, as suggested to me by Scott Merryman. Thanks to him.
            But it seems that the -xlabel-s do not correspond with my values.

            Indeed, I created a variable with the count of the number of PV installations, by month. So, I should have 12 points by year, from Jan2021 to Jan2023. The problems are the following:
            1. I have 2021 repeated four times. The same for 2022.
            2. The values plotted do not correspond with the x-labels. It looks like my graph and its points are "shifted to the right".
            Could anyone give me a suggestion to correct that, please?
            Thank you in advance for your help.

            Here is my code:

            Code:
            gen double date_pv_install = dofc(clock(fecha_operativa_pv, "YMD hms"))
            format date_pv_install %td
            
            gen year   = year(date_pv_install)
            gen month  = month(date_pv_install)
            
            bysort id: keep if inrange(date_pv_install, date_contract_start, date_contract_end)
            
            
            bysort year month : egen count_pv_monthly = count(date_pv_install)
            
            sort date_pv_install
            twoway connect count_pv_monthly date_pv_install if date_contract_start > td(01jan2021),   ///
                             xlabel(#13, format(%dCCYY) angle(45) labsize(small)) ///
                             tline(01jun2021, lc(red%60) lp(dash) lw(medium)) ///
                             ylabel(0(25)225) msize(tiny) mcolor(black)  ///
                             ytitle("New PV Adoptions (Monthly)")  ///
                             lcolor(black) ///
                             scheme(white_w3d) plotregion(margin(none))
            And below the corresponding graph:
            Graph.png




            Thank you!
            Best,

            Michael
            Last edited by Michael Duarte Goncalves; 29 Dec 2023, 04:28.

            Comment

            Working...
            X