Announcement

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

  • Scatterplot help -

    Hello!

    I am ploting a scatterplot of average daily rainfall for a district over 5 years. I would like to single out the start of the monsoon season, which I have via a horizontal line. I have been trying to change the x axis labels with tlabel() but It doestnt work. ideally I would just like for the years to be stated. I also wanted to change the line colors, and was using lcolor(red) but the line stays grey - which is part of the scheme I am specifying.

    hre is the code of rthe attached graph

    scatter dailymeanrainfall edate, title("Scatterplot") subtitle("Average Rainfall in Jaipur district in Rajastan") xtitle("Years") tline(30may2009) tline(04jun2010) tline(27may2011) tline(13may2012) tline(13may2013) scheme(plotplain)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	73.3 KB
ID:	1523396


  • #2
    x axis labels with tlabel() but It doestnt work
    In a scatter plot, you use the -xlabel()- option to label the x-axis. Extract the years or label your time variable using years.

    Code:
    gen year= year(edate)
    scatter dailymeanrainfall year,...
    I also wanted to change the line colors, and was using lcolor(red) but the line stays grey
    If you adopt a scheme, you need to accept its defaults. Otherwise, use a different scheme or create your own.

    Comment


    • #3
      I've worked a lot with rainfall data. I would consider

      plotting on a cube root scale (which allows zeros to be shown as such)

      plotting against day of year starting at an arbitrary but convenient split (e.g. 1 May or day 120 of year) (the doy() function is invaluable there).

      Comment


      • #4
        On #2: no scheme definition I know forbids a user overriding it with an option choice.

        I installed plotplain (from Stata Journal, as #1 is expected to explain)

        and did this

        Code:
        . sysuse auto, clear
        (1978 Automobile Data)
        
        . scatter mpg weight, scheme(plotplain) xli(3000, lc(red))
        and believe me, the line is red. There is no syntax in #1 that shows an attempt to colour any line red, so I can't guess what else Mike tried.


        P.S. Annoying but I think correct: "Rajasthan" is so spelled.

        Comment


        • #5
          Correct Nick Cox, I took this statement at face value.

          I also wanted to change the line colors, and was using lcolor(red) but the line stays grey

          Comment


          • #6
            Thank you for the help! I indeed didnt include the color attempt at the graph, and have managed to change the labels (including the spelling )

            Comment


            • #7
              Dear Statalist,

              I am working with rainfall and health data.
              1. I would like to derive the average rainfall in the region of birth for the prenatal period (9 months before birth) for each child. I have data on the monthly precipitation, mean monthly rainfall, month and region of birth for each child.
              2. I would also like to derive the long term average rainfall in the region of birth excluding the individual's birth year (i.e., the 9 months or prenatal period)
              Can you please guide me on what to do for each of these cases?

              Regards!

              Comment


              • #8
                It would be difficult to suggest any specific code without a data example. However, the command rangestat from SSC will do all this for you.

                Code:
                ssc install rangestat
                help rangestat
                If you read through the help file, you will see similar examples of what you want. Otherwise, if you still cannot figure it out, use dataex to present an example of your dataset or datasets. Here, ensure that you have both birth information and rainfall data for at least one child. dataex is available in Stata 15+ and a fully updated version of Stata 14. If you have earlier versions, run

                Code:
                ssc install dataex
                help dataex

                Comment


                • #9
                  Dear Statalist,

                  Here is a sample of my data. For instance, for someone born in Jan 2005, I need to derive the average rainfall in the region for the prenatal period (i.e, May 2004 to Jan 2005). Is there a way to get all the prenatal averages for each month of 2005, 2006 etc? Secondly, I need to calculate the long run average rainfall in the region of birth (for instance Jan 2005 birth), excluding the 9 months.

                  reg year month mean rain precipitation (monthly)
                  1 2004 1 16.82793 15.3875
                  1 2004 2 9.908793 5.84
                  1 2004 3 7.752069 7.2333
                  1 2004 4 11.648196 15.12
                  1 2004 5 25.20661 32.007
                  1 2004 6 31.033167 26.2933
                  1 2004 7 26.338245 28.8567
                  1 2004 8 21.875484 21.63
                  1 2004 9 16.780794 13.1267
                  1 2004 10 18.437414 19.59
                  1 2004 11 20.098333 19.315
                  1 2004 12 26.05887 22.55
                  1 2005 1 16.82793 39.05

                  Thank you.

                  Regards!

                  Comment


                  • #10
                    I need to derive the average rainfall in the region for the prenatal period (i.e, May 2004 to Jan 2005). Is there a way to get all the prenatal averages for each month of 2005, 2006 etc?
                    Code:
                    * Example generated by -dataex-. To install: ssc install dataex
                    clear
                    input float(reg year month rain precipitation)
                    1 2004  1  16.82793 15.3875
                    1 2004  2  9.908793    5.84
                    1 2004  3  7.752069  7.2333
                    1 2004  4 11.648196   15.12
                    1 2004  5  25.20661  32.007
                    1 2004  6 31.033167 26.2933
                    1 2004  7 26.338245 28.8567
                    1 2004  8 21.875484   21.63
                    1 2004  9 16.780794 13.1267
                    1 2004 10 18.437414   19.59
                    1 2004 11 20.098333  19.315
                    1 2004 12  26.05887   22.55
                    1 2005  1  16.82793   39.05
                    end
                    
                    gen yearmonth= ym(year, month)
                    format yearmonth %tm
                    rangestat (mean) rain, interval(yearmonth -8 0) by(reg)
                    Now, your sample starts in Jan. 2004, so we cannot calculate a 9 month historical average for this month due to lack of data. The command above will give you the 1 month average which is that month's average rainfall. For Feb. 2004, you will get a 2 month average (Jan and Feb, 2004). Only in Sep. 2004 and beyond will you get a true 9 month average. If you want to blank out averages less than 9 months due to no data, you can subsequently

                    Code:
                    bys reg (yearmonth): replace rain_mean=. if _n<9
                    Secondly, I need to calculate the long run average rainfall in the region of birth (for instance Jan 2005 birth), excluding the 9 months.
                    This is not clear. How long is long-run?
                    Last edited by Andrew Musau; 05 Dec 2019, 07:05.

                    Comment


                    • #11

                      I am reporting, as the data is not very clear on the first post
                      reg year month meanrain precipitation (monthly)
                      1 2004 1 16.82793 15.3875
                      1 2004 2 9.908793 5.84
                      1 2004 3 7.752069 7.2333
                      1 2004 4 11.648196 15.12
                      1 2004 5 25.20661 32.007
                      1 2004 6 31.033167 26.2933
                      1 2004 7 26.338245 28.8567
                      1 2004 8 21.875484 21.63
                      1 2004 9 16.780794 13.1267
                      1 2004 10 18.437414 19.59
                      1 2004 11 20.098333 19.315
                      1 2004 12 26.05887 22.55
                      1 2005 1 16.82793 39.05

                      Thank you!

                      Comment


                      • #12
                        Dear Andrew,

                        Thank you for your reply.

                        have data from Jan 1981- Dec 2010. The meanrain is the long term mean for 1981-2010. I am trying to find the deviation or rainfall shock.
                        With regards to the second question, I need to sum the long-run average rainfall in the region of birth excluding the 9 months (prenatal months) for a particular child.

                        Regards!

                        Comment


                        • #13
                          So this is what you get (either the first table or the second as explained in #10). So you can merge the variable "rain_mean" with the data set containing birth information and it gives you the 9 month historical average rainfall.

                          Code:
                          
                          . l, sep(15)
                          
                               +-----------------------------------------------------------------+
                               | reg   year   month       rain   precip~n   yearmo~h   rain_mean |
                               |-----------------------------------------------------------------|
                            1. |   1   2004       1   16.82793    15.3875     2004m1    16.82793 |
                            2. |   1   2004       2   9.908793       5.84     2004m2   13.368362 |
                            3. |   1   2004       3   7.752069     7.2333     2004m3   11.496264 |
                            4. |   1   2004       4    11.6482      15.12     2004m4   11.534247 |
                            5. |   1   2004       5   25.20661     32.007     2004m5    14.26872 |
                            6. |   1   2004       6   31.03317    26.2933     2004m6   17.062794 |
                            7. |   1   2004       7   26.33825    28.8567     2004m7   18.387859 |
                            8. |   1   2004       8   21.87548      21.63     2004m8   18.823812 |
                            9. |   1   2004       9   16.78079    13.1267     2004m9    18.59681 |
                           10. |   1   2004      10   18.43741      19.59    2004m10   18.775641 |
                           11. |   1   2004      11   20.09833     19.315    2004m11   19.907813 |
                           12. |   1   2004      12   26.05887      22.55    2004m12   21.941902 |
                           13. |   1   2005       1   16.82793      39.05     2005m1   22.517428 |
                               +-----------------------------------------------------------------+
                          
                          . bys reg (yearmonth): replace rain_mean=. if _n<9
                          (8 real changes made, 8 to missing)
                          
                          . l, sep(15)
                          
                               +-----------------------------------------------------------------+
                               | reg   year   month       rain   precip~n   yearmo~h   rain_mean |
                               |-----------------------------------------------------------------|
                            1. |   1   2004       1   16.82793    15.3875     2004m1           . |
                            2. |   1   2004       2   9.908793       5.84     2004m2           . |
                            3. |   1   2004       3   7.752069     7.2333     2004m3           . |
                            4. |   1   2004       4    11.6482      15.12     2004m4           . |
                            5. |   1   2004       5   25.20661     32.007     2004m5           . |
                            6. |   1   2004       6   31.03317    26.2933     2004m6           . |
                            7. |   1   2004       7   26.33825    28.8567     2004m7           . |
                            8. |   1   2004       8   21.87548      21.63     2004m8           . |
                            9. |   1   2004       9   16.78079    13.1267     2004m9    18.59681 |
                           10. |   1   2004      10   18.43741      19.59    2004m10   18.775641 |
                           11. |   1   2004      11   20.09833     19.315    2004m11   19.907813 |
                           12. |   1   2004      12   26.05887      22.55    2004m12   21.941902 |
                           13. |   1   2005       1   16.82793      39.05     2005m1   22.517428 |
                               +-----------------------------------------------------------------+

                          Comment


                          • #14
                            have data from Jan 1981- Dec 2010. The meanrain is the long term mean for 1981-2010. I am trying to find the deviation or rainfall shock.
                            With regards to the second question, I need to sum the long-run average rainfall in the region of birth excluding the 9 months (prenatal months) for a particular child.
                            So we can define a very long window which will include all months in the sample. So we just need to exclude the past 9 months from a given month.

                            Code:
                            rangestat (mean) rain, interval(yearmonth -10000 -9) by(reg)
                            So this will give you the average that you seek. Then you can merge back with the other data set using region, year and month of birth as identifiers.

                            Edit: This will give you the mean before birth. Do you need the mean rainfall including months after birth as well (excluding the prenatal 9 month period)?
                            Last edited by Andrew Musau; 05 Dec 2019, 07:27.

                            Comment


                            • #15
                              Dear Andrew,

                              Thank you for your reply.

                              So the codes below will derive table #13, which is the prenatal average for each child?

                              rangestat (mean) rain, interval(yearmonth -1000 -9) by (reg)
                              gen yearmonth= ym(year, month) format yearmonth %tm rangestat (mean) rain, interval(yearmonth -8 0) by(reg) With regard to the second question, yes I need the mean rainfall or(long term average rainfall (or rain in table 1 ~#13)), for the region of birth excluding the 9 prenatal months). Therefore, it is months before and after the prenatal period. Please let me know if this is clear. Regards!

                              Comment

                              Working...
                              X