function to return plots

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

function to return plots

Naresh Gurbuxani

I want to write a function that will return lattice plots.  This simple
function output a list of two plots.  These plots can be
individually shown on the console.  But I am unable to put them on two
panels of a single plot.

What changes do I need to make to this function?

Thanks,
Naresh

library(lattice)

getPlots <- function(){
    x <- rnorm(1000)
    plt1 <- histogram(x)
    plt2 <- bwplot(x)
    list(plt1, plt2)
}

plot.list <- getPlots()

plot.list[1] #Plots graph
plot.list[2] #Plots graph

plot(plot.list[1], position = c(0, 0, 1, 0.5), more = TRUE) #Error message
plot(plot.list[2], position = c(0, 0.5, 1, 1), more = FALSE) #Error message

## Plotting outside function works
x <- rnorm(1000)
plt1 <- histogram(x)
plt2 <- bwplot(x)
plot(plt1, position = c(0, 0, 1, 0.5), more = TRUE)
plot(plt2, position = c(0, 0.5, 1, 1), more = FALSE)

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: function to return plots

Jim Lemon-4
Hi Naresh,
The somewhat obscure syntax of lattice.

print(plot.list[[1]])
print(plot.list[[2]])

Jim

On Fri, Jun 12, 2020 at 7:53 PM Naresh Gurbuxani
<[hidden email]> wrote:

>
>
> I want to write a function that will return lattice plots.  This simple
> function output a list of two plots.  These plots can be
> individually shown on the console.  But I am unable to put them on two
> panels of a single plot.
>
> What changes do I need to make to this function?
>
> Thanks,
> Naresh
>
> library(lattice)
>
> getPlots <- function(){
>     x <- rnorm(1000)
>     plt1 <- histogram(x)
>     plt2 <- bwplot(x)
>     list(plt1, plt2)
> }
>
> plot.list <- getPlots()
>
> plot.list[1] #Plots graph
> plot.list[2] #Plots graph
>
> plot(plot.list[1], position = c(0, 0, 1, 0.5), more = TRUE) #Error message
> plot(plot.list[2], position = c(0, 0.5, 1, 1), more = FALSE) #Error message
>
> ## Plotting outside function works
> x <- rnorm(1000)
> plt1 <- histogram(x)
> plt2 <- bwplot(x)
> plot(plt1, position = c(0, 0, 1, 0.5), more = TRUE)
> plot(plt2, position = c(0, 0.5, 1, 1), more = FALSE)
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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: function to return plots

Rui Barradas
In reply to this post by Naresh Gurbuxani
Hello,

plot.list is a list, try '[[' to access its members.
('[' returns sub-lists.)


plot(plot.list[[1]], position = c(0, 0, 1, 0.5), more = TRUE) #Works
plot(plot.list[[2]], position = c(0, 0.5, 1, 1), more = FALSE) #Works


Hope this helps,

Rui Barradas

Às 10:52 de 12/06/20, Naresh Gurbuxani escreveu:

>
> I want to write a function that will return lattice plots.  This simple
> function output a list of two plots.  These plots can be
> individually shown on the console.  But I am unable to put them on two
> panels of a single plot.
>
> What changes do I need to make to this function?
>
> Thanks,
> Naresh
>
> library(lattice)
>
> getPlots <- function(){
>      x <- rnorm(1000)
>      plt1 <- histogram(x)
>      plt2 <- bwplot(x)
>      list(plt1, plt2)
> }
>
> plot.list <- getPlots()
>
> plot.list[1] #Plots graph
> plot.list[2] #Plots graph
>
> plot(plot.list[1], position = c(0, 0, 1, 0.5), more = TRUE) #Error message
> plot(plot.list[2], position = c(0, 0.5, 1, 1), more = FALSE) #Error message
>
> ## Plotting outside function works
> x <- rnorm(1000)
> plt1 <- histogram(x)
> plt2 <- bwplot(x)
> plot(plt1, position = c(0, 0, 1, 0.5), more = TRUE)
> plot(plt2, position = c(0, 0.5, 1, 1), more = FALSE)
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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: function to return plots

Naresh Gurbuxani
Thanks for your quick response.  It works as I wanted. 



From: Rui Barradas <[hidden email]>
Sent: Friday, June 12, 2020 7:08 AM
To: Naresh Gurbuxani <[hidden email]>; [hidden email] <[hidden email]>
Subject: Re: [R] function to return plots
 
Hello,

plot.list is a list, try '[[' to access its members.
('[' returns sub-lists.)


plot(plot.list[[1]], position = c(0, 0, 1, 0.5), more = TRUE) #Works
plot(plot.list[[2]], position = c(0, 0.5, 1, 1), more = FALSE) #Works


Hope this helps,

Rui Barradas

Às 10:52 de 12/06/20, Naresh Gurbuxani escreveu:

>
> I want to write a function that will return lattice plots.  This simple
> function output a list of two plots.  These plots can be
> individually shown on the console.  But I am unable to put them on two
> panels of a single plot.
>
> What changes do I need to make to this function?
>
> Thanks,
> Naresh
>
> library(lattice)
>
> getPlots <- function(){
>      x <- rnorm(1000)
>      plt1 <- histogram(x)
>      plt2 <- bwplot(x)
>      list(plt1, plt2)
> }
>
> plot.list <- getPlots()
>
> plot.list[1] #Plots graph
> plot.list[2] #Plots graph
>
> plot(plot.list[1], position = c(0, 0, 1, 0.5), more = TRUE) #Error message
> plot(plot.list[2], position = c(0, 0.5, 1, 1), more = FALSE) #Error message
>
> ## Plotting outside function works
> x <- rnorm(1000)
> plt1 <- histogram(x)
> plt2 <- bwplot(x)
> plot(plt1, position = c(0, 0, 1, 0.5), more = TRUE)
> plot(plt2, position = c(0, 0.5, 1, 1), more = FALSE)
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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.