如何指定时间范围并在Plotly 3D散点图上以小时为单位给出颜色值?

时间:2017-07-05 10:48:05

标签: r date colors plotly scatter3d

我正在尝试创建一个3D散点图

  • X轴=时间
  • Y轴=长度
  • Z轴=重量

我需要在一天中的每个小时都有一个单独的颜色,并且颜色必须形成一个完整的圆圈,这意味着第23和00小时必须具有相似的颜色。我有一个标签分隔的txt文件,其中包含类似的数据,字段为时间戳,长度和重量。

2017-05-13 00:00:00 1853    0
2017-05-13 01:00:00 2008    0
2017-05-13 02:00:00 2008    2
2017-05-13 03:00:00 2008    23
2017-05-13 04:00:00 2008    118
2017-05-13 05:00:00 2008    180
2017-05-13 06:00:00 2008    240
2017-05-13 07:00:00 977 406
2017-05-13 08:00:00 605 516
2017-05-13 09:00:00 613 564
2017-05-13 10:00:00 613 595
2017-05-13 11:00:00 1615    595
2017-05-13 12:00:00 1040    571
2017-05-13 13:00:00 619 613
2017-05-13 14:00:00 1540    535
2017-05-13 15:00:00 1998    439
2017-05-13 16:00:00 433 267
2017-05-13 17:00:00 4   178
2017-05-13 18:00:00 4   91
2017-05-13 19:00:00 4   36
2017-05-13 20:00:00 4   0
2017-05-13 21:00:00 8   0
2017-05-13 22:00:00 5   0
2017-05-13 23:00:00 786 0
2017-05-14 00:00:00 1859    0

I have made it to the point where I have a 3D scatterplot with the points being in the right places with right colors, but the time scale on the X-axis is off. It shouldn't start from the year 1970.

我用于此

的代码
# Read file
working_directory = "~/Desktop"
data = read.table(file = paste0(working_directory,"/","data.txt"),
                  sep = '\t',
                  col.names = c("timestamp", "length", "weight"),
                  header = F)

# Convert column 'timestamp' data to timestamp
data$timestamp = as.POSIXct(data$timestamp, format="%Y-%m-%d %H:%M:%S")

# Get maximum length and weight
maximum_length = max(data$length)
maximum_weight = max(data$weight)

color_wheel <- c("#ef0000", "#f56b00", "#fca000", "#ffcd00", "#fff500", "#d8de00",
            "#9ec700", "#4caf0f", "#009825", "#009b71", "#009e9f", "#00a1c5",
            "#00a3e7", "#008dd0", "#0072b6", "#00509a", "#000c7a", "#6d087c",
            "#a1007e", "#cb0080", "#f00082", "#ef0070", "#ef005a", "#ef003a")

library(plotly)
p <- plot_ly (type = 'scatter3d' , data, x = ~timestamp, y = ~length , z = ~weight,
              color = as.POSIXlt(data[1]$timestamp)$hour, colors = color_wheel, mode = 'markers') %>%
  layout(scene = list(xaxis = list(title = 'Time'),
                      yaxis = list(title = 'Length', range = c(0, maximum_length)),
                      zaxis = list(title = 'Weight', range = c(0, maximum_weight))))
print(p)

我需要时间轴在第一次观察的第一个小时开始。 如您所见,我已经指定了长度和重量的范围,但没有指定时间。我试过xaxis = list(title = 'Time’, range = c(min(data$timestamp), max(data$timestamp))) 但它不起作用。图表完全消失了,我能看到的只是情节的传说。

如何指定时间范围,以便X轴在正确的时间开始和结束?

0 个答案:

没有答案