Reputation: 93
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
Upvotes: 1
Views: 441
Reputation: 3694
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()
Upvotes: 1