0
votes

I am new to R, and try to get a visual representation of a year collected temperature data from a server rack. The data is already prepared and loaded in the data object in the R environment in this format:

> str(data)
'data.frame':   516496 obs. of  5 variables:
 $ Day         : POSIXct, format: "2017-06-29" "2017-06-29" "2017-06-29" "2017-06-29" ...
 $ SecondsOfDay: int  0 60 120 180 240 300 360 420 480 540 ...
 $ Temp        : num  34.8 34.7 34.7 34.7 34.7 ...

The field Day contains just the date, the field SecondsOfDay is the second of the day, rounded to full minutes. The field Temp contains the actual temperature value.

I would like to create a 3D plot of this data, where the X-axis is the date, and the Y-axis the time of the day. The Z-axis should be the temperature.

Ideally, I would like to use one of the basic functions like persp to create a graph like this (but with correct Z values):

enter image description here

The core of my problem, why I could not find a own solution yet, is because the examples for the plot use a function and range sequences as input. I do not understand how to prepare the data from my table into the correct format for the plot functions.

What is a simple way to generate a plot as described above?

1
plotly can be a nice tool for a 3d surface - see plot.ly/r/3d-surface-plots if you have the data frame the plot would be something like p<-plot_ly(x=data.frame$Date, y=data.frame$SecondsOfDay, z=data.frame$Temp) %>% add_surface() then to see the chart print(p)John Walker
I'd rather look into rgl because it's not-proprietary.jay.sf

1 Answers

1
votes

No fancy shading, but this should give you an idea

d <- rep(1:5, each=10)
m <- rep((0:9)*144, 5)
v <- sin(seq(0, 1, length.out=50)*2*pi*5) + (1:50)/50

temperature <- matrix(v, ncol=10, byrow=TRUE)

par(mar=c(2, 2, 2, 2))

day <- unique(d)
tod <- unique(m)/60

persp(day, tod, temperature, theta=50, phi=20,
  ticktype="detailed", ylab="Time of day (hr)")

enter image description here

Essentially you spread out the temperature values in a matrix with dimensions taken from the date and minute vectors.
I can never quite remember whether it's byrow=TRUE or FALSE, and what vector decides which dimension, so I just check by eye and change things up until it looks right.

A couple of other options

library(plot3D) # an extension of persp()
persp3D(day, tod, temperature, theta=50, phi=20)

library(rgl) # also similar to persp(), but interactive
persp3d(day, tod, temperature, col=cm.colors(100))