[R] [R studio] Plotting of line chart for each columns at 1 page

PIKAL Petr petr@pik@l @ending from prechez@@cz
Wed Nov 21 08:43:14 CET 2018


Hi

When I do multiple plots of similar data I usually put the plots into the multipage pdf file

pdf("somename.pdf")
for (i in columns) {
p<-ggplot(something)
print(p+geom_point(size=4)+stat_smooth(se=F, span=0.5, size=1.3)+
facet_grid(al2o3~teplota, labeller="label_both"))

or

plot(something)

}
dev.off()

This will generate somename.pdf in your working directory and plots will be definitelly bigger than 40 plots in one page.

Another approach could be to store plots as objects in a list (which is easy done with ggplot) but rather trickier with base graphics and make actual plotting after the whole list is populated with your plots.

https://www.andrewheiss.com/blog/2016/12/08/save-base-graphics-as-pseudo-objects-in-r/

Cheers
Petr


> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Subhamitra Patra
> Sent: Wednesday, November 21, 2018 4:52 AM
> To: Jim Lemon <drjimlemon using gmail.com>
> Cc: r-help using r-project.org
> Subject: Re: [R] [R studio] Plotting of line chart for each columns at 1 page
>
> Hello Sir,
>
> Thank you very much. I will try it out and will let you the result.
>
> The no. of rows varies per sheet by a different number of observations. Due to
> different no. of rows or observations, I separated the columns in different
> sheets.
>
> *Will a different number of rows create a problem for appending all plots?*
>
> Concerning your last suggestion "*This seems like a lot of plots, and I suspect
> that you could work out a better way to display all this information.*",  I am
> doing a multi-country study and obtained results for each country. I would
> summarize the final result at the end. But, for displaying the information for
> each country, I thought the plot is the best way to give a supplementary result
> on each country. Sir, in this context, I would like to take your suggestion that Is
> the way what I am doing, right to proceed? If any alternative way is available,
> please suggest me.
>
> Thank you very much, Sir, for your kind help and suggestions.
>
> [image: Mailtrack]
> <https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campa
> ign=signaturevirality5&>
> Sender
> notified by
> Mailtrack
> <https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campa
> ign=signaturevirality5&>
> 11/21/18,
> 9:12:14 AM
>
> On Wed, Nov 21, 2018 at 8:18 AM Jim Lemon <drjimlemon using gmail.com>
> wrote:
>
> > I assume that you are importing the Excel sheets separately. When you
> > import a sheet, you can get the number of columns with this:
> >
> > ncol(<name of data frame>)
> >
> > Using the data frame "mpg" that I created:
> >
> > ncolumns<-ncol(mpg)
> > ncolumns
> > [1] 38
> >
> > You can then substitute "ncolumns" each time you import another sheet.
> > How you want to deal with the varying numbers of columns you will get
> > is another matter. One way is to work out the total number of plots
> > you want and put them all onto one PDF page. Say you have 50 plots
> > overall. You could start a very big PDF page:
> >
> > pdf("allplots.pdf",width=30,height=15)
> > par(mfrow=c(5,10))
> > # import your first sheet here (38 columns)
> > ncolumns<-ncol(mpg)
> > for(i in 1:ncolumns)
> >  plot(seq(1,500,length.out=10),mpg[,i],type="l",xlab="Distance",
> >   ylab="MPG",main=names(mpg)[i])# import your second sheet here, say
> > 10 columns # import your second sheet here, (10 columns)
> > ncolumns<-ncol(mpg1)
> > for(i in 1:ncolumns)
> >  plot(seq(1,500,length.out=10),mpg1[,i],type="l",xlab="Distance",
> >   ylab="MPG",main=names(mpg)[i])# import your third sheet here, say 2
> > columns # import your second sheet here, (2 columns)
> > ncolumns<-ncol(mpg2)
> > for(i in 1:ncolumns)
> >  plot(seq(1,500,length.out=10),mpg2[,i],type="l",xlab="Distance",
> >   ylab="MPG",main=names(mpg)[i])
> > # finish plotting
> > dev.off()
> >
> > You would then have 50 plots on the PDF page. I am assuming that all
> > of your sheets have the same number of rows and a few other things.
> > This seems like a lot of plots, and I suspect that you could work out
> > a better way to display all this information.
> >
> > Jim
> >
> >
> > On Wed, Nov 21, 2018 at 1:20 PM Subhamitra Patra <
> > subhamitra.patra using gmail.com> wrote:
> >
> >> Hello Sir,
> >>
> >> Thanks, now I understood and will check them out.
> >>
> >> One more thing I want to ask that I have 1 excel file with multiple (i.e.
> >> 12 sheets). Each sheet contains different number of columns, for
> >> instance, 1st sheet contains 38 columns, 2nd sheet contains 10
> >> columns, Third 2 columns, 4th 1 column and so on. Actually, due to
> >> some missing observations in these columns, I couldn't add them in 1 sheet.
> >>
> >> As you suggested the below code in the last mail,
> >>
> >> par(mfrow=c(4,10))
> >> for(i in 1:38)
> >>  plot(seq(1,500,length.out=10),mpg[,i],type="l",xlab="Distance",
> >>   ylab="MPG",main=names(mpg)[i])
> >> dev.off()
> >>
> >> Do I need to run the code separately for each sheet?
> >>
> >> Actually, in par (mfrow=c(4,10)), the plot for 38 columns will be
> >> added, the space for extra 2 will remain as empty. So, I thought to
> >> add plots for the columns from the next sheet in those emptied space.
> >>
> >> Is there any way that I can add plots from the next sheets of the
> >> same excel file in the emptied space? In other words, Is there any
> >> way to append plots from all sheets?
> >>
> >> Kindly help a new R learner Sir for which I shall be always grateful
> >> to you.
> >>
> >> Thank you very much for your kind help.
> >>
> >>
> >>
> >> [image: Mailtrack]
> >>
> <https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campa
> >> ign=signaturevirality5&> Sender notified by Mailtrack
> >>
> <https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campa
> >> ign=signaturevirality5&> 11/21/18,
> >> 7:30:30 AM
> >>
> >> On Wed, Nov 21, 2018 at 7:17 AM Jim Lemon <drjimlemon using gmail.com>
> wrote:
> >>
> >>> Hi Subhamitra,
> >>>
> >>> 1. Here I manufacture some data so that the example is
> >>> "reproducible", that is anyone can run the code and get the same
> >>> output that I do. Yes,
> >>> veh1...veh38 are the names of the variables.
> >>>
> >>> 2. Here I join the 38 variables I created into a data frame, which I
> >>> think is the input for your plotting routine. This names of the
> >>> columns of the data frame become the names of the variables.
> >>>
> >>> When you say that you want the column names as the "header" (title)
> >>> of each plot, I think if you change the plotting loop to this:
> >>>
> >>> pdf("mpg.pdf",width=30,height=12)
> >>> par(mfrow=c(4,10))
> >>> for(i in 1:38)
> >>>  plot(seq(1,500,length.out=10),mpg[,i],type="l",xlab="Distance",
> >>>   ylab="MPG",main=names(mpg)[i])
> >>> dev.off()
> >>>
> >>> you will get what you requested. Remember that I have done this in
> >>> base graphics, not ggplot.
> >>>
> >>> Jim
> >>>
> >>> On Wed, Nov 21, 2018 at 12:37 PM Subhamitra Patra <
> >>> subhamitra.patra using gmail.com> wrote:
> >>>
> >>>> Hello Sir,
> >>>>
> >>>> Thanks, I'll check them out.
> >>>>
> >>>> But, I am not understanding 2 points of your suggestion.
> >>>>
> >>>> 1. In the line,* "*for(i in 1:38) assign(paste0("veh",i),rep(sam
> >>>> ple(10:35,1),10)+runif(10,-4,*4))", *what veh,
> >>>> rep(sample(10:35,1),10)
> >>>> +runif(10,-4,4)) indicate? Here veh indicates columns right?
> >>>> *2. In the
> >>>> line,
> >>>> mpg<-data.frame(veh1,veh2,veh3,veh4,veh5,veh6,veh7,veh8,veh9,veh10,
> >>>> *
> >>>> *
> veh11,veh12,veh13,veh14,**veh15,veh16,veh17,veh18,veh19,**veh20,*
> >>>> *
> veh21,veh22,veh23,veh24,**veh25,veh26,veh27,veh28,veh29,**veh30,*
> >>>> * veh31,veh32,veh33,veh34,**veh35,veh36,veh37,veh38)  ** , *veh[i]
> >>>> indicates column sequence, right? I need to give column names as
> >>>> the header of their respective graphs. Please suggest me How to add this?
> >>>>
> >>>>
> >>>> I am very new to R and therefore asking you these queries which
> >>>> might be simple for you.
> >>>>
> >>>> I expect positive help from you.
> >>>>
> >>>> Thanks for your kind help.
> >>>> [image: Mailtrack]
> >>>>
> <https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_cam
> >>>> paign=signaturevirality5&> Sender notified by Mailtrack
> >>>>
> <https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_cam
> >>>> paign=signaturevirality5&> 11/21/18,
> >>>> 7:02:18 AM
> >>>>
> >>>>
> >>>> On Wed, Nov 21, 2018 at 4:38 AM Jim Lemon <drjimlemon using gmail.com>
> wrote:
> >>>>
> >>>>> Hi Subhamitra,
> >>>>> As Bert noted, you are mixing base and grid graphics. Here is a
> >>>>> simple way to get a plot like what you described. It will probably
> >>>>> take more work to find what you actually do want and discover how to
> get it.
> >>>>>
> >>>>> for(i in 1:38)
> >>>>> assign(paste0("veh",i),rep(sample(10:35,1),10)+runif(10,-4,4))
> >>>>> mpg<-data.frame(veh1,veh2,veh3,veh4,veh5,veh6,veh7,veh8,veh9,veh10
> >>>>> ,  veh11,veh12,veh13,veh14,veh15,veh16,veh17,veh18,veh19,veh20,
> >>>>>  veh21,veh22,veh23,veh24,veh25,veh26,veh27,veh28,veh29,veh30,
> >>>>>  veh31,veh32,veh33,veh34,veh35,veh36,veh37,veh38)
> >>>>> pdf("mpg.pdf",width=30,height=12)
> >>>>> par(mfrow=c(4,10))
> >>>>> for(i in 1:38)
> >>>>>  plot(seq(1,500,length.out=10),mpg[,i],type="l",xlab="Distance",
> >>>>>   ylab=names(mpg)[i],main="MPG by distance")
> >>>>> dev.off()
> >>>>>
> >>>>> Jim
> >>>>>
> >>>>> On Wed, Nov 21, 2018 at 5:19 AM Subhamitra Patra
> >>>>> <subhamitra.patra using gmail.com> wrote:
> >>>>> >
> >>>>> > Dear R users,
> >>>>> >
> >>>>> > I have one excel file with 5 sheets. The no. of columns vary for
> >>>>> > each sheet. The 1st sheet consists of 38 columns. So, I want to
> >>>>> > plot 38
> >>>>> separate
> >>>>> > line charts and arrange them in par(mfrow = c(4, 10)) order.
> >>>>> > Please
> >>>>> suggest
> >>>>> > me how to do this. I have tried with the following code by
> >>>>> > running a
> >>>>> loop
> >>>>> > inside of a sheet, but it is not working. Further, I want to run
> >>>>> loops for
> >>>>> > each sheet.
> >>>>> >
> >>>>> > par(mfrow = c(4, 10))
> >>>>> > loop.vector <- 1:38
> >>>>> > for (i in loop.vector)
> >>>>> > x <- JJ[,i]
> >>>>> > library(ggplot2)
> >>>>> >   library(cowplot)
> >>>>> >   plot.mpg <- ggplot(mpg, aes(x,
> >>>>> >                               main = paste ("country", i),
> >>>>> >                               xlab = "Scores",
> >>>>> >                               xlim = c(1,500)
> >>>>> >                               y = colnames[i,], colour =
> >>>>> factor(cyl))) +
> >>>>> >   geom_line(size=2.5)
> >>>>> > save_plot("mpg.png", plot.mpg,
> >>>>> >           base_aspect_ratio = 1.3)
> >>>>> >
> >>>>> > I want to give my X axis name as scores of (1,500) and Y axis as
> >>>>> > the particular column names for all graphs.
> >>>>> >
> >>>>> > Please suggest.
> >>>>> >
> >>>>> > Thanks in advance.
> >>>>> >
> >>>>> > --
> >>>>> > *Best Regards,*
> >>>>> > *Subhamitra Patra*
> >>>>> > *Phd. Research Scholar*
> >>>>> > *Department of Humanities and Social Sciences* *Indian Institute
> >>>>> > of Technology, Kharagpur*
> >>>>> > *INDIA*
> >>>>> >
> >>>>> >
> >>>>> >
> >>>>> >
> >>>>> >
> >>>>> >
> >>>>> >
> >>>>> >
> >>>>> > [image: Mailtrack]
> >>>>> > <
> >>>>>
> https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_cam
> >>>>> paign=signaturevirality5&
> >>>>> >
> >>>>> > Sender
> >>>>> > notified by
> >>>>> > Mailtrack
> >>>>> > <
> >>>>>
> https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_cam
> >>>>> paign=signaturevirality5&
> >>>>> >
> >>>>> > 11/20/18,
> >>>>> > 11:49:42 PM
> >>>>> >
> >>>>> >         [[alternative HTML version deleted]]
> >>>>> >
> >>>>> > ______________________________________________
> >>>>> > R-help using 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.
> >>>>>
> >>>>
> >>>>
> >>>> --
> >>>> *Best Regards,*
> >>>> *Subhamitra Patra*
> >>>> *Phd. Research Scholar*
> >>>> *Department of Humanities and Social Sciences* *Indian Institute of
> >>>> Technology, Kharagpur*
> >>>> *INDIA*
> >>>>
> >>>
> >>
> >> --
> >> *Best Regards,*
> >> *Subhamitra Patra*
> >> *Phd. Research Scholar*
> >> *Department of Humanities and Social Sciences* *Indian Institute of
> >> Technology, Kharagpur*
> >> *INDIA*
> >>
> >
>
> --
> *Best Regards,*
> *Subhamitra Patra*
> *Phd. Research Scholar*
> *Department of Humanities and Social Sciences* *Indian Institute of
> Technology, Kharagpur*
> *INDIA*
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using 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.
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/



More information about the R-help mailing list