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
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 <phil at philipsmith.ca> 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 > > ______________________________________________ > R-help at r-project.org 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]]
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 <phil at philipsmith.ca> 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 >> >> ______________________________________________ >> R-help at r-project.org 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.