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
Related
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.
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.
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!
I want to use grid.table() in an existing plot in R. However I can't locate this table on the right side of my chart. So the thing is:
First of all, I make an histogram of my data:
hist(as.numeric(unlist((vels[counts]))),freq=F,
col="gray",border="black",ylim=c(0,0.15),
xlab=paste(names(vels)[counts]),
main=paste("Weibull fitting",names(vels[counts])))
After that, I have implemented a function that plots in an existing chart the Weibull curve giving both parameters A and K:
plot_weibull(K_value,A_value)
And finally I want to place a data.frame using the grid.table() because it shows the cells in a very pretty form, and you can use italic and bold text in cells.
grid.table(round(values,3),cex=0.75,show.rownames=T,
show.colnames=T,show.hlines=T)
The problem is that this table appears in the center of the device in front of the histogram and the curve, and I want it to be in the right side.
After all, I would like to know a tool that clicking on the graph, I would receive the area under my Weibull curve.
The hist function is base graphics and the grid.table function is grid graphics. The 2 graphics systems do not play nicely together without extra effort (as you have noticed).
The easiest fix is to use the addtable2plot function from the plotrix package rather than grid.table. It may not look the same but it would be simple.
Another option is to use a grid graphics function to create the histogram, such as something from the lattice or ggplot2 packages (both can do histograms), then create a specific viewport using grid graphics functions and use grid.table to put the table into that viewport.
Last, if you really want to mix them then see the gridBase package for ways to mix grid and base graphics.
I searched for this and found that with {grid} there are ways to rotate an image, and that for some plots you can play with their rotation (for example plot(x,y) instead of plot(y,x)).
However, I want to know if there is a generic method to rotate a plot in R (one that would work for ANY plot generated in base graphics) ?
you could export the graphic, read it back in, and display it rotated as a rasterGrob, say, (or a rasterImage after rotating the matrix, or a grImport grob if you want vector paths)
plot(1:10, rnorm(10))
library(grid)
cap <- grid.cap()
grid.newpage()
grid.raster(cap, vp=viewport(angle=30))
The new gridGraphics package may now be a better alternative.
Note: this doesn't seem to work with Rstudio's graphics device, presumably they haven't implemented grid.cap.
It's kind of possible via the gridGraphics package, although it feels a bit rough on the edges (the examples in ?grid.echo don't all work for me),
plot(1:10, rnorm(10))
library(gridGraphics)
grab_grob <- function(){
grid.echo()
grid.grab()
}
g <- grab_grob()
grid.newpage()
pushViewport(viewport(width=0.7,angle=30))
grid.draw(g)
I'm reasonably certain that there isn't a way with base graphics itself to do this generically. There is however the gridBase package which allows one to mix base graphics and grid graphics in a 'plot'. The vignette for the package has a section on embedding base graphics in grid viewports, so you might look there to see if you can cook up a grid wrapper around your plots and use grid to do the rotation. Not sure if this is a viable route but is, as far as I know, the on only potential route to an answer to your Q.
gridBase is on CRAN and the author is Paul Murrell, the author of the grid package.
After browsing the vignette, I note one of the bullets in the Problems and Limitations section on page, which states that it is not possible to embed base graphics into a rotated grid viewport. So I guess you are out of luck.
Spinning 3D Scatterplots
You can also create an interactive 3D scatterplot using the plot3D(x, y, z) function in the rgl package. It creates a spinning 3D scatterplot that can be rotated with the mouse. The first three arguments are the x, y, and z numeric vectors representing points. col= and size= control the color and size of the points respectively.
# Spinning 3d Scatterplot
library(rgl)
plot3d(wt, disp, mpg, col="red", size=3)
A function rotate_plot to be used like
rotate_plot(some_base_plot(x, y, ...))
isn't possible because most of the base plot don't return a value.
Some of the plots contain a horiz argument to allow you to choose which way round you want the plot drawing. Take a look at barplot.default to see how to implement this. (Warning: it's messy.)
#ucfagls's suggestion to use gridBase is your best bet. There are some examples of its use in Appendix B of Murrell's R Graphics.
Given that its possible to write your own plot functions using base graphics, I can't see how a single solution could exist. Is what you want really just a way to rep lace x data with y data? What exactly do you mean by "rotate"?
Yes it is possible to rotate the the plot in R
by using the function Coord flip() in r
you can flip the graph from horizontal to vertical and from vertical to horizontal.