This function works on the command line...

20 views
Skip to first unread message

kw

unread,
Mar 20, 2012, 3:22:36 PM3/20/12
to ggplot2
Folks,
The following function works if I repeat each statement on the
command line.

I successfully create an "inset" graph within a graph using the
viewport commands and following the example of the ggplot2 book
section 8.4.2

When I call it as a function I only get the inset graph. I am sure
that there must be something that I am missing regarding the way pdf()
or par() works in a function.

Thanks for your time,
KW

Here is my code:

pAndLgraph<-function(cf.data, upFront) {
# Create p and l in millions
pl<-pAndL(cf.data, upFront)/1000
names(pl)<-c("P.And.L")
# This is all the p and l less than the maximum value. I.e. the tail
pl.tail<-as.data.frame(pl[var1.pl<max(var1.pl)])
names(pl.tail)<-c("P.And.L")

pl.plot<-qplot(P.And.L, data = pl, geom = "density", size = I(.75))
pl.tail.plot<-qplot(P.And.L, data = pl.tail, geom = "density")
pl.tail.plot<-pl.tail.plot + theme_gray(9) + labs(x = NULL, y =
NULL) +
opts(plot.margin = unit(rep(0,4), "lines"))

pdf(file.choose(), width = 4, height = 4)
vp2<-viewport(width = .4, height = .4, x = .3, y = .7)
pl.plot
print(pl.tail.plot, vp = vp2)
dev.off()
}

baptiste auguie

unread,
Mar 20, 2012, 3:25:01 PM3/20/12
to kw, ggplot2
In a function you need to print all ggplot objects; in particular, you
need to print(pl.plot) for it to appear in the file.

HTH,

baptiste

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2

Brandon Hurr

unread,
Mar 20, 2012, 3:26:11 PM3/20/12
to baptiste auguie, kw, ggplot2
Just out of curiosity, why is that the case? 

Keith Weintraub

unread,
Mar 20, 2012, 3:34:55 PM3/20/12
to baptiste auguie, ggplot2
Cool. That worked.

One interesting thing is that if you look at the doc for pdf() the TextChars example does all the work inside the function.

I am sure there must be something different about ggplot compared to the example.

Thanks again,
KW

baptiste auguie

unread,
Mar 20, 2012, 4:36:57 PM3/20/12
to Brandon Hurr, kw, ggplot2
On 21 March 2012 08:26, Brandon Hurr <brando...@gmail.com> wrote:
> Just out of curiosity, why is that the case?

ggplot2 and lattice produce plots using grid graphics, where one can
separate the production of grobs (GRaphical OBjects) that can be
stored as a description of the plot, and the actual drawing where
these grobs are pushed onto the device. With raw grid objects this is
the distinction between,

g = grid::rectGrob() # create a rectangle, draw nothing
grid::grid.draw(g) # draw it

ggplot2 and lattice work at a higher level, and define a print()
method to display the graphics description containing many grobs and
viewports. In the interactive read-eval-print R session, objects are
printed when their name is invoked on the command line. This is not
the case within a function, or in non-interactive mode, hence the need
to explicitely call print().

HTH,

b.

baptiste auguie

unread,
Mar 20, 2012, 4:39:56 PM3/20/12
to Keith Weintraub, ggplot2
With the latest version of ggplot2, you can add inset plots as part of
the plot itself

?annotation_custom

which, among other things, allows you to use ggsave() to conveniently
save the two plots at once.

b.

Reply all
Reply to author
Forward
0 new messages