multi paneled graphs: box plot and spagetti plot - r

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!

Related

making a 2x2 plot layout with plots from different platforms

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

multiple plots with ggplots plotted in sequence without using the graphics position

Is there any way to plot multiple graphics using ggplots2 for the graphics that are built in sequence but other scripts are invoked in between?
grid.arrange is practical but requires all plots to be defined. the solution given in the discussion
Combined plot of ggplot2 (Not in a single Plot), using par() or layout() function?
can be used but requires to know the sequence or column of the graphics
print(plot1, vp = vplayout(1, 1))
how is it possible like par() to open the device with the number of graphics to plot and like any function such as plot, plot the graphics without knowing in which position or column we're going to plot?
Hope it's clear.
Carol

R_plotting points in multiple plots

I have two plots and I want to add some additional lines to both plots. Is there a way in R to choose the plot (among the two) to draw the new lines?
Edit1:
Actually I have multiple plots in one window using mfrow
Edit2:
I have edited the the question to include the problem i faced after using mfgin par()
x=1:10
y=seq(10,100,10)
z=seq(100,1000,100)
par(mfrow=c(2,1))
plot(x,y)
abline(a=0,b=10,col="blue")
plot(x,z)
abline(a=0,b=100,col="blue")
which gives
But when I use
x=1:10
y=seq(10,100,10)
z=seq(100,1000,100)
par(mfrow=c(2,1))
plot(x,y)
plot(x,z)
par(mfg=c(1,1))
abline(a=0,b=10,col="blue")
par(mfg=c(2,1))
abline(a=0,b=100,col="blue")
the result is
Note the false behavior of the first abline
Can anyone explain the reason and a solution for this?
Assuming you have multiple graphics windows open, you want to use the dev.cur(), dev.next(), dev.set(), dev.list() functions (see ?dev.cur) to identify the current graphics device and switch among devices.
If on the other hand you have set up multiple plots within a single window via the mfrow or mfcol parameters to par(), you can use par("mfg") to query/set which plot is current.
If you use layout, lattice, ggplot2, or raw grid graphics, I'm not sure.

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.

Resources