r 中的 5 维图

编程入门 行业动态 更新时间:2024-10-09 13:25:38
本文介绍了r 中的 5 维图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在 R 中绘制 5 维图.我目前正在使用 rgl 包在 4 维中绘制我的数据,使用 3 个变量作为 x、y、z 坐标,另一个变量作为颜色.我想知道是否可以使用此包添加第五个变量,例如空间中点的大小或形状.这是我的数据示例和我当前的代码:

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")要求(rgl)plot3d(df$var1, df$var2, df$var3, col=as.numeric(df$var4), size=0.5, type='s',xlab="var1",ylab="var2",zlab="var3")

我希望可以做到第五维度.非常感谢,

解决方案

这是一个 ggplot2 选项.我通常回避 3D 绘图,因为它们很难正确解释.我也几乎从未像这里一样在同一个图中放入 5 个连续变量...

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))

虽然这有点乱,但实际上您可以合理地读取大多数点的所有 5 个维度.

如果您的某些变量是分类变量,则可以使用更好的多维绘图方法.如果您的所有变量都是连续的,您可以使用 cut 将其中一些变量转换为分类变量,然后使用 facet_wrap 或 facet_grid 绘制它们.>

例如,这里我将 var3 和 var4 分解为五分位数,并在它们上使用 facet_grid.请注意,我还保留了颜色美学,以强调在大多数情况下,在高维图中将连续变量转换为分类变量足以让关键点越过(在这里您会注意到填充和边框颜色很漂亮在任何给定的网格单元内均匀):

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)

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,

解决方案

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))

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)

更多推荐

r 中的 5 维图

本文发布于:2023-07-18 14:21:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1145660.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:维图

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!