Christophe Bouffioux
2013-May-03 13:33 UTC
[R] print multiple plots to jpeg, one lattice and one ggplot2
hello everybody,
I want to print two plots in one png file, I tried several options but i
didn't succeed
the first plot (bwplot) print to the defined position, but the second
(ggplot) doesn't
Any idea?
Thanks a lot
Christophe
# Example:
#-------------------------------------
library(ggplot2)
library(lattice)
library(grid)
one <- bwplot(decrease ~ treatment, OrchardSprays, groups = rowpos,
panel = "panel.superpose",
panel.groups = "panel.linejoin",
xlab = "treatment",
key = list(lines = Rows(trellis.par.get("superpose.line"),
c(1:7, 1)),
text = list(lab as.character(unique(OrchardSprays$rowpos))),
columns = 4, title = "Row position"))
df <- data.frame(gp = factor(rep(letters[1:3], each = 10)),
y = rnorm(30))
# Compute sample mean and standard deviation in each group
library(plyr)
ds <- ddply(df, .(gp), summarise, mean = mean(y), sd = sd(y))
two <- ggplot(df, aes(x = gp, y = y)) +
geom_point() +
geom_point(data = ds, aes(y = mean),
colour = 'red', size = 3)
# 1. not working
jpeg(file=paste(pathgraph,'/fig03_profiltot','.png',sep=''),width
= 600,
height = 400, units="px", res=100)
print(one, position=c(0,0,0.5,1), more=TRUE)
print(two, position=c(0.5,0,1,1), )
dev.off()
# 2 not working
jpeg(file=paste(pathgraph,'/fig03_profiltot','.png',sep=''),width
= 600,
height = 400, units="px", res=100)
grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 2)))
print(one, vp = viewport(layout.pos.row = 1, layout.pos.col = 1))
# ça ne fonctionne pas
print(two, vp = viewport(layout.pos.row = 1, layout.pos.col = 2))
dev.off()
# 3 not working
jpeg(file=paste(pathgraph,'/fig03_profiltot','.png',sep=''),width
= 600,
height = 400, units="px", res=100)
par(mfrow=c(1,2))
one
two
dev.off()
[[alternative HTML version deleted]]
Felipe Carrillo
2013-May-03 14:54 UTC
[R] print multiple plots to jpeg, one lattice and one ggplot2
Something like this? library(gridExtra) grid.arrange(one,two) Felipe D. Carrillo Supervisory Fishery Biologist Department of the Interior US Fish & Wildlife Service California, USA http://www.fws.gov/redbluff/rbdd_jsmp.aspx>________________________________ >From: Christophe Bouffioux <christophe.b00@gmail.com> >To: "r-help@r-project.org" <r-help@r-project.org> >Sent: Friday, May 3, 2013 6:33 AM >Subject: [R] print multiple plots to jpeg, one lattice and one ggplot2 > > >hello everybody, > >I want to print two plots in one png file, I tried several options but i >didn't succeed >the first plot (bwplot) print to the defined position, but the second >(ggplot) doesn't >Any idea? >Thanks a lot >Christophe > > ># Example: >#------------------------------------- > >library(ggplot2) >library(lattice) >library(grid) > >one <- bwplot(decrease ~ treatment, OrchardSprays, groups = rowpos, > panel = "panel.superpose", > panel.groups = "panel.linejoin", > xlab = "treatment", > key = list(lines = Rows(trellis.par.get("superpose.line"), > c(1:7, 1)), > text = list(lab >as.character(unique(OrchardSprays$rowpos))), > columns = 4, title = "Row position")) > > >df <- data.frame(gp = factor(rep(letters[1:3], each = 10)), > y = rnorm(30)) ># Compute sample mean and standard deviation in each group >library(plyr) >ds <- ddply(df, .(gp), summarise, mean = mean(y), sd = sd(y)) > >two <- ggplot(df, aes(x = gp, y = y)) + > geom_point() + > geom_point(data = ds, aes(y = mean), > colour = 'red', size = 3) > > > ># 1. not working >jpeg(file=paste(pathgraph,'/fig03_profiltot','.png',sep=''),width = 600, >height = 400, units="px", res=100) > print(one, position=c(0,0,0.5,1), more=TRUE) > print(two, position=c(0.5,0,1,1), ) >dev.off() > > ># 2 not working >jpeg(file=paste(pathgraph,'/fig03_profiltot','.png',sep=''),width = 600, >height = 400, units="px", res=100) >grid.newpage() >pushViewport(viewport(layout = grid.layout(1, 2))) > > print(one, vp = viewport(layout.pos.row = 1, layout.pos.col = 1)) ># ça ne fonctionne pas > print(two, vp = viewport(layout.pos.row = 1, layout.pos.col = 2)) >dev.off() > > > ># 3 not working >jpeg(file=paste(pathgraph,'/fig03_profiltot','.png',sep=''),width = 600, >height = 400, units="px", res=100) > par(mfrow=c(1,2)) > one > two >dev.off() > > [[alternative HTML version deleted]] > >______________________________________________ >R-help@r-project.org mailing list >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]]