5 dimensional plot in r

前端 未结 1 1263
花落未央
花落未央 2021-01-31 00:36

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, an

1条回答
  •  囚心锁ツ
    2021-01-31 01:17

    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

    0 讨论(0)
提交回复
热议问题