3

I have a following dataset:

group<-c(rep("X1",5),rep("X3",5))
set.seed(1)
value<-c(seq(0.2, 1, .2),seq(10, 30, 5))
time<-c("2018-07-01 00:00:00","2018-07-01 01:00:00","2018-07-01 02:00:00","2018-07-01 03:00:00","2018-07-01 04:00:00",
        "2018-07-01 00:00:00","2018-07-01 01:00:00","2018-07-01 03:00:00","2018-07-01 04:00:00","2018-07-01 05:00:00")
order<-c(1,2,3,4,5,1,2,4,5,6)
country<-c("HU","ZA","XX","ZZ","RO","HU","ZA","XX","ZZ","RO")
dat <-data.frame(time,country,group,value,order)

I want to plot a 3d plot where x=order, Y=value, z=hour(time). I read here that z should have dimension [x,y]. 3d Surface Plot in R with plotly 4 Something like this one should be obtained:

enter image description here

How can I plot this, because do not understand how can I create a matrix for Z with this dimension?

0

1 Answer 1

2

Your data doesn't seem suited for 3D. You can't have two values for z with the same x and y-axis. If you filter out one of the groups you can technically make it work, but I think some other chart would work better. Here's an example of how you could make it work with one group and an example of a plot that may work better.

library(plotly)

group<-c(rep("X1",5),rep("X3",5))
set.seed(1)
value<-c(seq(0.2, 1, .2),seq(10, 30, 5))
time<-c("2018-07-01 00:00:00","2018-07-01 01:00:00","2018-07-01 02:00:00","2018-07-01 03:00:00","2018-07-01 04:00:00",
        "2018-07-01 00:00:00","2018-07-01 01:00:00","2018-07-01 03:00:00","2018-07-01 04:00:00","2018-07-01 05:00:00")
order<-c(1,2,3,4,5,1,2,4,5,6)
country<-c("HU","ZA","XX","ZZ","RO","HU","ZA","XX","ZZ","RO")
dat <-data.frame(time,country,group,value,order)

library(tidyverse)
m = dat %>%
  filter(group == "X1") %>% 
  pivot_wider(c(time,value,order), names_from = time,
                    values_from = value) %>% 
  select(-order) %>%
  as.matrix
m[] = case_when(is.na(m) ~ 0, TRUE ~ m)
plot_ly(z = ~m) %>% 
  add_surface()

plot_ly(data = dat, x = ~time, y = ~value, color = ~group)

This is an example of data that is better suited

m = matrix(rnorm(25),nrow = 5, ncol = 5)

plot_ly(z = ~m) %>% 
  add_surface()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! could you also i.e. create a sample data, which produces the same plot with the same axis's as in my picture (numbers can be random)? I.e. If I have hourly data for X variables (numeric) for 5 years (say there is only one group). How can I plot this surface plot where X=values, Y=years, Z=Months ? (should I group by months and years and aggregate by mean(values) in order to plot it?
I would aggregate by month. Look into how to do that with lubridate. This sample data switches months and values, but is basically what you would want to do and how I would label it. library(plotly) library(tidyverse) dat = matrix(rnorm(24*60),ncol=24,nrow = 60) plot_ly(z = ~dat) %>% add_surface() %>% layout(scene = list(xaxis = list(title = "hours"), yaxis = list(title = "months"), zaxis = list(title = "value")))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.