show and produce PDF file with pdf() and dev.off( ) in function

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

show and produce PDF file with pdf() and dev.off( ) in function

casperyc
Hi all,

I know how to use pdf() and dev.off() to produce and save a graph.

However, when I put them in a function say

myplot(x=1:20){
  pdf("xplot.pdf")
  plot(x)
  dev.off()
}

the function work. But is there a way show the graph in R as well as saving it to the workspace?

Thanks.

casper
######################
PhD candidate in Statistics
Big R Fan
Big LEGO Fan
Big sTaTs Fan
######################
Reply | Threaded
Open this post in threaded view
|

Re: show and produce PDF file with pdf() and dev.off( ) in function

chuck.01
1) thats not a function (i'm sure just a mistake)
2) Just add another plot line

myplot <- function(x){
  plot(x)
  pdf("xplot.pdf")
  plot(x)
  dev.off()
}

myplot(1:20)




casperyc wrote
Hi all,

I know how to use pdf() and dev.off() to produce and save a graph.

However, when I put them in a function say

myplot(x=1:20){
  pdf("xplot.pdf")
  plot(x)
  dev.off()
}

the function work. But is there a way show the graph in R as well as saving it to the workspace?

Thanks.

casper
Reply | Threaded
Open this post in threaded view
|

Re: show and produce PDF file with pdf() and dev.off( ) in function

M. Lell
you could either define a plotting function which passes your arguments
to plot() two times, with different devices active.
this function plots the given arguments two times:
 pl<- function(...){
   X11()                   #or pdf()
   plot(...)
   # dev.off()  if pdf() is used
   X11()                  # open second graphics device
   plot(...)
}

pl(1:10,pch=10)
 
or you make a function evaluate your plotting arguments two times if
you're using other plotting functions like lines(), points(), abline()
etc. In this case you have to escape your plotting code with
expression() so it is only executed when you explicitly demand it by
using eval()

Best regards!

plEval <- function(e){
  X11()
  eval(e)
  X11()
  eval(e)
}

plEval(
 expression({
  plot(1:10,pch=5)
  abline(h=3,col="red")
 })
)

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: show and produce PDF file with pdf() and dev.off( ) in function

Igor Sosa Mayor
In reply to this post by casperyc
apart from the other answers, be aware that you have to 'print' the
graph with

pl<-plot(x)
print(pl)

in case you're using lattice or ggplot2 plots.

On Fri, Mar 23, 2012 at 02:40:04PM -0700, casperyc wrote:

> Hi all,
>
> I know how to use pdf() and dev.off() to produce and save a graph.
>
> However, when I put them in a function say
>
> myplot(x=1:20){
>   pdf("xplot.pdf")
>   plot(x)
>   dev.off()
> }
>
> the function work. But is there a way show the graph in R as well as saving
> it to the workspace?
>
> Thanks.
>
> casper
>
> -----
> ###################################################
> PhD candidate in Statistics
> School of Mathematics, Statistics and Actuarial Science, University of Kent
> ###################################################
>
> --
> View this message in context: http://r.789695.n4.nabble.com/show-and-produce-PDF-file-with-pdf-and-dev-off-in-function-tp4500213p4500213.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
:: Igor Sosa Mayor     :: [hidden email] ::
:: GnuPG: 0x1C1E2890   :: http://www.gnupg.org/      ::
:: jabberid: rogorido  ::                            ::

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

attachment0 (237 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: show and produce PDF file with pdf() and dev.off( ) in function

Uwe Ligges-3
On 24.03.2012 13:11, Igor Sosa Mayor wrote:
> apart from the other answers, be aware that you have to 'print' the
> graph with
>
> pl<-plot(x)
> print(pl)


Which is true for lattice function but not for a base graphics plot().

Uwe Ligges



> in case you're using lattice or ggplot2 plots.
>
> On Fri, Mar 23, 2012 at 02:40:04PM -0700, casperyc wrote:
>> Hi all,
>>
>> I know how to use pdf() and dev.off() to produce and save a graph.
>>
>> However, when I put them in a function say
>>
>> myplot(x=1:20){
>>    pdf("xplot.pdf")
>>    plot(x)
>>    dev.off()
>> }
>>
>> the function work. But is there a way show the graph in R as well as saving
>> it to the workspace?
>>
>> Thanks.
>>
>> casper
>>
>> -----
>> ###################################################
>> PhD candidate in Statistics
>> School of Mathematics, Statistics and Actuarial Science, University of Kent
>> ###################################################
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/show-and-produce-PDF-file-with-pdf-and-dev-off-in-function-tp4500213p4500213.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: show and produce PDF file with pdf() and dev.off( ) in function

glsnow
As others have said, you pretty much need to do the plot 2 times, but
if it takes more that one command to create the plot you can use the
dev.copy function to copy what you have just plotted into another
graphics device rather than reissuing all the commands again.

On Sat, Mar 24, 2012 at 9:43 AM, Uwe Ligges
<[hidden email]> wrote:

> On 24.03.2012 13:11, Igor Sosa Mayor wrote:
>>
>> apart from the other answers, be aware that you have to 'print' the
>> graph with
>>
>> pl<-plot(x)
>> print(pl)
>
>
>
> Which is true for lattice function but not for a base graphics plot().
>
> Uwe Ligges
>
>
>
>
>> in case you're using lattice or ggplot2 plots.
>>
>> On Fri, Mar 23, 2012 at 02:40:04PM -0700, casperyc wrote:
>>>
>>> Hi all,
>>>
>>> I know how to use pdf() and dev.off() to produce and save a graph.
>>>
>>> However, when I put them in a function say
>>>
>>> myplot(x=1:20){
>>>   pdf("xplot.pdf")
>>>   plot(x)
>>>   dev.off()
>>> }
>>>
>>> the function work. But is there a way show the graph in R as well as
>>> saving
>>> it to the workspace?
>>>
>>> Thanks.
>>>
>>> casper
>>>
>>> -----
>>> ###################################################
>>> PhD candidate in Statistics
>>> School of Mathematics, Statistics and Actuarial Science, University of
>>> Kent
>>> ###################################################
>>>
>>> --
>>> View this message in context:
>>> http://r.789695.n4.nabble.com/show-and-produce-PDF-file-with-pdf-and-dev-off-in-function-tp4500213p4500213.html
>>> Sent from the R help mailing list archive at Nabble.com.
>>>
>>> ______________________________________________
>>> [hidden email] mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>>
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



--
Gregory (Greg) L. Snow Ph.D.
[hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.