0

I have the following R dataset where the X axis are UNIX Epoch seconds and I am trying to plot it using the following code:

print("PLOT DATA")
print(dataset[1:10,])
plot(dataset[,"timestamp"], dataset[,"close"], cex=0, xlab="", ylab="")
lines(dataset[,"timestamp"], dataset[,"close"], xlab="", ylab="")
title(main="AAPL 2005-2010 DAILY CLOSE PRICES", xlab="Year", ylab="Price")

Which generates:

[1] "PLOT DATA"
    timestamp   open   high    low  close     volume
1  1104814800 1.1393 1.1691 1.1245 1.1418 1097193328
2  1104901200 1.1491 1.1652 1.1438 1.1518  680729056
3  1104987600 1.1551 1.1591 1.1309 1.1527  704141984
4  1105074000 1.1607 1.2434 1.1563 1.2366 2234953728
5  1105333200 1.2482 1.2625 1.2122 1.2315 1735512240
6  1105419600 1.2190 1.2348 1.1454 1.1529 2614860808
7  1105506000 1.1670 1.1768 1.1304 1.1690 2028471312
8  1105592400 1.3152 1.3290 1.2452 1.2465 3168234216
9  1105678800 1.2536 1.2807 1.2356 1.2536 1757210000
10 1106024400 1.2454 1.2625 1.2359 1.2616 1005413024

And I get:

RPLOT

How can I make the xticks in the plot display the year YYYY or month-year MM-YY instead of the UNIX Epoch seconds values?

Data

dataset <- structure(list(timestamp = c(1104814800L, 1104901200L, 1104987600L,  1105074000L, 1105333200L, 1105419600L, 1105506000L, 1105592400L,  1105678800L, 1106024400L), open = c(1.1393, 1.1491, 1.1551, 1.1607,  1.2482, 1.219, 1.167, 1.3152, 1.2536, 1.2454), high = c(1.1691,  1.1652, 1.1591, 1.2434, 1.2625, 1.2348, 1.1768, 1.329, 1.2807,  1.2625), low = c(1.1245, 1.1438, 1.1309, 1.1563, 1.2122, 1.1454,  1.1304, 1.2452, 1.2356, 1.2359), close = c(1.1418, 1.1518, 1.1527,  1.2366, 1.2315, 1.1529, 1.169, 1.2465, 1.2536, 1.2616), volume = c(1097193328,  680729056, 704141984, 2234953728, 1735512240, 2614860808, 2028471312,  3168234216, 1757210000, 1005413024)), class = "data.frame", row.names = c("1",  "2", "3", "4", "5", "6", "7", "8", "9", "10"))
rawr
  • 20,481
  • 4
  • 44
  • 78
M.E.
  • 4,955
  • 4
  • 49
  • 128
  • 1
    does this help? https://stackoverflow.com/a/9053934/2994949 `axis.POSIXct(3, as.POSIXct(dataset$timestamp, origin = '1970-01-01'), format = '%m-%y')` – rawr Oct 15 '20 at 18:18
  • 1
    Converting epoch to date: https://stackoverflow.com/a/13456338/7547327 – mrhellmann Oct 15 '20 at 18:19
  • 1
    to convert your timestamps to dates, you could use `as.POSIXct(dataset[,"timestamp"], origin = "1970-01-01")` , because you said it was UNIX time. – Daniel_j_iii Oct 15 '20 at 18:20

1 Answers1

0

From above's comments, one potential solution is:

print("PLOT DATA")
print(dataset[1:10,])
xaxis = as.POSIXct(dataset[,"timestamp"], origin="1970-01-01")
plot(xaxis, dataset[,"close"], cex=0, xlab="", ylab="", format="%Y")
lines(xaxis, dataset[,"close"], xlab="", ylab="")
title(main="AAPL 2005-2010 DAILY CLOSE PRICES", xlab="Year", ylab="Price")

Now I am wondering if text can be rotated 45%, but that is a different question.

M.E.
  • 4,955
  • 4
  • 49
  • 128