Export graphics from R in vector format - r

I'd like to export graphics from R (for example, histograms and ROC curves) in vector format in order to make layout edits in a graphic design program like Intaglio. Is this possible?

pdf("myfile.pdf")
# graph goes here
dev.off()
Other devices (besides PDF) exist as well. (#Justin points out what is probably the most useful one in a comment).

Related

Modify R Graphics in CRAN Contributed Packages

I like using the R package qcc. This is a great package for the quality control professional. The package generates a lot of cool graphics. I know how to modify basic graphs in R with the par() command.
The graphics in the qcc package are somewhat unique and I don't always know what elements make up the graphics. How do I determine what elements make up the graphics so I can modify them with the par() commands and arguments. Take this simple cause and effect diagram in the code below. How would I go about modifying line colors, line thicknesses, font, etc? I have no idea how the author constructed this when they developed the package.
library(qcc)
cause.and.effect(cause=list(Measurements=c("Micrometers", "Microscopes", "Inspectors"),
Materials=c("Alloys", "Lubricants", "Suppliers"),
Personnel=c("Shifts", "Supervisors", "Training", "Operators"),
Environment=c("Condensation", "Moisture"),
Methods=c("Brake", "Engager", "Angle"),
Machines=c("Speed", "Lathes", "Bits", "Sockets")),
effect="Surface Flaws")
The following capability analysis should be a little more familiar to the normal R user. How do I modify the three vertical red lines in this graphic? Perhaps I want to use a different color, different line style/thickness, etc. And again, how do I determine what elements make up the graphic so I can modify any particular part of it if I so wish?
library(qcc)
data(pistonrings)
attach(pistonrings)
diameter <- qcc.groups(diameter, sample)
q <- qcc(diameter[1:25,], type="xbar", nsigmas=3, plot=FALSE)
process.capability(q, spec.limits=c(73.95,74.05))

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.

How to save an object through GGally in R

I have a pretty dumb question to ask everyone.
I am using ggpairs under GGally to create a correlation matrix, and somehow I found that GGally did not provide a saving function as ggplot2 did. The function ggsave did not work for a non-ggplot2 object. I tried to use pdf or png, but they did not work. I am wondering if there's an easy to save this picture to a local file? Thank you for your kind help.
While #CMichael's comment is nice (I didn't know that, hence +1), it's applicable only if you want to save a particular plot from GGally-generated plot matrix. I believe that you'd like to save the whole plot matrix - the need, which I've recently also experienced. Therefore, you can use a standard R approach and save the graphics by opening corresponding (to desired format) graphical device, printing the object and closing the device, which will effectively save the graphics in a desired format.
# use pdf() instead of svg(), if you want PDF output
svg("myPlotMatrix.svg", height = 7, width = 7)
g <- ggpairs(...)
print(g)
dev.off()

Placing grid tables in R

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.

Repeat plot command with minor changes in R

I made a plot in R and I want to repeat all the commands (like plot(), legend() or line()) that were carried out for this plot, with some minor changes. For example I want to set the axes to logarithmic scale and change the title of the plot.
In gnuplot I would use the replot command.
plot ...
set title "The same plot with logarithmic axes"
set logscale
replot
Is something like this possible in R. The only thing that comes to my mind of doing this (besides changing the values manually and re-run the lines of codes) would be setting up a function, that asks for all parameters that might be changed by the user.
Thanks for your help,
Sven
R uses a pen and paper graphics model - once the plot has been drawn on the device that is it. If you want to change some aspect of the plot, you need to replay the graphics function calls that produce the plot with the changes made to the code.
Depending on what you are really doing there are two options:
If this is just for you, write code in a text editor / IDE that knows R and can send chunks of code at a time to R. That way the code to produce the figure is recorded in a separate script which you can paste into/send to R making the changes you need each time to the script.
If you are going to be doing this often, then write yourself a wrapper plotting function that encapsulates the plot code you want but allows you to pass in arguments to alter the aspects you want.
Lattice and ggplot2 are a little different as they are based on grid graphics and create objects that when printed produce a plot on the device. One can manipulate that object to alter what is drawn, and with grid one can push and pop things on to / off a viewport.

Resources