making a 2x2 plot layout with plots from different platforms - r

I am trying to make a 2x2 layout of four plots. I have two plots from ggplot(i.e., ggbiplot) and two plots from heatmap2. My dream was to put them all together in one page. I have followed Claus Wilkes cowplot tutorials. They work fine with ggplot-style plots and base plots (i.e., plots make with the plot command). However, moving to heatmap2 making the desired layout does not result in any 2x2 plot, only single plots.
Appreciate suggestions.
Thanks,
jahn

Related

R: Lattice Plots/Wireframe : Splitting Panel Plots into single plots

How can I force wireframe panels to produce single plots instead of one panel plot/grid plot? The reason is that if I have to produce a Sweave/ Pdf File the original wireframe plot, which R produces and which you can see in my other post
Faceted Lattice Plots in R, e.g., wireframes: How to remove strips and add 1-Line subtitles.
will look very small, especially if I have many many single wireframe plots. I can handle single plots more easily in Sweave.
Lattice allows you to specify the number of columns and rows for the plots which then spill over onto adjacent pages if a multi-page device is used:
pdf("nine.pdf", onefile=TRUE, paper="special")
wireframe(pred~Sepal.Width+Petal.Width|interaction(Species,Petal.Length),
pd, drape=FALSE,scale=list(arrows=FALSE), subset=(Species=="setosa"),
layout=c(1,1,9))
dev.off()
On the console device they create new plots which stack up in the plot device and you can "scroll-back" with keystrokes that may vary depending on your unstated OS. The eps format is accessible using directions in ?ps.

multiple plots in r, a, b, c, d with lines and curves

I have some pretty nice plots that I want to present in a report, but I would like to have multiple plots with one common legend, title and caption. The problem is the lines in the plots. So far I have made a plot and added lines or curves from one or several binomial models. These curves/lines exist in different data.frames, but they can fit in the same plot because they have the same parameters and lengths.
I have tried to make a plot where I add all the lines at the same time as the plot is being created but then only the lines show up and i got a lot of errors.
So when i create a plot with lines i do like this:
plot(Spruce,Leaves,xlab="Spruce",ylab="Leaves>0")
g=glm(Leaves>0~Spruce,family=binomial,data=1)
g2=glm(Leaves>0~Spruce,family=binomial,data=2)
curve(predict(g,data.frame("Spruce"=x),type="resp"),add=TRUE,col="blue")
curve(predict(g2,data.frame("Spruce"=x),type="resp"),add=TRUE,col="red")
I cannot upload a picture of the plot, but it's nice.
The problem is that I need to present it with several other plots to explain my point and then I would like to do it as one of several a,b,c,d plots.
I have used
par(mfrow=c(2,2))
To have all plots visual at the same time but I would like to name them a,b,c,d and have a common legend, title and mtext.
Any suggestions how to do that?

multi paneled graphs: box plot and spagetti plot

I'm trying to create multiple graphs on one panel. I'm hoping for a box plot on both sides of a spaghetti plot.
This is an example of my code:
par(mfrow=c(1,3))
boxplot(h~y,dat,
xlab="Y",
ylab="Incidence 1 (percent)",
main="H",
scales=list(x=list(at=c(1,2))))
xyplot(H~Yr,groups=Subject,type="b",data=data,
ylab="Incidence (percent)",
xlab="Year",
main="Incidence",
scales=list(x=list(at=c(1,2))))
boxplot(h1~y1,dat1,
xlab="Y",
ylab="Incidence 2 (percent)",
main="R",
scales=list(x=list(at=c(1,2))))
When I plot my first box plot things look ok (there is still empty space ready to be filled), but once the spaghetti plot is added in, the whole graph is the spaghetti plot (the box plot is erased).
Is there a way to do multiple but different types of graphs on one panel?
As #DWin and #mnel point out, you are having trouble because you are trying to mix base graphics (boxplot()) and grid-based graphics (xyplot()). To get two boxplots and a spaghetti plot in a single figure, you have three main options. The first two will be much easier than the third:
Use just base graphics (here boxplot() and plot( , type="b")), arranging them in a single figure with par(mfrow=c(1,3).
Use just grid-based graphics (here the lattice functions bwplot() and xyplot( , type="b")), arranging them in a single figure with grid.arrange() from the gridExtra package.
Use a mixture of base and grid-based graphics (like you're trying to do now), combining them in a single figure with functions from the gridBase package.
The only thing to be said for option 3 is that pursuing it will teach you a lot about the low-level implementation of both the base and grid graphical systems!

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.

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