Add image (png file) to header of pdf file created with R - r

I am trying to add an .png image (logo) to the header of my pdf report of graphs created with ggplot and printed to pdf.
I found the following example how to add a image to a ggplot plot. But, I am looking to add the .png image to he header of the pdf which is outside the ggplot area.
#-------------------------------------------------------------------------------
# Example png file
#-------------------------------------------------------------------------------
library(reshape2)
library(png)
mypngfile = download.file('http://api.altmetric.com/donut/502878_64x64.png',
destfile = 'mypng.png', mode = 'wb')
mypng = readPNG('mypng.png')
#-------------------------------------------------------------------------------
# create example plot using mtcars data frame from ggplot
#-------------------------------------------------------------------------------
library(ggplot2)
p.example = qplot(mpg, wt, data = mtcars) +
annotation_raster(mypng, ymin = 4.5, ymax= 5, xmin = 30, xmax = 35)
#-------------------------------------------------------------------------------
# print to pdf file with footnote
#-------------------------------------------------------------------------------
fname = "C:/temp/my report.pdf"
pdf(fname, 10.75, 6.5, onefile=TRUE, paper="a4r")
print(p.example)
dev.off()
...which produces a pdf that looks like this:
But, I would like the image to show up outside the ggplot area. Or more specifically, I want the image to show up in the report header (in upper left) like the following example:
I found the following function that can be used to create a text footnote but wasn't sure how to modify it to insert a .png image.
makeFootnote <- function(footnoteText= format(Sys.time(), "%d %b %Y"),
size= .4, color= grey(.5))
{
require(grid)
pushViewport(viewport())
grid.text(label= footnoteText ,
x = unit(1,"npc") - unit(12, "mm"),
y = unit(0.1, "mm"),
just=c("right", "bottom"),
gp=gpar(cex= size, col=color))
popViewport()
}
Any assistance would be greatly appreciated.

here's a suggestion,
library(ggplot2)
p.example = qplot(mpg, wt, data = mtcars)
library(grid)
library(gtable)
ann <- rasterGrob(mypng, width=unit(1,"cm"), x = unit(0.5,"cm"))
g <- ggplotGrob(p.example)
g <- gtable_add_rows(g, grobHeight(ann), 0)
g <- gtable_add_grob(g, ann, t=1, l=4)
grid.newpage()
grid.draw(g)

Related

plot xtable or save it as image

I want to use xtable layouts in my powerpoint presentations and I need a way to directly convert a data.frame into a picture or plot of an xtable such as the ones displayed here.
The ideal solution would give me a ggplot object as I have the most flexibility from there, but I can work with another output as long as I can see a xtable in a pic (raster or vector) or plot window.
Using ggplot2 and grid.extra we can achieve decently looking tables:
library(ggplot2)
library(gridExtra)
ggplot() + annotation_custom(tableGrob(head(iris))) + theme_void()
tableGrob has a theme parameter that allows enough flexibility to reproduce something close to xtable. see this vignette.
Here's a convenient function to do a bit more, you'll need packages grid and gtable :
table_plot <- function(x,
theme_fun= gridExtra::ttheme_minimal,
base_size = 12,
base_colour = "black",
base_family = "",
parse = FALSE,
padding = unit(c(4, 4), "mm"),
col = base_colour,
lwd=1,
lty=1
){
g <- gridExtra::tableGrob(x,
theme = theme_fun(base_size, base_colour, base_family, parse, padding))
separators <- replicate(ncol(g) - 2,
grid::segmentsGrob(x1 = unit(0, "npc"), gp=gpar(col=col,lwd=lwd,lty=lty)),
simplify=FALSE)
g <- gtable::gtable_add_grob(g, grobs = separators,
t = 2, b = nrow(g), l = seq_len(ncol(g)-2)+2)
ggplot2::ggplot() + ggplot2::annotation_custom(g) + ggplot2::theme_void()
}
simple example:
table_plot(head(iris))
complicated example :
iris2 <- setNames(head(iris)[1:3],c("alpha*integral(xdx,a,infinity)", "this text\nis high", 'alpha/beta'))
table_plot(iris2,
base_size=10,
base_colour="darkred",
base_family = "serif",
parse=TRUE,
theme=ttheme_default,
lwd=3,
lty=2,
col="darkblue")

How to have a clickable picture in a PDF created using pictureGrob?

I create a simple plot and have a picture actually an SVG icon as follows:
library(ggplot2); library(grid); library(gridExtra)
facebookGrob <- gTree(children=gList(pictureGrob(readPicture("inst/svg/facebook2.svg"))))
p1 <- ggplot() +
ggplot2::annotation_custom(facebookGrob, xmin=1.8, xmax=3.2, ymin=-0.6, ymax=1)
final <- arrangeGrob(p1,...,)
ggsave(filename='output.pdf',plot=final,...)
Is there any way to generate a clickable link on top of this SVG icon in the final PDF?
the tikzDevice package lets you insert hyperref links as nodes,
library(tikzDevice)
tikz("annotation.tex",width=4,height=4, standAlone = TRUE,
packages = c(getOption('tikzLatexPackages'),
"\\usepackage{hyperref}",
"\\usetikzlibrary{positioning}")
)
tg <- tikzNodeGrob(x = 0.5, y = 0.5, name = 'google',
content='\\href{http://www.google.com}{\\includegraphics[width=1in]{google.png}}',
units = "native")
qplot(1:10, 1:10) +
annotation_custom(grob = tg, xmin=3,xmax=3,ymin=5,ymax=5)
dev.off()

Rasterise ggplot images in R for tikzdevice

I use R to analyse data, ggplot to create plots, tikzDevice to print them and finally latex to create a report. THe problem is that large plots with many points fail due to the memory limit of latex. I found here https://github.com/yihui/tikzDevice/issues/103 a solution that rasterises the plot before printing the tikz file, which allows printing the points and the text individually.
require(png)
require(ggplot2)
require(tikzDevice)
## generate data
n=1000000; x=rnorm(n); y=rnorm(n)
## first try primitive
tikz("test.tex",standAlone=TRUE)
plot(x,y)
dev.off()
## fails due to memory
system("pdflatex test.tex")
## rasterise points first
png("inner.png",width=8,height=6,units="in",res=300,bg="transparent")
par(mar=c(0,0,0,0))
plot.new(); plot.window(range(x), range(y))
usr <- par("usr")
points(x,y)
dev.off()
# create tikz file with rasterised points
im <- readPNG("inner.png",native=TRUE)
tikz("test.tex",7,6,standAlone=TRUE)
plot.new()
plot.window(usr[1:2],usr[3:4],xaxs="i",yaxs="i")
rasterImage(im, usr[1],usr[3],usr[2],usr[4])
axis(1); axis(2); box(); title(xlab="x",ylab="y")
dev.off()
## this works
system("pdflatex test.tex")
## now with ggplot
p <- ggplot(data.frame(x=x, y=y), aes(x=x, y=y)) + geom_point()
## what here?
In this example the first pdflatex fails. The second succeeds due to the rasterisation.
How can I apply this using ggplot?
here's a proof-of-principle to illustrate the steps that would be involved. As pointed out in the comments it's not recommendable or practical, but could be the basis of a lower-level implementation.
require(png)
require(ggplot2)
require(tikzDevice)
n=100;
d <- data.frame(x=rnorm(n), y=rnorm(n), z=rnorm(n))
p <- ggplot(d, aes(x=x, y=y, colour=z, size=z, alpha=x)) + geom_point()
## draw the layer by itself on a png file
library(grid)
g <- ggplotGrob(p)
# grid.newpage()
gg <- g$grobs[[6]]$children[[3]]
gg$vp <- viewport() # don't ask me
tmp <- tempfile(fileext = "png")
png(tmp, width=10, height=4, bg = "transparent", res = 30, units = "in")
grid.draw(gg)
dev.off()
## import it as a raster layer
rl <- readPNG(tmp, native = TRUE)
unlink(tmp)
## add it to a plot - note that the positions match,
## but the size can be off unless one ensures that the panel has the same size and aspect ratio
ggplot(d, aes(x=x, y=y)) + geom_point(shape="+", colour="red") +
annotation_custom(rasterGrob(rl, width = unit(1,"npc"), height=unit(1,"npc"))) +
geom_point(aes(size=z), shape=1, colour="red", show.legend = FALSE)
## to illustrate the practical use, we use a blank layer to train the scales
## and set the panel size to match the png file
pf <- ggplot(d, aes(x=x, y=y)) + geom_blank() +
annotation_custom(rasterGrob(rl, width = unit(1,"npc"), height=unit(1,"npc"), interpolate = FALSE))
tikz("test.tex", standAlone=TRUE)
grid.draw(egg::set_panel_size(pf, width=unit(10, "cm"), height=unit(4, "cm")))
dev.off()
system("lualatex test.tex")
system("open test.pdf")
we can zoom in and check that the text is vector-based while the layer is (here low-res for demonstration) raster.
ok, I will write it here because it was too big for the comment box. Instead of adding the rasterised points to a nw plot with new scales you can actually replace the original grob with the rasterised grob by
g$grobs[[6]]$children[[3]] <- rasterGrob(rl). The problem is that it doesn't scale, so you have to know the size of the final image before. Then you can sue sth like this:
rasterise <- function(ggp,
width = 6,
height = 3,
res.raster = 300,
raster.id= c(4,3),
file = ""){
## RASTERISE
require(grid)
require(png)
## draw the layer by itself on a png file
gb <- ggplot_build(ggp)
gt <- ggplot_gtable(gb)
## calculate widths
h <- as.numeric(convertUnit(sum(gt$heights), unitTo="in"))
w <- as.numeric(convertUnit(sum(gt$widths) , unitTo="in"))
w.raster <- width-w
h.raster <- height-h
## print points as png
grid.newpage()
gg <- gt$grobs[[raster.id[1]]]$children[[raster.id[2]]]
gg$vp <- viewport() # don't ask me
tmp <- tempfile(fileext = "png")
png(tmp, width=w.raster, height=h.raster, bg = "transparent", res = res.raster, units = "in")
grid.draw(gg)
dev.off()
## import it as a raster layer
points <- readPNG(tmp, native = TRUE)
points <- rasterGrob(points, width = w.raster, height = h.raster, default.units = "in")
unlink(tmp)
## ADD TO PLOT
gt$grobs[[raster.id[1]]]$children[[raster.id[2]]] <- points
## PLOT TMP
### HERE YOU CAN ONLY PRINT IT IN THIS DIMENSIONS!
pdf(file, width = width, height = height)
grid.draw(gt)
dev.off()
}
And then use it with
data <- data.frame(x = rnorm(1000), y = rnorm(1000))
plot <- ggplot(data, aes(x = x, y = y)) +
geom_point() +
annotate("text", x = 2, y = 2, label = "annotation")
rasterise(ggp = plot,
width = 6,
height = 3,
res.raster = 10,
raster.id = c(4,2),
file = "~/test.pdf")
The problem remains the ID of the grob you want to rasterise. I didn't figure out a good way to find the correct one automatically. It depends on which layers you add to the plot.

sanitize lattice axis labels for tikz device

I'm trying to knit a lattice contourplot into a PDF document using knitr and the tikz device but am getting an Error in getMetricsFromLaTeX(TeXMetrics) on compile. Here's the minimal reproducible example:
\documentclass{article}
\begin{document}
<<contourplot,dev='tikz',echo=FALSE>>=
library(lattice)
library(RCurl)
x <- getURL("https://dl.dropboxusercontent.com/u/3966900/likelihoods.csv")
likelihoods <- read.csv(text=x)
cutoffs <- c(- Inf,-2700,-1497,-1486.6,-1486.3,-1486.286,-1486.28513,-1486.285082,-1486.28508033,-1486.285080237, Inf)
contourplot(ll ~ var1*var2,
data = likelihoods,
scales = list(y = list(log = 10)),
at=cutoffs,
label.style='align',
labels=as.character(cutoffs),
xlab='$\\\\\\sigma$')
#
\end{document}
The whole thing works if I remove the scales line (I assume it's the ^ in the axis labels that trips tikz up?) but looks like shit.
It also works if I add sanitize=TRUE to the chunk options and remove two backslashes from the xlab string. In this case, however, the axis label also gets sanitized and I don't get a LaTeX-typeset axis label.
How do I get all of this to work?
To sanitize the axis labels, modify the scales argument in the following way:
# e-notation (like ggplot2)
scales = list(y = list(log=10, at=10**seq(5, 15, 5))))
or
scales = list(y = list(log=10, at=10**seq(5, 15, 5),
label=sprintf("$10^{%d}$", seq(5, 15, 5))))
As Andy Clifton suggests in comments, doing this with ggplot seems to be a hell of a lot easier than getting it to work with lattice:
\documentclass{article}
\begin{document}
<<contourplot,dev='tikz',echo=FALSE,warning=FALSE>>=
library(ggplot2)
library(scales)
library(RCurl)
x <- getURL("https://dl.dropboxusercontent.com/u/3966900/likelihoods.csv")
likelihoods <- read.csv(text=x)
cutoffs <- c(-Inf,-2700,-1497,-1486.6,-1486.3,-1486.286,-1486.28513,-1486.285082,-1486.28508033,-1486.285080237,Inf)
v <- ggplot(likelihoods, aes(var1, var2, z = ll))
v <- v + stat_contour(breaks=cutoffs)
v <- v + scale_y_continuous(trans=log10_trans(),
breaks=c(1e-5,1e0,1e5,1e10,1e15,1e20),
expand = c(0, 0))
v <- v + scale_x_continuous(limits = c(-3, 5),
expand = c(0, 0))
v <- v + theme_bw()
v <- v + ylab('$\\sigma$')
v
#
\end{document}

Add filename or other annotation to ggplot figures

I use ggplot to make most of my graphics. These can be single panels, or faceted. To make it easier to track revisions, I would like to generate a small label in the corner of the plot that includes some text.
In pseudo code, I am looking for something like this:
# generate the initial plot
p <- ggplot()
# add the label
p + someAnnotationFunction(label = "Version 1.0", x = 1, y = 0,
hjust = "right", vjust = "bottom" )
# print
print(p)
Or: plot my label nestled in the lower right corner of my figure without messing up the existing ggplot graphics.
So far I'm not having any luck finding a solution. This (very interesting) method doesn't work if you have a full m x n table of facets. Methods using gridExtra tend to mess with the plots too much. So, does anyone have a way to add arbitrary text anywhere on a plot that was generated using ggplot?
Here's a worked solution using gridExtra(), based on Baptiste's comment:
require("ggplot2")
require("gridExtra")
# set our working directory
working.dir <- '/Users/aclifton/Documents/projects/Rcode'
setwd(working.dir)
# create a data frame
df <- data.frame(x =runif(100, 1, 10),
y = runif(100, 1, 10))
#create a plot
p <- ggplot(data = df,
aes(x = x,
y = y)) +
geom_point()
print(p)
We now have our plot, and the trick is adding that label and saving the overall plot using ggsave():
# label to show
sub.label = textGrob("Some kind of label",
gp=gpar(fontsize=6),
x = unit(1, "npc"),
hjust = 1,
vjust = 0)
ggsave(filename=file.path(working.dir,'DemoPlot.png'),
plot = arrangeGrob(p,
sub = sub.label,
clip = FALSE),
scale = 1,
width = 6.5,
height = 3.5,
units = c("in"),
dpi = 300)
Which gives you this:
By making a data frame of your annotations, you can add them on top of your plot using geom_text.
note <- data.frame(xVarName = c(1, 5), yVarName = c(1, 10),
text = c("Version 1.0", "April 26, 2014")
p + geom_text(data = anno, aes(label = text))
"Version 1.0" will show up in the bottom left and "April 26, 2014" will show up in the top right.
By making your notes in a separate dataframe, you can add multiple notes to one graph if desired.

Resources