-3

Is there any way to produce excel 3d plot in r?

I have a matrix and I would like to plot it in 3d surface format using R. I managed to do it in excel and it gives me this plot https://drive.google.com/open?id=1hNqCguPX_ar8ueSK-u-yHTUYZxdyT_cE

I would like to produce same plot but with R this plot is produced with this data

m<-matrix(c(7,73,195,195,416,625,120,52,178,178,349,454,6,83,164,164,244,0,3,52,150,150,329,330),nrow = 4, ncol = 6, byrow = T)
r2evans
  • 141,215
  • 6
  • 77
  • 149
Alaa
  • 49
  • 4

1 Answers1

2

Unfortunately the dataset in use is not available, so there is no way to faithfully reproduce the image and expand the details.

To create the contour surface, it is interesting to use the ggplot2 package with the geom_raster function. But this will return a continuous surface.

library(ggplot2)
library(dplyr)

x <- seq(1, 6, 1)
y <- seq(0, 3, 1)
grid <- expand.grid(x = x,
                    y = y)
m <- matrix(c(7,73,195,195,416,625,120,52,178,178,349,454,6,
            83,164,164,244,0,3,52,150,150,329,330), 
            nrow = 4, ncol = 6, byrow = T)
m2 <- m %>% 
  t %>% as.data.frame()
grid <- grid %>% 
  dplyr::mutate(response = c(m2$V1, m2$V2, m2$V3, m2$V4))

Palet <- c("royalblue2", "orangered3", "lavenderblush3", "gold3")

ggplot2::ggplot(grid, aes(x, y, z = response)) +
  geom_raster(aes(fill = response)) + 
  scale_fill_gradientn(colours = Palet, limits = c(0, 800)) +
  theme_void() +
  theme(legend.position = "bottom")

enter image description here

In addition, it is possible to perform data manipulation and work with the discrete fill. Where for each interval, a color will be assigned.

grid2 <- grid %>% 
  dplyr::mutate(cor = ifelse(response >= 0 & response < 200, 1,
                             ifelse(response >= 200 & response < 400, 2,
                                    ifelse(response >= 400 & response < 600, 3,
                                           ifelse(response >= 600, 4, "error")))))

ggplot2::ggplot(grid2, aes(x, y, z = response)) +
  geom_raster(aes(fill = cor)) + 
  scale_fill_manual(values = Palet, labels = c("0-200", "200-400",
                                               "400-600", "600-800"),
                    name = "") +
  theme_void() +
  theme(legend.position = "bottom")

enter image description here


However, if it is considered that in matrix m each cell is intensity, it is possible to generate a 3D plot.

library(plot3D)
persp3D(z = m, theta = 60)

enter image description here

3D surface plot from 2D matrix

Plot 3D data in R

Impressive package for 3D and 4D graph - R software and data visualization

bbiasi
  • 1,549
  • 2
  • 15
  • 31
  • Thanks for help, however it is still very far from plot made by Microsoft excel (attached) – Alaa Jun 01 '19 at 16:09
  • @Alaa see this: [link1](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), [link2](https://meta.stackexchange.com/questions/173399/how-to-upvote-on-stack-overflow). Thanks. – bbiasi Jun 12 '19 at 13:25