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
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/
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
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
(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}
Interesting about Sweave -- I'll have to play with that more.
Michael
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/