print multiple plots to jpeg, one lattice and one ggplot2

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

print multiple plots to jpeg, one lattice and one ggplot2

Christophe Bouffioux
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]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: print multiple plots to jpeg, one lattice and one ggplot2

Felipe Carrillo
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 <[hidden email]>
>To: "[hidden email]" <[hidden email]>
>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]]
>
>______________________________________________
>[hidden email] 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]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: print multiple plots to jpeg, one lattice and one ggplot2

Christophe Bouffioux
OK thanks
it works

christophe


2013/5/3 Felipe Carrillo <[hidden email]>

> 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 <[hidden email]>
> *To:* "[hidden email]" <[hidden email]>
> *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]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
>
>
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.