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

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.

Related

Specify plot width and height with ggplot and grid.arrange

I would like to combine different plots in ggplot with grid.arrange. I have data from two different groups that performed a different number tasks in an experiment. One group performed 2 tasks and the other group 4 tasks. I would like to plot the median response times of each group per task, and then combine these plots with the help of grid.arrange (via the command grid.arrange(plot1,plot2, ncol=2)) into one image (see image). The problem is now that the width of the left plot in the image is not the same as the width at the right plot in the image. Is there a way to specify the width of the plots in either ggplot or grid.arrange? I know this might be possible with the "cowplot" package in R but some reason I cannot install this package into my version (version 3.2.2 of Rstudio).
These are the two plots

creating multiple file types while plotting

I would like to produce a series of plots in both high-resolution and low-resolution versions, or stated differently using two different file types (.png and .eps). I'd like to know the best/least repetetive way to do this. I am using the gplot function in sna, and the plot has a custom legend outside the plot area. I wrote a function something like this:
library(sna)
plotfun <- function(net){
png("test.png",width=800)
p <- gplot(net)
par(xpd=T)
legend(max(p[,1])+1,max(p[,2]),legend=letters[1:10],title="custom legend")
dev.off()
seteps()
postscript(test.eps)
#repeat all the plotting commands, which are much longer in real life
dev.off()
}
#try it with some random data
plotfun(rgraph(10))
This is perfectly functional but seems inefficient and clumsy. The more general version of this question is: if for any reason I want to create a plot (including extra layers like my custom legend), store it as an object, and then plot it later, is there a way to do this? Incidentally, this question didn't seem sna specific to me at first, but in trying to reproduce the problem using a similar function with plot, I couldn't get the legend to appear correctly, so this solution to the outside-the-plot-area legend doesn't seem general.
I would recommend generate graphs only in Postscript/PDF from R and then generate bitmaps (e.g. PNG) from the Postscript/PDF using e.g. ImageMagick with -density parameter (http://www.imagemagick.org/script/command-line-options.php#density) set appropriately to get desired resolution. For example
convert -density 100 -quality 100 picture.pdf picture.png
assuming picture.pdf is 7in-by-7in (R defaults) will give you a 700x700 png picture.
With this approach you will not have to worry that the picture comes out formatted differently depending which R device (pdf() vs png()) is used.

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?

R, ggplot2, size of plot area

I use R and ggplot2 to produce graphs for my thesis. I can port them to tikz objects using the tikzdevice or to a pdf using the pdf device very easily, and in each case, specifying the width and height of the overall plot is straightforward.
However, I am actually more interested in specifying the width of the plot AREA (ie the inner box), since differences in this (particularly for plots on the same page) are more easily detected by the eye, even if they are a couple of points different in the final document.
Of course, the source of this issue can be easily put down to the axis labels that vary depending on the content.
My question is, How can I define 'fixed' axis label widths as a global option, or define the width to be exported as the inner plotting area for ggplot2 objects....

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!

Resources