13

I am trying to plot a 5 dimensional plot in R. I am currently using the rgl package to plot my data in 4 dimensions, using 3 variables as the x,y,z, coordinates, another variable as the color. I am wondering if I can add a fifth variable using this package, like for example the size or the shape of the points in the space. Here's an example of my data, and my current code:

set.seed(1)
df <- data.frame(replicate(4,sample(1:200,1000,rep=TRUE)))
addme <- data.frame(replicate(1,sample(0:1,1000,rep=TRUE)))
df <- cbind(df,addme)
colnames(df) <- c("var1","var2","var3","var4","var5")
require(rgl)
plot3d(df$var1, df$var2, df$var3, col=as.numeric(df$var4), size=0.5, type='s',xlab="var1",ylab="var2",zlab="var3")

I hope it is possible to do the 5th dimension. Many thanks,

Error404
  • 6,959
  • 16
  • 45
  • 58
  • you could make your size parameter take on values depending on the var4 column, somthing like `size=df$var4 / 100`. Depending on the range of your data, you would want to change that 100 to something else. Is this what you are looking for? – JPC Apr 21 '14 at 14:35
  • Using size in a pseudo 3D plot is going to be very challenging as a lot of the depth perception is conveyed by size. – BrodieG Apr 21 '14 at 14:38
  • Hi @JPC, it actually worked! I was trying the same command and it was giving me a big white ball instead. Thanks. – Error404 Apr 21 '14 at 14:39
  • @BrodieG, that's a fair point. I usually don't plot 3D (and find it hard to interpret), so perhaps unless it is incredibly necessary, perhaps avoiding a 3D plot would be ideal. – JPC Apr 21 '14 at 14:40
  • @BrodieG, I am happy to apply a different suggestion, like changing the shape of points instead. is that possible? Sorry I am totally new to multi-dimensional plotting. – Error404 Apr 21 '14 at 14:40
  • 1
    If size mucks things up, you might experiment with passing parameters to `material3d` via `...` arguments, or even modifying your colormap so the RGB depends on `var4` but the transparency depends on `var5` – Carl Witthoft Apr 21 '14 at 14:42
  • @CarlWitthoft, I found an interesting option that might do the trick for me this time using material3d(). The var5 I have is a binary (0,1) variable. If I make the 0 as wireframe and the 1 as the filled spheres that would do it. unfortunately I couldn't apply the right syntax. Would you please show me how to write the command with the material3d()? I have edited the question to make "var5" as a binary variable. Cheers. – Error404 Apr 21 '14 at 15:01
  • 1
    Sorry - I've never actually used `material3d` . You'll have to experiment with some small dataset to see how to pass the parameters. – Carl Witthoft Apr 21 '14 at 17:01
  • You're alright, I will look it up an post it as an answer here if I found the syntax. Thank you :) – Error404 Apr 21 '14 at 17:27

1 Answers1

29

Here is a ggplot2 option. I usually shy away from 3D plots as they are hard to interpret properly. I also almost never put in 5 continuous variables in the same plot as I have here...

ggplot(df, aes(x=var1, y=var2, fill=var3, color=var4, size=var5^2)) +
  geom_point(shape=21) +
  scale_color_gradient(low="red", high="green") +
  scale_size_continuous(range=c(1,12))

enter image description here

While this is a bit messy, you can actually reasonably read all 5 dimensions for most points.

A better approach to multi-dimensional plotting opens up if some of your variables are categorical. If all your variables are continuous, you can turn some of them to categorical with cut and then use facet_wrap or facet_grid to plot those.

For example, here I break up var3 and var4 into quintiles and use facet_grid on them. Note that I also keep the color aesthetics as well to highlight that most of the time turning a continuous variable to categorical in high dimensional plots is good enough to get the key points across (here you'll notice that the fill and border colors are pretty uniform within any given grid cell):

df$var4.cat <- cut(df$var4, quantile(df$var4, (0:5)/5), include.lowest=T)
df$var3.cat <- cut(df$var3, quantile(df$var3, (0:5)/5), include.lowest=T)

ggplot(df, aes(x=var1, y=var2, fill=var3, color=var4, size=var5^2)) +
  geom_point(shape=21) +
  scale_color_gradient(low="red", high="green") +
  scale_size_continuous(range=c(1,12)) +
  facet_grid(var3.cat ~ var4.cat)

enter image description here

BrodieG
  • 51,669
  • 9
  • 93
  • 146
  • 1
    Nice! I frequently deal with graphing the same measurements for multiple subsets and `facet wrap/grid` are godsends. – JPC Apr 21 '14 at 14:52
  • 1
    That's a very good answer actually. I am still not very good at the ggplot2 package yet, but this is very helpful. Appreciated. – Error404 Apr 21 '14 at 14:54
  • @JPC, yes, facetting is a godsend. I can't believe I spent most of my life not knowing about it. – BrodieG Apr 21 '14 at 14:57
  • I was first introduced to it when using `lattice`, but it was only after being introduced to `ggplot2` and the (much more understandable IMO) logic of its graphing that I fully appreciated. – JPC Apr 21 '14 at 14:58
  • 1
    Astonishing answer BrodieG. I am sure many people will thank you for this! – Error404 Apr 21 '14 at 15:07