date and time data on x axis

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

date and time data on x axis

snowball0916
Hi, guys
How do you guys deal with the date and time data on x axis?
I have some trouble with it. Could you help with this?

=============
Sample Data
=============
The sample data look like this:

20181028_10:00:00 600
20181028_10:00:01 500
20181028_10:00:02 450
20181028_10:00:03 660
......

=============
My Code
=============

library(lubridate)
mydata <- read.table("e:/R_study/graph_test2.txt")
xdata <- ymd_hms(mydata$V1)
ydata <- mydata$V2
plot(xdata, ydata, type="o")


=============
Questions:
=============

1. Why my x axis does not show me the correct date time like ""2018-10-28 10:00:00 UTC" ?
2. If my data is very huge(like data in every second and the data has the whole day , even the whole month), how can I display the x axis in a neat and clean way?

Thanks very much.
 



        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date and time data on x axis

Rui Barradas
Hello,

Maybe you could get some inspiration in the following code.


op <- par(mar = c(4, 0, 0, 0) + par("mar"))

plot(xdata, ydata, type = "o", xaxt = "n")
axis.POSIXct(1, xdata, at = xdata, labels = xdata, las = 2)

par(op)


The important part is the call axis.POSIXct, argument las = 2 and the
call to par is to make sure that the labels are visible.


Hope this helps,

Rui Barradas


Às 15:16 de 28/10/2018, snowball0916 escreveu:

> Hi, guys
> How do you guys deal with the date and time data on x axis?
> I have some trouble with it. Could you help with this?
>
> =============
> Sample Data
> =============
> The sample data look like this:
>
> 20181028_10:00:00 600
> 20181028_10:00:01 500
> 20181028_10:00:02 450
> 20181028_10:00:03 660
> ......
>
> =============
> My Code
> =============
>
> library(lubridate)
> mydata <- read.table("e:/R_study/graph_test2.txt")
> xdata <- ymd_hms(mydata$V1)
> ydata <- mydata$V2
> plot(xdata, ydata, type="o")
>
>
> =============
> Questions:
> =============
>
> 1. Why my x axis does not show me the correct date time like ""2018-10-28 10:00:00 UTC" ?
> 2. If my data is very huge(like data in every second and the data has the whole day , even the whole month), how can I display the x axis in a neat and clean way?
>
> Thanks very much.
>  
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date and time data on x axis

jholtman
In reply to this post by snowball0916
You need to specify what the format of the date will be.  I am using
ggplot for the plot:


    library(lubridate)
    library(tidyverse)
    mydata <- read.table(text = "time value
    20181028_10:00:00 600
    20181028_10:00:01 500
    20181028_10:00:02 450
    20181028_10:00:03 660", header = TRUE, as.is = TRUE)

    mydata <- mutate(mydata,
                     time = ymd_hms(time)
    )

    ggplot(mydata, aes(time, value)) +
      geom_point() +
      scale_x_datetime(date_labels = "%m/%d %H:%M:%S"
      ) +
      theme(axis.text.x = element_text(angle = 25, vjust = 1.0, hjust = 1.0))

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.


On Sun, Oct 28, 2018 at 11:23 AM snowball0916 <[hidden email]> wrote:

>
> Hi, guys
> How do you guys deal with the date and time data on x axis?
> I have some trouble with it. Could you help with this?
>
> =============
> Sample Data
> =============
> The sample data look like this:
>
> 20181028_10:00:00 600
> 20181028_10:00:01 500
> 20181028_10:00:02 450
> 20181028_10:00:03 660
> ......
>
> =============
> My Code
> =============
>
> library(lubridate)
> mydata <- read.table("e:/R_study/graph_test2.txt")
> xdata <- ymd_hms(mydata$V1)
> ydata <- mydata$V2
> plot(xdata, ydata, type="o")
>
>
> =============
> Questions:
> =============
>
> 1. Why my x axis does not show me the correct date time like ""2018-10-28 10:00:00 UTC" ?
> 2. If my data is very huge(like data in every second and the data has the whole day , even the whole month), how can I display the x axis in a neat and clean way?
>
> Thanks very much.
>
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date and time data on x axis

snowball0916
In reply to this post by Rui Barradas
Hi, Rui
Thanks for your code, even though I'm not fully understand it.
And I am new to R, could you kindly help me with
-1). what's the nested usage stand for?
op <- par(mar = c(4, 0, 0, 0) + par("mar"))

-2). how can I get the axis.POSIXct usage or help?
I use help(axis) and help(POSIXct) and not found the actual use.

Thanks very much.






 
From: Rui Barradas
Date: 2018-10-29 02:53
To: snowball0916; r-help
Subject: Re: [R] date and time data on x axis
Hello,
 
Maybe you could get some inspiration in the following code.
 
 
op <- par(mar = c(4, 0, 0, 0) + par("mar"))
 
plot(xdata, ydata, type = "o", xaxt = "n")
axis.POSIXct(1, xdata, at = xdata, labels = xdata, las = 2)
 
par(op)
 
 
The important part is the call axis.POSIXct, argument las = 2 and the
call to par is to make sure that the labels are visible.
 
 
Hope this helps,
 
Rui Barradas
 
 
Às 15:16 de 28/10/2018, snowball0916 escreveu:

> Hi, guys
> How do you guys deal with the date and time data on x axis?
> I have some trouble with it. Could you help with this?
>
> =============
> Sample Data
> =============
> The sample data look like this:
>
> 20181028_10:00:00 600
> 20181028_10:00:01 500
> 20181028_10:00:02 450
> 20181028_10:00:03 660
> ......
>
> =============
> My Code
> =============
>
> library(lubridate)
> mydata <- read.table("e:/R_study/graph_test2.txt")
> xdata <- ymd_hms(mydata$V1)
> ydata <- mydata$V2
> plot(xdata, ydata, type="o")
>
>
> =============
> Questions:
> =============
>
> 1. Why my x axis does not show me the correct date time like ""2018-10-28 10:00:00 UTC" ?
> 2. If my data is very huge(like data in every second and the data has the whole day , even the whole month), how can I display the x axis in a neat and clean way?
>
> Thanks very much.
>  
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date and time data on x axis

snowball0916
In reply to this post by jholtman
Hi, Jim
Thanks very much, I will need to study your code, though.
Will large volume of data will affect the x axis display?
Thanks again.
 


 
From: jim holtman
Date: 2018-10-29 05:53
To: snowball0916
CC: R mailing list
Subject: Re: [R] date and time data on x axis
You need to specify what the format of the date will be.  I am using
ggplot for the plot:
 
 
    library(lubridate)
    library(tidyverse)
    mydata <- read.table(text = "time value
    20181028_10:00:00 600
    20181028_10:00:01 500
    20181028_10:00:02 450
    20181028_10:00:03 660", header = TRUE, as.is = TRUE)
 
    mydata <- mutate(mydata,
                     time = ymd_hms(time)
    )
 
    ggplot(mydata, aes(time, value)) +
      geom_point() +
      scale_x_datetime(date_labels = "%m/%d %H:%M:%S"
      ) +
      theme(axis.text.x = element_text(angle = 25, vjust = 1.0, hjust = 1.0))
 
Jim Holtman
Data Munger Guru
 
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
 
Jim Holtman
Data Munger Guru
 
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
 
 
On Sun, Oct 28, 2018 at 11:23 AM snowball0916 <[hidden email]> wrote:

>
> Hi, guys
> How do you guys deal with the date and time data on x axis?
> I have some trouble with it. Could you help with this?
>
> =============
> Sample Data
> =============
> The sample data look like this:
>
> 20181028_10:00:00 600
> 20181028_10:00:01 500
> 20181028_10:00:02 450
> 20181028_10:00:03 660
> ......
>
> =============
> My Code
> =============
>
> library(lubridate)
> mydata <- read.table("e:/R_study/graph_test2.txt")
> xdata <- ymd_hms(mydata$V1)
> ydata <- mydata$V2
> plot(xdata, ydata, type="o")
>
>
> =============
> Questions:
> =============
>
> 1. Why my x axis does not show me the correct date time like ""2018-10-28 10:00:00 UTC" ?
> 2. If my data is very huge(like data in every second and the data has the whole day , even the whole month), how can I display the x axis in a neat and clean way?
>
> Thanks very much.
>
>
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date and time data on x axis

R help mailing list-2
In reply to this post by snowball0916
Here's an example of 24 hours of data at one second intervals.

npts <- 24*60*60

df <- data.frame(
                 tm = seq( Sys.time(), by='1 sec', length=npts),
                 yd = round(runif(npts),2)
                 )

head(df)

with(df, plot(tm,yd))

The x axis appears to me to be displayed in a neat and clean way. I don't understand what the problem is.
(The data itself is illegible, but that's a different problem.)

The default axis may not be what you want, but it is neat and clean. To choose the axis tick marks and labels yourself, use axis() or axis.POSIXct, as Rui mentioned.  help(axis.POSIXct) provides examples of actual use.

I prefer to do as much as possible with base R, so look at this example:

> as.POSIXct( '20181028_10:00:00' , format='%Y%m%d_%H:%M:%S')
[1] "2018-10-28 10:00:00 PDT"

Therefore
  xdata <- as.POSIXct(mydata$V1, format='%Y%m%d_%H:%M:%S')
is perfectly adequate (the lubridate package is not essential here)


par() is the function that sets graphical parameters. There are many graphical parameters.
"mar" is the parameter that specifies the sizes of the plot margins  ( see ?par )

This expression
   op <- par(mar = c(4, 0, 0, 0) + par("mar"))
is a way to modify the values of the "mar" parameter.

Type the following commands
   par('mar')
   par()$mar        ## an alternative
   c(4,0,0,0) + par('mar')
   par(mar = c(4, 0, 0, 0) + par("mar"))
   par('mar')        ## to see that the margins have been changed

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 

On 10/28/18, 8:16 AM, "R-help on behalf of snowball0916" <[hidden email] on behalf of [hidden email]> wrote:

    Hi, guys
    How do you guys deal with the date and time data on x axis?
    I have some trouble with it. Could you help with this?
   
    =============
    Sample Data
    =============
    The sample data look like this:
   
    20181028_10:00:00 600
    20181028_10:00:01 500
    20181028_10:00:02 450
    20181028_10:00:03 660
    ......
   
    =============
    My Code
    =============
   
    library(lubridate)
    mydata <- read.table("e:/R_study/graph_test2.txt")
    xdata <- ymd_hms(mydata$V1)
    ydata <- mydata$V2
    plot(xdata, ydata, type="o")
   
   
    =============
    Questions:
    =============
   
    1. Why my x axis does not show me the correct date time like ""2018-10-28 10:00:00 UTC" ?
    2. If my data is very huge(like data in every second and the data has the whole day , even the whole month), how can I display the x axis in a neat and clean way?
   
    Thanks very much.
     
   
   
   
    [[alternative HTML version deleted]]
   
    ______________________________________________
    [hidden email] 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.
   

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date and time data on x axis

Rui Barradas
In reply to this post by snowball0916
Hello,

Inline.

Às 14:03 de 29/10/2018, snowball0916 escreveu:
> Hi, Rui
> Thanks for your code, even though I'm not fully understand it.
> And I am new to R, could you kindly help me with
> -1). what's the nested usage stand for?
> op <- par(mar = c(4, 0, 0, 0) + par("mar"))

See the help for ?par. You have

mar
A numerical vector of the form c(bottom, left, top, right) which gives
the number of lines of margin to be specified on the four sides of the
plot. The default is c(5, 4, 4, 2) + 0.1.


So par("mar") returns c(5.1, 4.1, 4.1, 2.1)
In order to see the x axis labels, that are longer than normal, I have
added 4 to the bottom only, c(4, 0, 0, 0) + default_value.
And par() returns the previous values of the graphices parameters, so
save them in op (old param) and restore the defaults when done plotting.

>
> -2). how can I get the axis.POSIXct usage or help?
> I use help(axis)and help(POSIXct)and not found the actual use.

It's ?axis.POSIXct or help('axis.POSIXct'). You want the S3 method for
objects of class "POSIXct".

Hope this helps,

Rui Barradas

>
> Thanks very much.
>
>
>
>
> ------------------------------------------------------------------------
>
>     *From:* Rui Barradas <mailto:[hidden email]>
>     *Date:* 2018-10-29 02:53
>     *To:* snowball0916 <mailto:[hidden email]>; r-help
>     <mailto:[hidden email]>
>     *Subject:* Re: [R] date and time data on x axis
>     Hello,
>     Maybe you could get some inspiration in the following code.
>     op <- par(mar = c(4, 0, 0, 0) + par("mar"))
>     plot(xdata, ydata, type = "o", xaxt = "n")
>     axis.POSIXct(1, xdata, at = xdata, labels = xdata, las = 2)
>     par(op)
>     The important part is the call axis.POSIXct, argument las = 2 and the
>     call to par is to make sure that the labels are visible.
>     Hope this helps,
>     Rui Barradas
>     Às 15:16 de 28/10/2018, snowball0916 escreveu:
>      > Hi, guys
>      > How do you guys deal with the date and time data on x axis?
>      > I have some trouble with it. Could you help with this?
>      >
>      > =============
>      > Sample Data
>      > =============
>      > The sample data look like this:
>      >
>      > 20181028_10:00:00 600
>      > 20181028_10:00:01 500
>      > 20181028_10:00:02 450
>      > 20181028_10:00:03 660
>      > ......
>      >
>      > =============
>      > My Code
>      > =============
>      >
>      > library(lubridate)
>      > mydata <- read.table("e:/R_study/graph_test2.txt")
>      > xdata <- ymd_hms(mydata$V1)
>      > ydata <- mydata$V2
>      > plot(xdata, ydata, type="o")
>      >
>      >
>      > =============
>      > Questions:
>      > =============
>      >
>      > 1. Why my x axis does not show me the correct date time like
>     ""2018-10-28 10:00:00 UTC" ?
>      > 2. If my data is very huge(like data in every second and the data
>     has the whole day , even the whole month), how can I display the x
>     axis in a neat and clean way?
>      >
>      > Thanks very much.
>      >
>      >
>      >
>      >
>      > [[alternative HTML version deleted]]
>      >
>      > ______________________________________________
>      > [hidden email] 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.
>      >
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date and time data on x axis

snowball0916
Hi, Rui
Thank you . I will try later.
Thanks again.

 
 


 
From: Rui Barradas
Date: 2018-10-30 00:38
To: snowball0916; r-help
Subject: Re: [R] date and time data on x axis
Hello,
 
Inline.
 
Às 14:03 de 29/10/2018, snowball0916 escreveu:
> Hi, Rui
> Thanks for your code, even though I'm not fully understand it.
> And I am new to R, could you kindly help me with
> -1). what's the nested usage stand for?
> op <- par(mar = c(4, 0, 0, 0) + par("mar"))
 
See the help for ?par. You have
 
mar
A numerical vector of the form c(bottom, left, top, right) which gives
the number of lines of margin to be specified on the four sides of the
plot. The default is c(5, 4, 4, 2) + 0.1.
 
 
So par("mar") returns c(5.1, 4.1, 4.1, 2.1)
In order to see the x axis labels, that are longer than normal, I have
added 4 to the bottom only, c(4, 0, 0, 0) + default_value.
And par() returns the previous values of the graphices parameters, so
save them in op (old param) and restore the defaults when done plotting.
 
>
> -2). how can I get the axis.POSIXct usage or help?
> I use help(axis)and help(POSIXct)and not found the actual use.
 
It's ?axis.POSIXct or help('axis.POSIXct'). You want the S3 method for
objects of class "POSIXct".
 
Hope this helps,
 
Rui Barradas
 

>
> Thanks very much.
>
>
>
>
> ------------------------------------------------------------------------
>
>     *From:* Rui Barradas <mailto:[hidden email]>
>     *Date:* 2018-10-29 02:53
>     *To:* snowball0916 <mailto:[hidden email]>; r-help
>     <mailto:[hidden email]>
>     *Subject:* Re: [R] date and time data on x axis
>     Hello,
>     Maybe you could get some inspiration in the following code.
>     op <- par(mar = c(4, 0, 0, 0) + par("mar"))
>     plot(xdata, ydata, type = "o", xaxt = "n")
>     axis.POSIXct(1, xdata, at = xdata, labels = xdata, las = 2)
>     par(op)
>     The important part is the call axis.POSIXct, argument las = 2 and the
>     call to par is to make sure that the labels are visible.
>     Hope this helps,
>     Rui Barradas
>     Às 15:16 de 28/10/2018, snowball0916 escreveu:
>      > Hi, guys
>      > How do you guys deal with the date and time data on x axis?
>      > I have some trouble with it. Could you help with this?
>      >
>      > =============
>      > Sample Data
>      > =============
>      > The sample data look like this:
>      >
>      > 20181028_10:00:00 600
>      > 20181028_10:00:01 500
>      > 20181028_10:00:02 450
>      > 20181028_10:00:03 660
>      > ......
>      >
>      > =============
>      > My Code
>      > =============
>      >
>      > library(lubridate)
>      > mydata <- read.table("e:/R_study/graph_test2.txt")
>      > xdata <- ymd_hms(mydata$V1)
>      > ydata <- mydata$V2
>      > plot(xdata, ydata, type="o")
>      >
>      >
>      > =============
>      > Questions:
>      > =============
>      >
>      > 1. Why my x axis does not show me the correct date time like
>     ""2018-10-28 10:00:00 UTC" ?
>      > 2. If my data is very huge(like data in every second and the data
>     has the whole day , even the whole month), how can I display the x
>     axis in a neat and clean way?
>      >
>      > Thanks very much.
>      >
>      >
>      >
>      >
>      > [[alternative HTML version deleted]]
>      >
>      > ______________________________________________
>      > [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date and time data on x axis

snowball0916
In reply to this post by R help mailing list-2
Hi, Don
I got it, I will try and study .
Thanks very much.
 


 
From: MacQueen, Don
Date: 2018-10-30 00:01
To: snowball0916; r-help
Subject: Re: [R] date and time data on x axis
Here's an example of 24 hours of data at one second intervals.
 
npts <- 24*60*60
 
df <- data.frame(
                 tm = seq( Sys.time(), by='1 sec', length=npts),
                 yd = round(runif(npts),2)
                 )
 
head(df)
 
with(df, plot(tm,yd))
 
The x axis appears to me to be displayed in a neat and clean way. I don't understand what the problem is.
(The data itself is illegible, but that's a different problem.)
 
The default axis may not be what you want, but it is neat and clean. To choose the axis tick marks and labels yourself, use axis() or axis.POSIXct, as Rui mentioned.  help(axis.POSIXct) provides examples of actual use.
 
I prefer to do as much as possible with base R, so look at this example:
 
> as.POSIXct( '20181028_10:00:00' , format='%Y%m%d_%H:%M:%S')
[1] "2018-10-28 10:00:00 PDT"
 
Therefore
  xdata <- as.POSIXct(mydata$V1, format='%Y%m%d_%H:%M:%S')
is perfectly adequate (the lubridate package is not essential here)
 
 
par() is the function that sets graphical parameters. There are many graphical parameters.
"mar" is the parameter that specifies the sizes of the plot margins  ( see ?par )
 
This expression
   op <- par(mar = c(4, 0, 0, 0) + par("mar"))
is a way to modify the values of the "mar" parameter.
 
Type the following commands
   par('mar')
   par()$mar        ## an alternative
   c(4,0,0,0) + par('mar')
   par(mar = c(4, 0, 0, 0) + par("mar"))
   par('mar')        ## to see that the margins have been changed
 
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
On 10/28/18, 8:16 AM, "R-help on behalf of snowball0916" <[hidden email] on behalf of [hidden email]> wrote:
 
    Hi, guys
    How do you guys deal with the date and time data on x axis?
    I have some trouble with it. Could you help with this?
   
    =============
    Sample Data
    =============
    The sample data look like this:
   
    20181028_10:00:00 600
    20181028_10:00:01 500
    20181028_10:00:02 450
    20181028_10:00:03 660
    ......
   
    =============
    My Code
    =============
   
    library(lubridate)
    mydata <- read.table("e:/R_study/graph_test2.txt")
    xdata <- ymd_hms(mydata$V1)
    ydata <- mydata$V2
    plot(xdata, ydata, type="o")
   
   
    =============
    Questions:
    =============
   
    1. Why my x axis does not show me the correct date time like ""2018-10-28 10:00:00 UTC" ?
    2. If my data is very huge(like data in every second and the data has the whole day , even the whole month), how can I display the x axis in a neat and clean way?
   
    Thanks very much.
     
   
   
   
    [[alternative HTML version deleted]]
   
    ______________________________________________
    [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date and time data on x axis

snowball0916
In reply to this post by R help mailing list-2
Hi, Don
After I've tried 1 month data. It show me like attachment.

The problem is it's hard to identify the the high point from the graph.
Is there possible, when my cursor move on some point , it will show me both x axis and y axis data?
OR
Is there other way to get the same goal?

Thanks very much.
 
 


 
From: MacQueen, Don
Date: 2018-10-30 00:01
To: snowball0916; r-help
Subject: Re: [R] date and time data on x axis
Here's an example of 24 hours of data at one second intervals.
 
npts <- 24*60*60
 
df <- data.frame(
                 tm = seq( Sys.time(), by='1 sec', length=npts),
                 yd = round(runif(npts),2)
                 )
 
head(df)
 
with(df, plot(tm,yd))
 
The x axis appears to me to be displayed in a neat and clean way. I don't understand what the problem is.
(The data itself is illegible, but that's a different problem.)
 
The default axis may not be what you want, but it is neat and clean. To choose the axis tick marks and labels yourself, use axis() or axis.POSIXct, as Rui mentioned.  help(axis.POSIXct) provides examples of actual use.
 
I prefer to do as much as possible with base R, so look at this example:
 
> as.POSIXct( '20181028_10:00:00' , format='%Y%m%d_%H:%M:%S')
[1] "2018-10-28 10:00:00 PDT"
 
Therefore
  xdata <- as.POSIXct(mydata$V1, format='%Y%m%d_%H:%M:%S')
is perfectly adequate (the lubridate package is not essential here)
 
 
par() is the function that sets graphical parameters. There are many graphical parameters.
"mar" is the parameter that specifies the sizes of the plot margins  ( see ?par )
 
This expression
   op <- par(mar = c(4, 0, 0, 0) + par("mar"))
is a way to modify the values of the "mar" parameter.
 
Type the following commands
   par('mar')
   par()$mar        ## an alternative
   c(4,0,0,0) + par('mar')
   par(mar = c(4, 0, 0, 0) + par("mar"))
   par('mar')        ## to see that the margins have been changed
 
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
On 10/28/18, 8:16 AM, "R-help on behalf of snowball0916" <[hidden email] on behalf of [hidden email]> wrote:
 
    Hi, guys
    How do you guys deal with the date and time data on x axis?
    I have some trouble with it. Could you help with this?
   
    =============
    Sample Data
    =============
    The sample data look like this:
   
    20181028_10:00:00 600
    20181028_10:00:01 500
    20181028_10:00:02 450
    20181028_10:00:03 660
    ......
   
    =============
    My Code
    =============
   
    library(lubridate)
    mydata <- read.table("e:/R_study/graph_test2.txt")
    xdata <- ymd_hms(mydata$V1)
    ydata <- mydata$V2
    plot(xdata, ydata, type="o")
   
   
    =============
    Questions:
    =============
   
    1. Why my x axis does not show me the correct date time like ""2018-10-28 10:00:00 UTC" ?
    2. If my data is very huge(like data in every second and the data has the whole day , even the whole month), how can I display the x axis in a neat and clean way?
   
    Thanks very much.
     
   
   
   
    [[alternative HTML version deleted]]
   
    ______________________________________________
    [hidden email] 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.
   
 
______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date and time data on x axis

Bert Gunter-2
See ?identify and ?locator

Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sat, Nov 3, 2018 at 6:47 PM snowball0916 <[hidden email]> wrote:

> Hi, Don
> After I've tried 1 month data. It show me like attachment.
>
> The problem is it's hard to identify the the high point from the graph.
> Is there possible, when my cursor move on some point , it will show me
> both x axis and y axis data?
> OR
> Is there other way to get the same goal?
>
> Thanks very much.
>
>
>
>
>
> From: MacQueen, Don
> Date: 2018-10-30 00:01
> To: snowball0916; r-help
> Subject: Re: [R] date and time data on x axis
> Here's an example of 24 hours of data at one second intervals.
>
> npts <- 24*60*60
>
> df <- data.frame(
>                  tm = seq( Sys.time(), by='1 sec', length=npts),
>                  yd = round(runif(npts),2)
>                  )
>
> head(df)
>
> with(df, plot(tm,yd))
>
> The x axis appears to me to be displayed in a neat and clean way. I don't
> understand what the problem is.
> (The data itself is illegible, but that's a different problem.)
>
> The default axis may not be what you want, but it is neat and clean. To
> choose the axis tick marks and labels yourself, use axis() or axis.POSIXct,
> as Rui mentioned.  help(axis.POSIXct) provides examples of actual use.
>
> I prefer to do as much as possible with base R, so look at this example:
>
> > as.POSIXct( '20181028_10:00:00' , format='%Y%m%d_%H:%M:%S')
> [1] "2018-10-28 10:00:00 PDT"
>
> Therefore
>   xdata <- as.POSIXct(mydata$V1, format='%Y%m%d_%H:%M:%S')
> is perfectly adequate (the lubridate package is not essential here)
>
>
> par() is the function that sets graphical parameters. There are many
> graphical parameters.
> "mar" is the parameter that specifies the sizes of the plot margins  ( see
> ?par )
>
> This expression
>    op <- par(mar = c(4, 0, 0, 0) + par("mar"))
> is a way to modify the values of the "mar" parameter.
>
> Type the following commands
>    par('mar')
>    par()$mar        ## an alternative
>    c(4,0,0,0) + par('mar')
>    par(mar = c(4, 0, 0, 0) + par("mar"))
>    par('mar')        ## to see that the margins have been changed
>
> --
> Don MacQueen
> Lawrence Livermore National Laboratory
> 7000 East Ave., L-627
> Livermore, CA 94550
> 925-423-1062
> Lab cell 925-724-7509
>
> On 10/28/18, 8:16 AM, "R-help on behalf of snowball0916" <
> [hidden email] on behalf of [hidden email]> wrote:
>
>     Hi, guys
>     How do you guys deal with the date and time data on x axis?
>     I have some trouble with it. Could you help with this?
>
>     =============
>     Sample Data
>     =============
>     The sample data look like this:
>
>     20181028_10:00:00 600
>     20181028_10:00:01 500
>     20181028_10:00:02 450
>     20181028_10:00:03 660
>     ......
>
>     =============
>     My Code
>     =============
>
>     library(lubridate)
>     mydata <- read.table("e:/R_study/graph_test2.txt")
>     xdata <- ymd_hms(mydata$V1)
>     ydata <- mydata$V2
>     plot(xdata, ydata, type="o")
>
>
>     =============
>     Questions:
>     =============
>
>     1. Why my x axis does not show me the correct date time like
> ""2018-10-28 10:00:00 UTC" ?
>     2. If my data is very huge(like data in every second and the data has
> the whole day , even the whole month), how can I display the x axis in a
> neat and clean way?
>
>     Thanks very much.
>
>
>
>
>     [[alternative HTML version deleted]]
>
>     ______________________________________________
>     [hidden email] 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.
>
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: date and time data on x axis

snowball0916
Hi, Bert
I will check those two, by the way, how can you find the two function? have you used them before?
Thanks very much.
 


 
From: Bert Gunter
Date: 2018-11-04 10:07
To: snowball0916
CC: MacQueen, Don; R-help
Subject: Re: [R] date and time data on x axis
See ?identify and ?locator

Cheers,
Bert

Bert Gunter

"The trouble with having an open mind is that people keep coming along and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sat, Nov 3, 2018 at 6:47 PM snowball0916 <[hidden email]> wrote:
Hi, Don
After I've tried 1 month data. It show me like attachment.

The problem is it's hard to identify the the high point from the graph.
Is there possible, when my cursor move on some point , it will show me both x axis and y axis data?
OR
Is there other way to get the same goal?

Thanks very much.





From: MacQueen, Don
Date: 2018-10-30 00:01
To: snowball0916; r-help
Subject: Re: [R] date and time data on x axis
Here's an example of 24 hours of data at one second intervals.

npts <- 24*60*60

df <- data.frame(
                 tm = seq( Sys.time(), by='1 sec', length=npts),
                 yd = round(runif(npts),2)
                 )

head(df)

with(df, plot(tm,yd))

The x axis appears to me to be displayed in a neat and clean way. I don't understand what the problem is.
(The data itself is illegible, but that's a different problem.)

The default axis may not be what you want, but it is neat and clean. To choose the axis tick marks and labels yourself, use axis() or axis.POSIXct, as Rui mentioned.  help(axis.POSIXct) provides examples of actual use.

I prefer to do as much as possible with base R, so look at this example:

> as.POSIXct( '20181028_10:00:00' , format='%Y%m%d_%H:%M:%S')
[1] "2018-10-28 10:00:00 PDT"

Therefore
  xdata <- as.POSIXct(mydata$V1, format='%Y%m%d_%H:%M:%S')
is perfectly adequate (the lubridate package is not essential here)


par() is the function that sets graphical parameters. There are many graphical parameters.
"mar" is the parameter that specifies the sizes of the plot margins  ( see ?par )

This expression
   op <- par(mar = c(4, 0, 0, 0) + par("mar"))
is a way to modify the values of the "mar" parameter.

Type the following commands
   par('mar')
   par()$mar        ## an alternative
   c(4,0,0,0) + par('mar')
   par(mar = c(4, 0, 0, 0) + par("mar"))
   par('mar')        ## to see that the margins have been changed

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509

On 10/28/18, 8:16 AM, "R-help on behalf of snowball0916" <[hidden email] on behalf of [hidden email]> wrote:

    Hi, guys
    How do you guys deal with the date and time data on x axis?
    I have some trouble with it. Could you help with this?

    =============
    Sample Data
    =============
    The sample data look like this:

    20181028_10:00:00 600
    20181028_10:00:01 500
    20181028_10:00:02 450
    20181028_10:00:03 660
    ......

    =============
    My Code
    =============

    library(lubridate)
    mydata <- read.table("e:/R_study/graph_test2.txt")
    xdata <- ymd_hms(mydata$V1)
    ydata <- mydata$V2
    plot(xdata, ydata, type="o")


    =============
    Questions:
    =============

    1. Why my x axis does not show me the correct date time like ""2018-10-28 10:00:00 UTC" ?
    2. If my data is very huge(like data in every second and the data has the whole day , even the whole month), how can I display the x axis in a neat and clean way?

    Thanks very much.




    [[alternative HTML version deleted]]

    ______________________________________________
    [hidden email] 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.


______________________________________________
[hidden email] 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]]

______________________________________________
[hidden email] 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.