Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make 3D plots with categorical data in R?

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
like image 550
wishihadabettername Avatar asked Jul 25 '10 01:07

wishihadabettername


People also ask

Can you make 3D plots in R?

3D plot in R Language is used to add title, change viewing direction, and add color and shade to the plot. The persp() function which is used to create 3D surfaces in perspective view. This function will draw perspective plots of a surface over the x–y plane.

How do you visualize 2 categorical variables in R?

The categorical variables can be easily visualized with the help of mosaic plot. In a mosaic plot, we can have one or more categorical variables and the plot is created based on the frequency of each category in the variables. To create a mosaic plot in base R, we can use mosaicplot function.

Can dot plots have categorical data?

A dot plot is a graphical display used in statistics that uses dots to represent data. Dot plots can be used for univariate data; that is, data with only one variable that is being measured. Dot plots are useful when the variable is categorical or quantitative.


1 Answers

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()
like image 86
JoFrhwld Avatar answered Oct 05 '22 05:10

JoFrhwld