How do you plot a 3d scatterplot with multiple facets? - r

I'm trying to plot a 3d scatterplot (let's use this previous question as an example), but as a grid with multiple 3D scatterplots on the same page based on a categorical factor. I see how many people can do this with, for example, boxplots, but have no idea how to do so with a 3-d scatterplot. Any thoughts would be very helpful.

You can do this with the cloud function in the lattice package, although it probably doesn't offer as much easy fine-scale control as scatterplot3d- or rgl-based plots:
set.seed(101)
d <- data.frame(x=runif(1000),y=runif(1000),z=runif(1000),
f=factor(sample(1:10,replace=TRUE,size=1000)))
library("lattice")
cloud(z~x*y|f,data=d)

Related

How do I plot a scatterplot graph with a repeated-measures variable and a continuous variable in r?

I have a four levels repeated measures variable (let's call it RM) and I have a continuous variable predictor (let's call it C).
I want to plot a scatterplot graph with C on the X-Axis and RM on the Y-Axis with different lines within the plot for each level of RM.
Is this possible to do with ggplot or a similar package?
Thanks in advance!
Utilizing ggplot2, you should be able to achieve this type of graphical output. Viewing a portion of your data that you wish to plot would be beneficial to provide a sufficient answer.
I have attached a link to a summary of ggplot2 graphical functions here. This link provides some background on ggplot2 and the components necessary to create a graph. Three components are needed in ggplot2 to make a graph, the data, the ggplot2 function, and mapping your variables with the aesthetic.
Because you don't have a representation of some of your data, providing a sufficient answer is difficult, but it might look something like this.
ggplot(data=yourdata, aes(x= C(continuous_variable, y = RM(repeated_measures)) +
geom_point()
You may also map geom_line for each RM variable in addition to this example. Hope this helps!

R statistics: Drawing multiple plots in RGL

Does anyone know how to draw multiple 3d plots in one picture using RGL in R Statistics.
I have three variables and each of those variables belong to two groups. I want each group to have a different color so I can visualize it. In regular R stats, I just use subset and then use par(new=T). I haven't seen anything equivalent for the 3d plot. Does anyone have any suggestion?
Thanks!
try plot3d(x, y, z, add=TRUE)
Admittedly I was a bit surprised when it worked, I thought it would throw an error on the first plot, but i guess it creates an existing plot if none exists and otherwise adds the points to the existing plot

R: How to overlay pie charts on 'dots' in a scatterplot in R

Using R I would like to replace the points in a 2d scatter plot by a pie chart displaying additional values.
The rational behind is that I have time series data for hundreds of elements (proteins) derived from a biological experiment monitored for 4 conditions. I would like to plot the elements (categorial data) on the y axis and occurrence of a event in time on the x axis. To visualize the relative occurrence between the 4 conditions I would like to visualize this in form of a pie chart or doughnut chart overplayed onto the respective point in the scatter plot.
The overall data density is low so overlapping won't be an issue.
Is this possible in R?
I was thinking of using a manual scale in ggplot2 but could not figure out how to define a pie chart as a scale.
Also of interest would be how to best cluster this data and sort it accordingly.
Yes. pieGlyph() is one ready-to-go function from the Rgraphviz package.
Also, I would check out this Q/A for how to do things like this more generally:
How to fill a single 'pch' point on the plot with two-colours?
Especially check out ?my.symbols from the TeachingDemos package.
Lastly, in regards to ggplot2, you should check out this blog post about possible upcoming features:
http://blog.revolutionanalytics.com/2011/10/ggplot2-for-big-data.html
See also Paul Murrell. Integrating grid graphics output with base graphics output. R News, 3(2):7-12, October 2003. http://www.r-project.org/doc/Rnews/Rnews_2003-2.pdf
The code on pp 10-11 sets up the main plot axes, labels and legend, and then opens a series of smaller windows centered at each individual point on the plot and plots a small graph in each window. I've tried pie charts, mosaics and barplots, but the method is not limited to these types.

Can I use shingles from lattice in ggplot2 in R

It is possible to use the shingles to define specific ranges in ggplot2. As far as i understand shingles are a way to generate groups. Can we create such shingles and use them in ggplot2 facet_grid to obtain graphs?
Following up from the comments, ggplot can't draw shingles (in the way lattice draws shingles with special indicators in the strip) and by default doesn't have a means of producing the overlapping groups.
However, I cam across this excellent PDF document which aims to produce a gpplot2 version of every figure in Depayan's excellent Lattice book (Lattice: Multivariate Data Visualization with R).
Page 31 contains a custom function fn() which replicates the behaviour of equal.count(), as far as I can tell, to provide the correct data structure to plot with overlapping shingles. The PDF contains plenty of examples of "shingles" in ggplot that you can play with.
So not sure if this answers the Q - but at least it appears one can fudge ggplot into producing plots that use the shingle concept.

How does one plot a 3D stacked histogram in R?

I want to plot stacked histograms in R; i.e. stack individual histograms in the third dimension.
thank you all for your suggestions, especially the one by Shane.
#hadley, I agree with your points, however, my situation is different: the main point I'm trying to convey by plotting four stacked histograms is that the tails vary significantly....the part that will get obscured is of no consequence in the data I'm presenting....also, being able to read the frequency axis is also not important since I'll be plotting the relative frequencies...
One doesn't. This is a terrible display of data because the front histograms obscure the rear histograms and the perspective makes it just about impossible to read the values off the y-axis.
You could try using either rgl (see here) or 3dscatterplot (as in this example). Lattice also supports this:
library(lattice)
library(latticeExtra)
?panel.3dbars
You can see an example of this on the Learnr blog.
I don't believe that's technically a stacked histogram (a stacked histogram stacks the bars on top of each other). Moreover, a different kind of histogram could be more informative: look at the ggplot2 the documentation here for some examples.
hist_cut <- ggplot(diamonds, aes(x=price, fill=cut))
hist_cut + geom_bar() # defaults to stacking
Another option is to use latticing instead, with facet_wrap in ggplot2 (see this post as an example).

Resources