在3D图中绘制日期R.

时间:2017-07-08 15:50:17

标签: r datetime-format rgl as.date scatter3d

我试图在R中的3D图中绘制日期,x和y。(使用RStudio 0.99.903),R(3.3.2),scatterplot3d(0.3-40),rgl(0.98.1)

dates = c("2016-11-10","2016-11-20","2016-12-01","2016-12-15","2016-12-30")

x = rnorm(5,0,1)

y = rnorm(5,1,2)

A = data.frame(dates, x, y)

A$dates = as.Date(A$dates,"%yyyy-%mm-%dd")

library(scatterplot3d)

with(data=A, scatterplot3d(x=x,y=y,z=dates))

This plots the dates as integers

I also tried the rgl package, but get the same result.

library(rgl)

plot3d(x=A$x,y=A$y,z=A$dates)

2 个答案:

答案 0 :(得分:1)

了解如何使用as.Date。您需要适当地格式化日期,并获得所需的情节:

     dates = c("2016-11-10","2016-11-20","2016-12-01","2016-12-15","2016-12-30")
     x = rnorm(5,0,1)
     y = rnorm(5,1,2)
     A = data.frame(dates, x, y)

     A$dates = as.Date(A$dates,"%Y-%m-%d")

     library(scatterplot3d)

     scatterplot3d(x=A$x, y=A$y, z=A$dates)

情节看起来像是:

enter image description here

要更正z轴,您可以参考此page

为避免出现这些错误,您可以使用anytime package by Dirk Eddelbuettel

library(anytime)
A$dates <- anytime(A$dates) 

答案 1 :(得分:0)

Masoud向您展示了如何将字符串转换为日期。要在rgl中获得不错的日期标签,您需要自己做一些额外的工作:它对日期一无所知。例如,

 dates = c("2016-11-10","2016-11-20","2016-12-01","2016-12-15","2016-12-30")
 x = rnorm(5,0,1)
 y = rnorm(5,1,2)
 A = data.frame(dates, x, y)


 A$dates = as.Date(A$dates,"%Y-%m-%d")

 ticks <- pretty(A$dates)
 plot3d(A$x, A$y, A$dates, axes = FALSE)
 box3d()
 axes3d(c("x", "y")) # default axis labels here
 axis3d("z", at = ticks, 
   labels = format(ticks, format = "%b %d")) # see ?strptime for formats

我不确定scatterplot3d是否可以使用这些标签。