3D histogram using ggplot2

Asked

Viewed 279 times

6

I have data of a two-dimensional distribution, for example, uniform. I wish to make a histogram with this data. I tried the package plot3D, but it wasn’t very nice.

teste = matrix(runif(100), ncol = 10) 

plot3D::hist3D(z = teste, bty = "g", phi = 15,  theta = -15,
                   xlab = "X1", ylab = "X2", zlab = "Relative frequency", main = "Teste",
                   col = NULL, border = "black", shade = 0.8, curtain = T, plot = T,
                   ticktype = "detailed", space = 0.15, d = 2, cex.axis = 1e-9, image = T, contour = T)

1 answer

5


In your case I’d make a chart like this:

library(ggplot2)
library(tidyr)
library(dplyr)
teste %>% data.frame() %>%
  mutate(x = 1:10) %>%
  gather(y, z, -x) %>%
  mutate(y = y %>% gsub("X", "", .) %>% as.numeric()) %>%
  ggplot(aes(x, y)) +
  geom_raster(aes(fill = z))

inserir a descrição da imagem aqui

In it you have the same information as in 3d graphic (below) however (in my view) is much easier to see. In fact, I don’t think any 3D graphics would look good.

inserir a descrição da imagem aqui

  • 1

    I agree that your suggestion is better than the 3D graph. By the way, this type of graph is called heatmap.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.