plot.ggplot

31 views
Skip to first unread message

R. Michael Weylandt

unread,
Mar 9, 2012, 10:01:55 PM3/9/12
to ggp...@googlegroups.com
Would there be interest in defining

plot.ggplot <- print.ggplot

or would that lead to confusion? I know lattice does something like
this and it seems intuitive enough to me -- more so than telling folks
to print() -- but I can see that it might lead to some confusion with
base graphics.

Michael

Joshua Wiley

unread,
Mar 9, 2012, 10:08:34 PM3/9/12
to R. Michael Weylandt, ggp...@googlegroups.com
Hi Michael,

I think you could, but I think typical show is more intuitive. For
example, suppose one saves a model object:

m <- lm(mpg ~ hp, data = mtcars)

to view it, simply:

m

people are used to this. ggplot2 works the same way:

p <- ggplot(mtcares, aes(hp, mpg)) + geom_point()

and to view:

p

no need to explicitly print(p) it. Again as with models, for a one
line save and view:

(p <- ggplot(mtcars, aes(mpg, hp)) + geom_point())

is a standard approach. My $.02.

Cheers,

Josh

> --
> 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

--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/

R. Michael Weylandt

unread,
Mar 9, 2012, 10:35:39 PM3/9/12
to Joshua Wiley, ggp...@googlegroups.com
Oh, I didn't mean to drop print.ggplot -- that's far too helpful in
interactive sessions -- I was thinking more of an alias intended for
folks who build up their graph over a few lines and then ask "and how
can I plot this?" I'd even implement it directly as an alias:

plot.ggplot<- function(x, ...) print.ggplot(x, ...)

No loss of back compatibility but perhaps just a little easier for
those just learning grid-based graphics who don't immediately realize
that print() methods are called automatically at top-level interactive
sessions.

I like the parentheses trick to trigger print() methods too, but I
think it's more of a (useful) cheat than a reasonable bit of design.

Incidentally, I first misread your email and found out that show()
also dispatches to print() on ggplot object

Best,

Michael Weylandt

Joshua Wiley

unread,
Mar 9, 2012, 10:39:32 PM3/9/12
to R. Michael Weylandt, ggp...@googlegroups.com
My point is that print() used interactively is not needed at all.
Yes, () is a bit of a trick (although very common for books,
tutorials, etc.).

You can build up the plot over several lines, and then simply by
entering the name of the ggplot object, it will be show()n, which as
you point out, dispatches internally to print(). So my answer to "how
can I plot this" is the same for "how can I view any R object?" just
type the object name. Again:

p <- ggplot(mtcars, aes(mpg, hp))

p <- p + geom_point()

p <- p + stat_smooth()

p # final p shows which implicitly prints, how would: plot(p) be better?

On Fri, Mar 9, 2012 at 7:35 PM, R. Michael Weylandt

Joshua Wiley

unread,
Mar 9, 2012, 10:48:23 PM3/9/12
to R. Michael Weylandt, ggp...@googlegroups.com
Hmm, maybe I was misunderstanding your intended use, maybe you meant
non-interactive sessions where the object is not stored. That would
make more sense.

(incidentally the show an object works with Sweave and the new knitr
by Yihui Xie packages does just include ggplot2 graphics without any
printing like you were working interactively)

\documentclass{article}
\begin{document}
<<fig=TRUE>>=
require(ggplot2)


p <- ggplot(mtcars, aes(mpg, hp))
p <- p + geom_point()
p

@
\end{document}

examples.pdf

R. Michael Weylandt

unread,
Mar 9, 2012, 11:48:36 PM3/9/12
to Joshua Wiley, ggp...@googlegroups.com
Yes, that's exactly what I was thinking.

Interesting about Sweave -- I'll have to play with that more.


Michael

Hadley Wickham

unread,
Apr 11, 2012, 3:25:00 PM4/11/12
to R. Michael Weylandt, Joshua Wiley, ggp...@googlegroups.com
Good idea: https://github.com/hadley/ggplot2/issues/478

Hadley

On Fri, Mar 9, 2012 at 10:48 PM, R. Michael Weylandt

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages