22

I Know this sounds basic, but have a been searching for literally more than an hour now without success. I'm simply trying to plot a 3D bar plot in 'R' using the 'ggplot2' package. My dataframe looks something like this:

 x   y     z
t1   5   high
t1   2   low
t1   4   med
t2   8   high
t2   1   low
t2   3   med
t3  50   high
t3  12   med
t3  35   low

and I want to plot something like this on it: enter image description here

Any help is more than appreciated!!

Tavi
  • 2,668
  • 11
  • 27
  • 41
  • 14
    You should realize that R aficionados think of Excel as a rich source of examples of how _NOT_ to do things (properly) and that 3D barplots are one of the most deprecated strategies for communication of accurate quantitative information. – IRTFM Nov 07 '14 at 04:52
  • @BondedDust ha! yes, i do realise that. probably the reason why i cannot find any examples at all. but i still want to try, and any pointers from you will help… i just need to know how to add a third axis to my `geom_bar()` – Tavi Nov 07 '14 at 04:55
  • This may be an avenue worth investigating: http://stackoverflow.com/questions/23261760/how-to-generate-3-d-bar-graph-in-r – Docconcoct Nov 07 '14 at 04:56
  • @Docconcoct thanks, but all the responses are for 2d plots!! – Tavi Nov 07 '14 at 04:59
  • True. Just thought it was worth consideration. – Docconcoct Nov 07 '14 at 05:01
  • 2
    From the author of rgl: https://stat.ethz.ch/pipermail/r-help/2007-September/141578.html and from @hadley: http://vita.had.co.nz/papers/prodplots.pdf – IRTFM Nov 07 '14 at 05:02
  • 7
    I don't think you can make a 3D barplot with `ggplot2`. Like exceeding the speed of light in our universe, there are just some things that are forbidden by the fundamental laws of the Hadleyverse. In any case, a line plot grouped by color will be much more informative. For example, with the data you listed above try this (where I've named the data frame dat): `ggplot(dat, aes(x, y, group=z, colour=z)) + geom_line() + geom_point()`. – eipi10 Nov 07 '14 at 06:00
  • @eipi10 Could not agree more. This comment of yours is actually worth posting as a decent answer IMO. – tonytonov Nov 07 '14 at 07:15
  • 2
    Does it have to be `ggplot`? (If not, take a peek at the examples in `?latticeExtra::panel.3dbars`. – jbaums Nov 07 '14 at 07:36
  • @jbaums thank u J I'll check it now. No it doesn't have to be ggplot. Anything I can do in R would be just fine :) – Tavi Nov 08 '14 at 14:35
  • @eipi10 thank you I'll try that in a second and post it as answer possibly;) – Tavi Nov 08 '14 at 14:36
  • @tonytonov good to know I haven't tried it just yet as I've been away but will do in a second!! – Tavi Nov 08 '14 at 14:37

2 Answers2

24

As mentioned in comments, 3D plots usually aren't a good choice (when other options are available) since they tend to give a distorted/obscured view of data.

That said, here's how you can plot your data as desired with latticeExtra:

d <- read.table(text=' x   y     z
t1   5   high
t1   2   low
t1   4   med
t2   8   high
t2   1   low
t2   3   med
t3  50   high
t3  12   med
t3  35   low', header=TRUE)

library(latticeExtra)

cloud(y~x+z, d, panel.3d.cloud=panel.3dbars, col.facet='grey', 
      xbase=0.4, ybase=0.4, scales=list(arrows=FALSE, col=1), 
      par.settings = list(axis.line = list(col = "transparent")))

enter image description here

jbaums
  • 27,115
  • 5
  • 79
  • 119
0

The {rayshader} package is another package that allows 3D visualisation - according to the author, it will only allow 3d visualisation for data that has indeed three dimension. Here based on a tile plot.

NB: the below required for me to install current GitHub versions of both the rayshader (v.0.33.3) and rgl (v.1.0.0) packages.

# devtools::install_github("tylermorganwall/rayshader") ## NB this will install a lot of dependencies
library(rayshader)
library(ggplot2)
d <- read.table(text=' x   y     z
t1   5   high
t1   2   low
t1   4   med
t2   8   high
t2   1   low
t2   3   med
t3  50   high
t3  12   med
t3  35   low', header=TRUE)

p <- ggplot(d, aes(x, z, fill = y)) +
  geom_tile() +
  scale_fill_fermenter(type = "div", palette = "RdYlBu")

plot_gg(p)
render_movie(filename = "plot.gif")

enter image description here

tjebo
  • 21,977
  • 7
  • 58
  • 94