[R] date and time data on x axis

MacQueen, Don m@cqueen1 @end|ng |rom ||n|@gov
Mon Oct 29 17:01:12 CET 2018


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" <r-help-bounces using r-project.org on behalf of snowball0916 using 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.
     
    
    
    
    	[[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