In R, how to prevent blank page in pdf when using gridBase to embed subplot inside plot - r

As explained here, it is easy to embed a plot into an existing one thanks to gridBase, even though both plots use the base graphics system of R. However, when saving the whole figure into a pdf, the first page is always blank. How to prevent this?
Here is an example:
require(gridBase)
## generate dummy data
set.seed(1859)
x <- 1:100
y <- x + rnorm(100, sd=5)
ols <- lm(y ~ x)
pdf("test.pdf")
## draw the first plot
plot.new() # blank page also happens when using grid.newpage()
pushViewport(viewport())
plot(x, y)
## draw the second plot, embedded into the first one
pushViewport(viewport(x=.75,y=.35,width=.2,height=.2,just=c("center","center")))
par(plt=gridPLT(), new=TRUE)
hist(ols$residuals, main="", xlab="", ylab="")
popViewport(2)
dev.off()

I think it's a bit of a hack but setting onefile=FALSE worked on my machine:
pdf("test.pdf", onefile=FALSE)
In searching for an answer (which I didn't really find so much as stumbled upon in the forest) I came across this post to Rhelp from Paul Murrell who admits that mixing grid and base graphics is confusing even to the Master.

A work around solution I found was to initiate the pdf file inside the for loop; then insert an if clause to assess whether the first iteration is being run. When the current iteration is the first one, go ahead and create the output device using pdf(). Put the dev.off() after closing the for loop. An quick example follows:
for(i in 1:5){
if (i == 1) pdf(file = "test.pdf")
plot(rnorm(50, i, i), main = i)}
dev.off()

Related

How to save several plots from an own function into a list in R?

I have created one function that contains two types of plots and it will give you one image. However, the title of this image will change depending on one list, so, you will have several plots but different title.
(The original function will change the numbers that the plot uses but, in essence, is what I need).
This is the example that I have created.
list_genes <- c("GEN1", "GEN2", "GEN3")
myfunction <- function(x,y){
for(gene in list_genes){
# This to draw both plots
par(mfrow=c(2,1))
plot(x,y, main=paste0("Plot of ", gene))
hist(x, main=paste0("Plot of ", gene))
}
}
myfunction(x=c(1,5,6,2,4),y=c(6,10,53,1,5))
Since the list has 3 elements, we get 3 plots.
However, as I need to create a presentation with all the plots that I generate, I found this post with a solution to create slides for several plots inside a for loop. It is what I want, but for that, I need to save my plots into a list/variable.
object <- myfunction(x=c(1,5,6,2,4),y=c(6,10,53,1,5))
> object
NULL
I found this post (which gives you an interesting solution) but, the plots still cannot be saved into an object.
calling_myfunc <- function(){
myfunction(x=c(1,5,6,2,4),y=c(6,10,53,1,5))
}
calling_myfunc()
object <- calling_myfunc()
> object
NULL
My final objective is to create a presentation (automatically) with all of the plots that I generate from my function. As I saw in this post. But I need to save the plots into a variable.
Could anyone help me with this?
Thanks very much in advance
Although I couldn't find the way to save the plots into an object, I found a way to create a presentation with those images thanks to this post and the export package.
library(export)
list_genes <- c("GEN1", "GEN2", "GEN3")
myfunction <- function(x,y){
for(gene in list_genes){
# This to draw both plots
par(mfrow=c(2,1))
plot(x,y, main=paste0("Plot of ", gene))
hist(x, main=paste0("Plot of ", gene))
graph2ppt(file="plots.pptx", width=6, height=5,append=TRUE) } }
myfunction(x=c(1,5,6,2,4),y=c(6,10,53,1,5))
Of course, the width and height of the plots can be changed or put them as a parameter in the function.
Since the package is not available in CRAN for my current R version (4.1.2), I downloaded it from GitHub:
devtools::install_github("tomwenseleers/export")
In addition, I have found another package that I can use for the same purpose (although it adds one extra slide at the beginning, I don't know why)
library(eoffice)
list_genes <- c("GEN1", "GEN2", "GEN3")
myfunction <- function(x,y){
for(gene in list_genes){
# This to draw both plots
par(mfrow=c(2,1))
plot(x,y, main=paste0("Plot of ", gene))
hist(x, main=paste0("Plot of ", gene))
topptx(file="plots.pptx", width=6, height=5,append=TRUE)
}
}
myfunction(x=c(1,5,6,2,4),y=c(6,10,53,1,5))
PS: I found the solution to create a presentation --> How to create a presentation in R with several plots obtained by a function?

How to overlay two lines using the -plot- function in R

I am using R version 3.6.0. I am trying to overlay 3 lines on a single plot. I have done this successfully in the past using identical code and for some reason it doesn't seem to be working. I have the following RWE:
y.l <- function(x){0.024 - 0.00004*x + 0.00001*(10-16.8764)}
y.a <- function(x){0.024 - 0.00004*x}
y.h <- function(x){0.024 - 0.00004*x + 0.00001*(20-16.8764)}
png("yplot.png")
yplot <- plot(y.l(1:800),
type="l", lty=2,
xlab="x", ylab="y", main="Getting better :/",
ylim=c(-0.025,0.025))
lines(1:800, lty=1, y.a(1:800))
lines(1:800, lty=3, y.h(1:800))
dev.off()
which produces the following plot:
For some reason it is ignoring the extra -lines()- code. Is there some obvious mistake I am overlooking after staring at a computer all day? I have done this exact same thing before and I cannot for the life of me figure it out. And yes I have expanded the y-axis to see if they were hiding above or below and they aren't.
Your 3 lines are plotted, it is because of your function that you can't see them.
Here the output of your three function:
> head(y.a(1:800))
[1] 0.02396 0.02392 0.02388 0.02384 0.02380 0.02376
> head(y.h(1:800))
[1] 0.02399124 0.02395124 0.02391124 0.02387124 0.02383124 0.02379124
> head(y.l(1:800))
[1] 0.02389124 0.02385124 0.02381124 0.02377124 0.02373124 0.02369124
You can see that your three function give almost the same results, it because of your 0.00001*(10-16.8764) that's is basically to small to modify your output.
If you zoom enough on the plot:
yplot <- plot(y.l(1:800),
type="l", lty=2,
xlab="x", ylab="y", main="Getting better :/",
ylim=c(.023,0.024),
xlim=c(0,30))
lines(1:800, lty=1, y.a(1:800))
lines(1:800, lty=3, y.h(1:800))
You can see the three lines:
I think you need to change the last parameter of your function if you want to see a dramatic difference between your lines.

plot multiple figures interactively with split.screen and hold one constant

I am using R to plot a multifgure image with interactive capability in one of them. I'm using split.screen to do this (as I tried unsuccesfuly to do it right with par() and base R graphics.
My goal is to hold the display of each screen(1) jth plot, while the screen(2) plot figure updates interactively k times..
But even though I set erase = FALSE or TRUE, each time it iterates through the subloop, the first plot is cleared and displays a blank image. Any ideas on how to hold the first plot image, each time the sub loop, k updates its graphics?
split.screen(figs=c(1,2),erase=TRUE)
for(j in 1:5){
screen(1)
plot(rnorm(3))
screen(2)
for(k in 1:5){
plot(rnorm(3))
par(ask=TRUE)
}
}
update from help.
These functions are totally incompatible with the other mechanisms for arranging plots on a device: par(mfrow), par(mfcol)'and'layout().
I guess that explains the par(ask=TRUE). I'm still interested if anyone can do this using par() and base graphics functions.
I couldn't find any par() variables that would explicitly specify one of the two screens to plot on at any step, while allowing par(ask=T) to update in a subloop, which is what split.screen() was able to do.
Give Hmisc::subplot a try:
## draw first plot
par(mfcol=c(1,2), xpd=NA)
plot(rnorm(3))
plot(1,1) # dummy plot
## update subplot k times
for(k in 1:5) {
rect(grconvertX(par("fin")[1], from="inches"),
grconvertY(0, from="inches"),
grconvertX(par("fin")[1]*2, from="inches"),
grconvertY(par("fin")[2], from="inches"), col="white", border=NA) ## cover-up
subplot( plot(rnorm(3)), 1, 1, size=par("pin") )
readline("Hit <Return> to see next plot: ")
}
Adding a flush.console() and Sys.sleep() should do it.
split.screen(figs=c(1,2),erase=TRUE)
for(j in 1:5){
screen(1)
plot(rnorm(3))
flush.console()
Sys.sleep(0.5)
for(k in 1:5){
screen(2)
plot(rnorm(3))
par(ask=TRUE)
flush.console()
Sys.sleep(0.5)
}
}
That will still suppress the par(ask=TRUE) as per the warnings, but it should prevent the first plot going blank whilst the second iterates.

Multiple plots with high-level plotting functions, especially plot.rqs()

I am trying to plot two regression summaries side-by-side with one centered title. Each regression summary is generated by plot.rqs() and amounts to a set of 9 plots.
I've tried using par(mfrow=c(1,2)) already, but as I learnt from Paul Murrel's (2006) book, high-level functions like plot.rqs() or pairs() save the graphics state before drawing and then restore the graphics state once completed, so that pre-emptive calls to par() or layout() can't help me. plot.rqs() doesn't have a 'panel' function either.
It seems that the only way to achieve the result is to modify the plot.rqs() function to get a new function, say modified.plot.rqs(), and then run
par(mfrow=c(1,2))
modified.plot.rqs(summary(fit1))
modified.plot.rqs(summary(fit2))
par(mfrow=c(1,1))
From there I might be able to work out how to add an overall title to the image using layout(). Does anyone know how to create a modified.plot.rqs() function that could be used in this way?
Thanks
You can patch a function as follows:
use dput and capture.output to retrieve
the code of the function, as a string;
change it as you want (here, I just replace each occurrence of par
with a function that does nothing);
finally evaluate the result to produce a new function.
library(quantreg)
a <- capture.output(dput(plot.summary.rqs))
b <- gsub("^\\s*par\\(", "nop(", a)
nop <- function(...) {}
my.plot.summary.rqs <- eval(parse(text=b))
First we generate an example object, fm . Then we copy plot.rqs and use trace on the copy to insert par <- list at top effectively nullifying any use of par within the function. Then we do the same with plot.summary.rqs. Finally we test it out with our own par:
library(quantreg)
example(plot.rqs) # fm to use in example
# plot.rqs
plot.rqs <- quantreg::plot.rqs
trace("plot.rqs", quote(par <- list), print = FALSE)
# plot.summary.rqs
plot.summary.rqs <- quantreg::plot.summary.rqs
trace("plot.summary.rqs", quote(par <- list), print = FALSE)
# test it out
op <- par(mfrow = c(2, 2))
plot(summary(fm))
plot(fm)
title("My Plots", outer = TRUE, line = -1)
par(op)
EDIT: added plot.summary.rqs.

Graphic of binary variable in R

I would like to plot a simple graphic. I have a dat set with n rowns and k columns, in which each row has a a sequence of 0 and 1. I would like to plot exactly this sequence for all rows.
Actually I want to reproduce the figure 24.1, p. 516, of Gelman and Hill's book (Data aAnalysis Using Regression and Multilevel/Hierarchical Models). I suspect that he made the graphic in Latex, but it seems quite ridiculous that I'm not able to repplicate this simple graphic in R. The figue is something like this. As you can see from the link, the "ones" are replaced by "S" and "zeros" by ".". It's a simple graphic, but it shows each individual response by time.
I would go with a formatted text output using sprintf. Much cleaner and simpler. If you still want a plot, you could go with the following:
Given matrix tbl containing your data:
tbl <- matrix(data=rep(0:1,25), nrow=5)
You can generate a plot as:
plot(1, 1, xlim=c(1,dim(tbl)[2]+.5), ylim=c(0.5,dim(tbl)[1]), type="n")
lapply(1:dim(tbl)[1], function(x) {
text(x=c(1:dim(tbl)[2]), y=rep(x,dim(tbl)[2]), labels=tbl[x,])
})
Using this as a base you can play around with the text and plot args to stylize the plot the way you wish.
Here are two possible solutions, based on fake data generated with this helper function:
generate.data <- function(rate=.3, dim=c(25,25)) {
tmp <- rep(".", prod(dim))
tmp[sample(1:prod(dim), ceiling(prod(dim)*rate))] <- "S"
m <- matrix(tmp, nr=dim[1], nc=dim[2])
return(m)
}
Text-based output
x <- generate.data()
rownames(x) <- colnames(x) <- 1:25
capture.output(as.table(x), file="res.txt")
The file res.txt include a pretty-printed version of the console output; you can convert it to pdf using any txt to pdf converter (I use the one from PDFlib). Here is a screenshot of the text file:
Image-based output
First, here is the plotting function I used:
make.table <- function(x, labels=NULL) {
# x = matrix
# labels = list of labels for x and y
coord.xy <- expand.grid(x=1:nrow(x), y=1:ncol(x))
opar <- par(mar=rep(1,4), las=1)
plot.new()
plot.window(xlim=c(0, ncol(x)), ylim=c(0, nrow(x)))
text(coord.xy$x, coord.xy$y, c(x), adj=c(0,1))
if (!is.null(labels)) {
mtext(labels[[1]], side=3, line=-1, at=seq(1, ncol(x)), cex=.8)
mtext(labels[[2]], side=2, line=-1, at=seq(1, nrow(x)), cex=.8, padj=1)
}
par(opar)
}
Then I call it as
make.table(x, list(1:25, 1:25))
and here is the result (save it as png, pdf, jpg, or whatever).
As far as I can see, this is a text table. I am wondering why you want to make it a graph? Anyway, quick solutions are (either way)
make the text table (by programming or typing) and make its screenshot and embed the image into the plot.
make a blank plot and put the text on the plot by programming R with "text" function. For more info on "text", refer to http://cran.r-project.org/doc/contrib/Lemon-kickstart/kr_adtxt.html

Resources