1

Dear All i have a Challenge where line plot and point plot has to combined in XYPlot of Lattice Package below is a reprocible example and plotting should be on PDFdepending on factor eg: here factor(f1) = 4 so there should b4 4 pages in a PDF [Note: here Factor is a group data]

`library(lattice)
 d=data.frame(seq <- c(1,2,3,4,5,6,7,8,9,10), 
             n1 <- c(23 ,27 ,26, 24 ,27 ,28 ,28 ,29 ,30 ,31), 
             n2 <- c(31 ,30 ,25, 23 ,22 ,26 ,24 ,29 ,25 ,27), 
             c1 <- c(44 ,0 ,0, 44 ,44 ,0 ,0 ,0 ,44 ,0), 
             c2 <- c(48 ,0 ,0, 48 ,48 ,0 ,0 ,0 ,48 ,0), 
             c3 <- c(50 ,0 ,0, 50 ,50 ,0 ,0 ,0 ,50 ,0),
                 f1 <- c(1 ,1 ,1, 2 ,2 ,3 ,3 ,3 ,4 ,4)) 
 names(d)<- c("Seq","n1","n2","c1","c2","c3")`

Below is RCode : for combining Line and Point Plot

`xyplot(c1 + c2 + c3 ~ seq | as.factor(f1), data = d) +
  xyplot(n1 + n2 ~ seq| as.factor(f1), data = d, type = "l") `

click the Blue link to see the plot,this each plot should be on different pdf page Here as we can Observer all group data is shown on one screen , Required it should created in diffrent pages as per pdf

Creating Pdf of that Plots Code

`pdf('Report.pdf',width = 35 ,height = 20)
print(xyplot(n1 + n2 + c1 + c2  + c3 ~ time | factor(f1), data=v1, pch=19,grid=TRUE,main="Time Series Analysis", xlab="Time",
lab="Y -axis",layout=c(1,1),points=TRUE,size = 50,fill="transparent"))
    dev.off()`

This Code is sucessful when we want to plot either point plot or line plot (either one of them) But In our case scenario is different. we want line plot as well as point and that has two different syntax of xyplot. now query is how to create every plot as per factorwise that to in every single page so that it can be written in pdf format as shown above in my code

Nabi Shaikh
  • 93
  • 1
  • 8
  • You have obviously already posted this question [here](http://stackoverflow.com/questions/42944033/plotting-point-and-line-plot-in-xyplot-lattice). It would have been much better had you just updated that question instead of posting a new one. – Johan Larsson Mar 23 '17 at 17:53
  • 1
    @JohanLarsson I appreciate your concern sir theres a problem with that account i am unable to acess that , its not showing my name ,and the question i posted is not been shown in my history of asking question .I am clueless whats happening there. – Nabi Shaikh Mar 23 '17 at 20:02

1 Answers1

1

You can accomplish this with latticeExtra, which enables you to add trellis grobs as layers.

pdf("report.pdf", paper = "a4")

for (i in unique(d$f1)) {
  pl <- xyplot(c1 + c2 + c3 ~ seq, subset = d$f1 == i, data = d) +
    xyplot(n1 + n2 ~ seq, subset = d$f1 == i, data = d, type = "l")
  print(pl)
}

dev.off()

lattice layers

Johan Larsson
  • 3,496
  • 18
  • 34
  • Thank you for the solution , i am alos including factor(f1) so depending on unique factor available that many number of Plot should be created Let say for example we have 4 uniques value for f1 ,so there would be 4 differrent plot with layout=c(1,1) and once this is done printing in pdf is a easy ,Kindly do guide me on this. – Nabi Shaikh Mar 24 '17 at 08:44
  • @NabiShaikh I've updated the answer to match you're updated question. Please try to provide a complete specification of your question from the start next time. – Johan Larsson Mar 24 '17 at 22:13
  • I Totally agree with you sir , i thought of going stepwise rather then with one go ,anyway thank you for the solution it was a learning to me .`But here with the new solution All plots should be pasted in one pdf Not separately for every plot.` – Nabi Shaikh Mar 25 '17 at 05:16
  • Can we have name on every plot `f1`content in the pdf format right now every plot dosent have heading . `Basically i want to have Legends in every Plot of Pdf Page` Thank you well in advance sir – Nabi Shaikh Mar 30 '17 at 07:29
  • Try browsing the documentation at `?lattice::xyplot` – Johan Larsson Apr 01 '17 at 13:03