Lattice plot problem outputting to jpeg

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

Lattice plot problem outputting to jpeg

creamers
Hi.....I am relatively new to R but was quite pleased with myself at having generated a series of lattice plots as PDFs. I was very surprised when plotting these out as jpegs (or png or tiff) that the strip title information above each lattice plot vanished. The pdf was fine. Has anybody any ideas? I can't add an image as the information is sensitive.

Many Thanks
Steve Creamer

Here is the code snippet

#jpeg("Z:\\My Documents\\PROJECTS\\Access Policy\\AccessDAPlots_1.jpg",height=600,width=600)
pdf("Z:\\My Documents\\PROJECTS\\Access Policy\\AccessDAPlots_1.pdf")
#png("Z:\\My Documents\\PROJECTS\\Access Policy\\AccessDAPlots_1.png")
#tiff("Z:\\My Documents\\PROJECTS\\Access Policy\\AccessDAPlots_1.tiff")
while (irec <= nrec)
{
   con_new<-consultants[irec]
   spec_new<-specialty[irec]
   spec_cons_new<-spec_cons[irec]

   if (spec_cons_new!=spec_cons_old || irec==nrec )
   {
      strip_name[con_count]<-paste(spec_old,'\n',con_old)
      if (spec_new!=spec_old || irec==nrec)
      {
      spec_old<-spec_new
   }
      con_count<-con_count+1
      if (con_count > 36 || irec==nrec )
      {

#        -------------------------------------------------------------------------------------------------
#        Use xyplot for the lattice - plot from iStartRec to irec each times - this will be 36 consultants
#        /specialties per device plot.
#        -------------------------------------------------------------------------------------------------

         tplot<-xyplot(DAtotals[irecStart:(irec-1)]~dates[irecStart:(irec-1)]
                       |spec_cons[irecStart:(irec-1)],group=nf[irecStart:(irec-1)],layout=c(6,6),
                       type='b',as.table=TRUE,
                       main=paste("Monthly Appts Provided After 1 DNA\n by Specialties/Consultants - ",iSuffix),
                       auto.key = list(cex=0.5,lines=TRUE, points=FALSE,border = TRUE, x=0.05,y=0.90,corner=c(0,0)),
                       ylab = "Number of Monthly Attended Appts Following a DNA ",xlab="Date",
                       scales = list(x = list(rot = 90,format="%b-%y",
                       cex=0.6)),xaxt="n",
              strip = function(which.panel,...)
                             {
                                panel.fill(trellis.par.get("strip.background")$col[1])
                                type <- strip_name[which.panel]
                                grid::grid.text(label = type,x = 0.5, y = 0.5,gp=grid::gpar(fontsize=5))
                                grid::grid.rect()
                             }
                       )
         print(tplot) # plot the lattice plot
         con_count<-1
         irecStart<-irec
         iSuffix<-iSuffix+1 # create suffix
         if (irec != nrec)
         {

#           ------------------------------------------------
#           Open new device and direct to jpeg with new name
#           ------------------------------------------------
            dev.off()
            graphics.off() # turn graphics off to clear memory
            dev.new()

#     jpeg(paste("Z:\\My  Documents\\PROJECTS\\AccessPolicy\\AccessDAPlots_",toString(iSuffix),".jpg",sep=""),
#                  height=600,width=600)
            pdf(paste("Z:\\My Documents\\PROJECTS\\Access Policy\\AccessDAPlots_",toString(iSuffix),".pdf",sep=""))
#            png(paste("Z:\\My Documents\\PROJECTS\\Access Policy\\AccessDAPlots_",toString(iSuffix),".png",sep=""))
#            tiff(paste("Z:\\My Documents\\PROJECTS\\Access Policy\\AccessDAPlots_",toString(iSuffix),".tiff",sep=""))
         }
      }
      con_old<-con_new
      spec_cons_old<-spec_cons_new
   }  
   irec<-irec+1
}
Reply | Threaded
Open this post in threaded view
|

Re: Lattice plot problem outputting to jpeg

David Winsemius

On Jul 19, 2011, at 5:40 AM, creamers wrote:

> Hi.....I am relatively new to R but was quite pleased with myself at  
> having
> generated a series of lattice plots as PDFs. I was very surprised when
> plotting these out as jpegs (or png or tiff) that the strip title
> information above each lattice plot vanished. The pdf was fine. Has  
> anybody
> any ideas? I can't add an image as the information is sensitive.

You have obviously advance far in you understanding of the  
underpinnings of lattice plots, farther than I in many respects. I was  
surprised, therefore, to see that you were directly accessing elements  
of your data in the global environment. Generally lattice functions  
work best when they are given data.frames as arguments.

The other (more specific to your problem) comment is that replacement  
strip functions are generally constructed with the function  
strip.custom(). I get the impression for the docs that this may be  
required, but apparently you succeeded with single plot testing and it  
may be a device issue, so I may be off base. You could still take a  
look at the examples in the help page and see if using that wrapper  
gets you better delivery of arguments to the operative code.

Obviously not able to do any testing, since you have not constructed a  
minimal test dataframe. Device issues often require knowing OS and  
other information that the Posting Guide requests you provide with  
sessionInfo().

--
David.

>
> Many Thanks
> Steve Creamer
>
> Here is the code snippet
>
> #jpeg("Z:\\My Documents\\PROJECTS\\Access
> Policy\\AccessDAPlots_1.jpg",height=600,width=600)
> pdf("Z:\\My Documents\\PROJECTS\\Access Policy\\AccessDAPlots_1.pdf")
> #png("Z:\\My Documents\\PROJECTS\\Access Policy\\AccessDAPlots_1.png")
> #tiff("Z:\\My Documents\\PROJECTS\\Access Policy\
> \AccessDAPlots_1.tiff")
> while (irec <= nrec)
> {
>   con_new<-consultants[irec]
>   spec_new<-specialty[irec]
>   spec_cons_new<-spec_cons[irec]
>
>   if (spec_cons_new!=spec_cons_old || irec==nrec )
>   {
>      strip_name[con_count]<-paste(spec_old,'\n',con_old)
>      if (spec_new!=spec_old || irec==nrec)
>      {
>      spec_old<-spec_new
>   }
>      con_count<-con_count+1
>      if (con_count > 36 || irec==nrec )
>      {
>
> #
> -------------------------------------------------------------------------------------------------
> #        Use xyplot for the lattice - plot from iStartRec to irec  
> each times
> - this will be 36 consultants
> #        /specialties per device plot.
> #
> -------------------------------------------------------------------------------------------------
>
>
> tplot<-xyplot(DAtotals[irecStart:(irec-1)]~dates[irecStart:(irec-1)]
>
> |spec_cons[irecStart:(irec-1)],group=nf[irecStart:
> (irec-1)],layout=c(6,6),
>                       type='b',as.table=TRUE,
>                       main=paste("Monthly Appts Provided After 1 DNA
> \n by
> Specialties/Consultants - ",iSuffix),
>                       auto.key = list(cex=0.5,lines=TRUE,
> points=FALSE,border = TRUE, x=0.05,y=0.90,corner=c(0,0)),
>                       ylab = "Number of Monthly Attended Appts  
> Following a
> DNA ",xlab="Date",
>                       scales = list(x = list(rot = 90,format="%b-%y",
>                       cex=0.6)),xaxt="n",
>             strip = function(which.panel,...)
>                             {
>
> panel.fill(trellis.par.get("strip.background")$col[1])
>                                type <- strip_name[which.panel]
>                                grid::grid.text(label = type,x = 0.5,  
> y =
> 0.5,gp=grid::gpar(fontsize=5))
>                                grid::grid.rect()
>                             }
>                       )
>         print(tplot) # plot the lattice plot
>         con_count<-1
>         irecStart<-irec
>         iSuffix<-iSuffix+1 # create suffix
>         if (irec != nrec)
>         {
>
> #           ------------------------------------------------
> #           Open new device and direct to jpeg with new name
> #           ------------------------------------------------
>            dev.off()
>            graphics.off() # turn graphics off to clear memory
>            dev.new()
>
> #     jpeg(paste("Z:\\My
> Documents\\PROJECTS\\AccessPolicy\
> \AccessDAPlots_",toString(iSuffix),".jpg",sep=""),
> #                  height=600,width=600)
>            pdf(paste("Z:\\My Documents\\PROJECTS\\Access
> Policy\\AccessDAPlots_",toString(iSuffix),".pdf",sep=""))
> #            png(paste("Z:\\My Documents\\PROJECTS\\Access
> Policy\\AccessDAPlots_",toString(iSuffix),".png",sep=""))
> #            tiff(paste("Z:\\My Documents\\PROJECTS\\Access
> Policy\\AccessDAPlots_",toString(iSuffix),".tiff",sep=""))
>         }
>      }
>      con_old<-con_new
>      spec_cons_old<-spec_cons_new
>   }
>   irec<-irec+1
> }
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Lattice-plot-problem-outputting-to-jpeg-tp3677705p3677705.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.

David Winsemius, MD
West Hartford, CT

______________________________________________
[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: Lattice plot problem outputting to jpeg

creamers
Thanks David...I am trying to plot out data for various consultants by specialty - each specialty has a varying number of consultants - each consultant a varying number of data points....I found direct access of the elements of the dataframe was the only way to plot this type of variation, otherwise xyplot seemed to assume that there were a similar number of consultants per specialty. As for the strip.custom, I did try this, its just that I ended up embedding the function inline...I'm not sure there is any difference is there? Sorry I didn't follow protocol ...it is my first time!
Steve
Reply | Threaded
Open this post in threaded view
|

Re: Lattice plot problem outputting to jpeg

Justin Haynes
creamers <stephen.creamer <at> rdeft.nhs.uk> writes:

>
> Thanks David...I am trying to plot out data for various consultants by
> specialty - each specialty has a varying number of consultants - each
> consultant a varying number of data points....I found direct access of the
> elements of the dataframe was the only way to plot this type of variation,
> otherwise xyplot seemed to assume that there were a similar number of
> consultants per specialty. As for the strip.custom, I did try this, its just
> that I ended up embedding the function inline...I'm not sure there is any
> difference is there? Sorry I didn't follow protocol ...it is my first time!
> Steve

It would probably be worth looking at Hadley's ggplot2 package also, ?melt
?reshape.  Maybe you could make one faceted plot instead?  


>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Lattice-plot-problem-outputting-to-jpeg-tp3677705p3678288.html
> Sent from the R help mailing list archive at Nabble.com.
>
>

______________________________________________
[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.