[R] Using R lines() to show sunrise and sunset

@vi@e@gross m@iii@g oii gm@ii@com @vi@e@gross m@iii@g oii gm@ii@com
Tue Jul 19 04:09:36 CEST 2022


Questions like this keep popping up.

Your example shows a single string containing 4 parts: 2022-01-01 07:26:45 2022-01-01 16:57:07

So forget how to ask lines() to do anything. Ask how YOU can make what lines wants.

You need to preprocess what sounds like a vector of such strings into four parts and conveniently you have spaces between them.
2022-01-01
07:26:45
2022-01-01
16:57:07

As it happens, your data is measuring sunrise and sunset on the same day so I suspect you can toss the third part as redundant.

How you extract them is one of many ways. Pick any you like. But use the parts to make three vectors with names like theDate, sunUp and sundown that are character strings.

Use one of many ways to convert the above into a pure Date format and a pure Time Format of your choice as long as your choice of the plot() function can handle them. Using the same names above, you would call plot once with the appropriate arguments to set up your graph and hand it theDate and sunup to place on the axes you want. Then, before the plot is closed, add a call to lines() using theDate and sundown.

The answer as to how to tell R or lines() (neither of which are sentient or will pay any attention) is to ask a different question. What arguments do you provide to lines. The answer is you provide two different vectors with each being of a type that plot() and lines() and other such functions can handle. They do not handle text for plotting. They handle integers, floating point, or things that can in some way be converted to them such as the number of seconds since midnight or the number of seconds since Jan 1, 1970. 

Some would also suggest using ggplot() instead as it allows you to specify multiple sets of lines in a somewhat different way and supplies much more. 

The first step is asking the right question. You may have intended it but I repeat, NOTHING will get plot() or line() to handle your text version directly. What you want is to feed it what it wants.

Some have suggested specifics and there should be enough info for you to do some reading and put the pieces together.

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Gregory Coats via R-help
Sent: Monday, July 18, 2022 8:18 PM
To: David Winsemius <dwinsemius using comcast.net>
Cc: Gregory Coats via R-help <r-help using r-project.org>
Subject: Re: [R] Using R lines() to show sunrise and sunset

Thanks. Yes, I can follow
https://www.stat.berkeley.edu/~s133/dates.html
Dates and Times in R
But my problem is, How to direct R to accept the first computed value as a sunrise, and the second computed values as a sunset?
Greg Coats
571-423-9847

> On Jul 18, 2022, at 7:58 PM, David Winsemius <dwinsemius using comcast.net> wrote:
> 
> The first thought it to do is make sure you have the data in POSIXct format for the date times. 
> 
> Sent from my iPhone
> 
>> On Jul 18, 2022, at 6:51 PM, Gregory Coats via R-help <r-help using r-project.org> wrote:
>> 
>> I compiled a program on my Apple MacBook that takes as inputs
>>   Year and Month and Day
>>   Latitude and Longitude
>> And then computes these two outputs
>> Sunrise Year-Month-Day Hour:Minute:Second Sunset  Year-Month-Day 
>> Hour:Minute:Second It automatically handles Daylight Savings Time.
>> A typical input, followed by the automatically computed outputs looks likes this.
>> 
>> ./sunrise_05 2022 01 1 38.8586314239524 77.0512533684194
>> 2022-01-01 07:26:45 2022-01-01 16:57:07
>> ./sunrise_05 2022 01 2 38.8586314239524 77.0512533684194
>> 2022-01-02 07:26:52 2022-01-02 16:57:56
>> 
>> I want to use R’s lines() command to show the sunrise and sunset times for the year 2012. How do I tell R that the first computed output is sunrise, and the second computer output is sunset?
>> Greg Coats
>>   [[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see 
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide 
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.


	[[alternative HTML version deleted]]

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list