如何在 R 中使用分类数据制作 3D 绘图?

发布于 2024-09-11 15:07:58 字数 784 浏览 9 评论 0原文

我一直在尝试根据分类数据创建 3D 条形图,但尚未找到方法。

解释起来很简单。考虑以下示例数据(真实的示例更复杂,但它会简化为这样),显示了按收入和年龄(均为分类数据)细分的某些情况的相对风险。

我想在 3D 条形图中显示它(类似于 http: //demos.devexpress.com/aspxperiencedemos/NavBar/Images/Charts/ManhattanBar.jpg)。我查看了 scatterplot3d 包,但它仅适用于散点图,不能很好地处理分类数据。我能够制作 3d 图表,但它显示点而不是 3d 条形。没有适合我需要的图表类型。我也尝试过 rgl 包,但也没有运气。我已经谷歌搜索了一个多小时了,还没有找到解决方案。我也有一本 ggplot2 - Elegant Graphics for Data Analysis 这本书,但 ggplot2 没有这种图表。

还有其他我可以使用的免费软件应用程序吗? OpenOffice 3.2 也没有这个图表。

感谢您的任何提示。

Age,Income,Risk
young,high,1
young,medium,1.2
young,low,1.36
adult,high,1
adult,medium,1.12
adult,low,1.23
old,high,1
old,medium,1.03
old,low,1.11

I've been trying to create a 3D bar plot based on categorical data, but have not found a way.

It is simple to explain. Consider the following example data (the real example is more complex, but it reduces to this), showing the relative risk of incurring something broken down by income and age, both categorical data.

I want to display this in a 3D bar plot (similar in idea to http://demos.devexpress.com/aspxperiencedemos/NavBar/Images/Charts/ManhattanBar.jpg). I looked at the scatterplot3d package, but it's only for scatter plots and doesn't handle categorical data well. I was able to make a 3d chart, but it shows dots instead of 3d bars. There is no chart type for what I need. I've also tried the rgl package, but no luck either. I've been googling for more than an hour now and haven't found a solution. I have a copy of the ggplot2 - Elegant Graphics for Data Analysis book as well, but ggplot2 doesn't have this kind of chart.

Is there another freeware app I could use? OpenOffice 3.2 doesn't have this chart either.

Thank you for any hints.

Age,Income,Risk
young,high,1
young,medium,1.2
young,low,1.36
adult,high,1
adult,medium,1.12
adult,low,1.23
old,high,1
old,medium,1.03
old,low,1.11

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小猫一只 2024-09-18 15:07:58

我不确定如何在 R 中制作 3d 图表,但还有其他比 3d 条形图更好的方法来表示这些数据。 3D 图表使解释变得困难,因为条形的高度会因 3D 视角而倾斜。在该示例图表中,很难判断 2004 年的威斯康星州是否真的高于 2001 年的威斯康星州,或者这是否是视角的影响。如果它更高,那么会高多少?

由于 AgeIncome 都有有意义的顺序,因此制作折线图不会很糟糕。 ggplot2 代码:

ggplot(data, aes(Age, Risk, color = Income))+
  geom_line(aes(group = Income))

或者,您可以制作热图。

ggplot(data, aes(Age, Income, fill = Risk)) +
  geom_tile()

I'm not sure how to make a 3d chart in R, but there are other, better ways to represent this data than with a 3d bar chart. 3d charts make interpretation difficult, because the heights of the bars and then skewed by the 3d perspective. In that example chart, it's hard to tell if Wisconsin in 2004 is really higher than Wisconsin 2001, or if that's an effect of the perspective. And if it is higher, how much so?

Since both Age and Income have meaningful orders, it wouldn't be awful to make a line graph. ggplot2 code:

ggplot(data, aes(Age, Risk, color = Income))+
  geom_line(aes(group = Income))

Or, you could make a heatmap.

ggplot(data, aes(Age, Income, fill = Risk)) +
  geom_tile()
幻梦 2024-09-18 15:07:58

就像其他人建议的那样,有更好的方法来呈现这一点,但是如果您想要与您拥有的类似的东西,这应该可以帮助您开始。

df <-  read.csv(textConnection("Age,Income,Risk
young,high,1
young,medium,1.2
young,low,1.36
adult,high,1
adult,medium,1.12
adult,low,1.23
old,high,1
old,medium,1.03
old,low,1.11
"))
df$Age <- ordered(df$Age, levels=c('young', 'adult', 'old'))
df$Income <- ordered(df$Income, levels=c('low', 'medium', 'high'))
library(rgl)
plot3d(Risk ~ Age|Income, type='h', lwd=10, col=rainbow(3))

这只会产生平面矩形。有关创建美观条形图的示例,请参阅demo(hist3d)

Like the others suggested there are better ways to present this, but this should get you started if you want something similar to what you had.

df <-  read.csv(textConnection("Age,Income,Risk
young,high,1
young,medium,1.2
young,low,1.36
adult,high,1
adult,medium,1.12
adult,low,1.23
old,high,1
old,medium,1.03
old,low,1.11
"))
df$Age <- ordered(df$Age, levels=c('young', 'adult', 'old'))
df$Income <- ordered(df$Income, levels=c('low', 'medium', 'high'))
library(rgl)
plot3d(Risk ~ Age|Income, type='h', lwd=10, col=rainbow(3))

This will just produce flat rectangles. For an example to create nice looking bars, see demo(hist3d).

小霸王臭丫头 2024-09-18 15:07:58

您可以在此处找到一个起点,但您需要添加更多内容线条和一些矩形以获得像您发布的那样的图。

You can find a starting point here but you need to add in more lines and some rectangles to get a plot like you posted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文