Chart will not display

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

Chart will not display

phil-3
I have made what must be a simple mistake, but I have not been able to
find it.

I create a function to plot a chart for a single variable. I want to
display separate charts for several variables, one after another, with
"Press [enter] to continue" in between. The function works fine for a
single variable, but when I try to display several variables
consecutively, using a for statement, no charts are displayed. The for
statement executes without apparent error, but no charts appear.

Here is a reprex.

library(tidyverse)
t <- c(1,2,3,4,5)
a <- c(1,4,5,8,7)
b <- c(2,2,5,3,1)
c <- c(3,6,2,8,3)
df <- data.frame(t=t,a=a,b=b,c=c)
df1 <- pivot_longer(df,cols=c(a,b,c),names_to="var",values_to="val")
chfn <- function(chnm) {
   ggplot(filter(df1,var==chnm),aes(x=t,y=val,group=1)) +
     geom_line() +
     labs(title=chnm)
}
chfn("b") # test of the function - it works
chnms <- c("a","b","c")
for (i in chnms) {
   chfn(i)
   readline(prompt="Press [enter] to continue")
}

Thanks for your help.

Philip

______________________________________________
[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: Chart will not display

Bert Gunter-2
In a function you must explicitly print/plot the ggplot() object, I assume.
i.e. plot(ggplot(...)) etc.

I do not use ggplot, so if I'm wrong, sorry.  But try it.  Hopefully
someone else will get it right if it doesn't do it.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Tue, Jun 2, 2020 at 6:46 PM <[hidden email]> wrote:

> I have made what must be a simple mistake, but I have not been able to
> find it.
>
> I create a function to plot a chart for a single variable. I want to
> display separate charts for several variables, one after another, with
> "Press [enter] to continue" in between. The function works fine for a
> single variable, but when I try to display several variables
> consecutively, using a for statement, no charts are displayed. The for
> statement executes without apparent error, but no charts appear.
>
> Here is a reprex.
>
> library(tidyverse)
> t <- c(1,2,3,4,5)
> a <- c(1,4,5,8,7)
> b <- c(2,2,5,3,1)
> c <- c(3,6,2,8,3)
> df <- data.frame(t=t,a=a,b=b,c=c)
> df1 <- pivot_longer(df,cols=c(a,b,c),names_to="var",values_to="val")
> chfn <- function(chnm) {
>    ggplot(filter(df1,var==chnm),aes(x=t,y=val,group=1)) +
>      geom_line() +
>      labs(title=chnm)
> }
> chfn("b") # test of the function - it works
> chnms <- c("a","b","c")
> for (i in chnms) {
>    chfn(i)
>    readline(prompt="Press [enter] to continue")
> }
>
> Thanks for your help.
>
> Philip
>
> ______________________________________________
> [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.
>

        [[alternative HTML version deleted]]

______________________________________________
[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: Chart will not display

phil-3
Thanks Bert. That did it.

Philip

On 2020-06-02 22:02, Bert Gunter wrote:

> In a function you must explicitly print/plot the ggplot() object, I
> assume. i.e. plot(ggplot(...)) etc.
>
> I do not use ggplot, so if I'm wrong, sorry.  But try it.  Hopefully
> someone else will get it right if it doesn't do it.
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
> On Tue, Jun 2, 2020 at 6:46 PM <[hidden email]> wrote:
>
>> I have made what must be a simple mistake, but I have not been able
>> to
>> find it.
>>
>> I create a function to plot a chart for a single variable. I want to
>>
>> display separate charts for several variables, one after another,
>> with
>> "Press [enter] to continue" in between. The function works fine for
>> a
>> single variable, but when I try to display several variables
>> consecutively, using a for statement, no charts are displayed. The
>> for
>> statement executes without apparent error, but no charts appear.
>>
>> Here is a reprex.
>>
>> library(tidyverse)
>> t <- c(1,2,3,4,5)
>> a <- c(1,4,5,8,7)
>> b <- c(2,2,5,3,1)
>> c <- c(3,6,2,8,3)
>> df <- data.frame(t=t,a=a,b=b,c=c)
>> df1 <- pivot_longer(df,cols=c(a,b,c),names_to="var",values_to="val")
>> chfn <- function(chnm) {
>> ggplot(filter(df1,var==chnm),aes(x=t,y=val,group=1)) +
>> geom_line() +
>> labs(title=chnm)
>> }
>> chfn("b") # test of the function - it works
>> chnms <- c("a","b","c")
>> for (i in chnms) {
>> chfn(i)
>> readline(prompt="Press [enter] to continue")
>> }
>>
>> Thanks for your help.
>>
>> Philip
>>
>> ______________________________________________
>> [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: [FORGED] Re: Chart will not display

Rolf Turner

On 3/06/20 2:21 pm, [hidden email] wrote:

> Thanks Bert. That did it.
>
> Philip
>
> On 2020-06-02 22:02, Bert Gunter wrote:
>> In a function you must explicitly print/plot the ggplot() object, I
>> assume. i.e. plot(ggplot(...)) etc.
>>
>> I do not use ggplot, so if I'm wrong, sorry.  But try it.  Hopefully
>> someone else will get it right if it doesn't do it.

<SNIP>

You might find fortunes::fortune(123) to relevant.  The comment was made
in respect of the *lattice* package, but it would seem to apply equally
to *ggplot2*.

cheers,

Rolf Turner

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[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.