Skip to content

date and time data on x axis

9 messages · Rui Barradas, jim holtman, snowball0916 +1 more

#
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.
#
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:
#
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 <snowball0916 at 163.com> wrote:
#
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, 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 <snowball0916 at 163.com> wrote:

  
  
#
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:
[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" <r-help-bounces at r-project.org on behalf of snowball0916 at 163.com> 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.
     
    
    
    
    
    ______________________________________________
    R-help at 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.
#
Hello,

Inline.

?s 14:03 de 29/10/2018, snowball0916 escreveu:
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.
It's ?axis.POSIXct or help('axis.POSIXct'). You want the S3 method for 
objects of class "POSIXct".

Hope this helps,

Rui Barradas
1 day later
#
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:
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.
It's ?axis.POSIXct or help('axis.POSIXct'). You want the S3 method for 
objects of class "POSIXct".
 
Hope this helps,
 
Rui Barradas

  
  
#
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:
[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" <r-help-bounces at r-project.org on behalf of snowball0916 at 163.com> 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.
     
    
    
    
    
    ______________________________________________
    R-help at 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.