【问题标题】:How can I see dates plot3D in R instead of 5 - digit integers如何在 R 中查看日期 plot3D 而不是 5 位整数
【发布时间】:2021-06-03 15:52:47
【问题描述】:

我在 R-studio 中使用 library(rgl) 包制作 3d 图。如图所示,几乎所有看起来都不错

日期格式除外。无论我尝试做什么,它都不会显示日期来代替整数。这是我用来生成图的代码:

 # Plot
Date <- as.Date(df$Date)

df["Color"] <- NA 
mycolors <- c('royalblue')
df$Color <- mycolors

par(mar=c(0,0,0,0))
plot3d(
  x = Date, y = C, z = L,
  col = df$Color,
  type = 'p',
  size = 2,
  xlab="Date", ylab="C", zlab="L")
filename <- writeWebGL(dir = file.path(folder, coln),
                       width=600, height=600, reuse = TRUE)

我尝试了一些方法来获取轴上的日期,例如:

Date1 = as.character(df$Date)
Date = as.Date(Date1, format = "%Y%m%d")
Date = format(as.Date(df$Date, origin="1970-01-01"))
Date <- seq(as.Date("2016-02-29", format = "%Y-%m-%d"), by ="days", length = length(df$Date), origin = "1970-01-01")

但是没有任何效果。我总是得到代表日期的整数,例如

as.Date(17000, "1970-01-01")
1 "2016-07-18"

谁能知道如何解决这个问题?我将不胜感激。

【问题讨论】:

  • @Ben,我看了,但对我没有帮助例如,这些答案中最简单的,anytime(),也给出了整数,但很大,例如 1.6e+09,可能是秒。此外,我对 2D 绘图没有这个问题,plot(),ggplot2(),
  • 您能否提供一个示例数据来重现此问题?

标签: r rgl plot3d


【解决方案1】:

rgl 包不使用与基本图形相同的格式化函数,因此您需要自己格式化值。例如,

df <- data.frame(Date = seq(as.Date("2016-02-29"), as.Date("2019-01-01"), length=100),
                 y = rnorm(100),
                 z = rnorm(100))
library(rgl)
plot3d(df, axes = FALSE)
ticks <- pretty(df$Date, 3)
labels <- format(ticks, format = "%Y-%m")
axis3d("x", at = ticks, labels = labels) # Custom on x axis
axes3d(c("y", "z"))                      # Defaults on other axes 
box3d()

reprex package (v0.3.0) 于 2021-03-05 创建

pretty() 的结果可能会超出数据范围,因此您可能需要减少它,或者更改您的xlim 以避免轴超出范围,如2016-01 的这张图片中所示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多