Dimitri Liakhovitski
2009-Feb-16 17:14 UTC
[R] Printing out a graph using different graphics devices
Hello, everyone! The code below allows me to produce the graph I want (I know - the colors are strange, but it's just for the sake of an example). After you run the plot<- part and then do print(plot) - that's what I want. However, when I run the bits of code below (with graphics devices) - what they print is different from the original plot. In .png, .emf, and .tiff - my dots change their shape and my lines (I think) become thinner. And in .ps format - the dots stay what they are supposed to be but they change their color. Could you please explain to me what the reasons are for those changes? Also - should I specify my chart characteristics differently depending on the device I'll be using? Thank you very much! Dimitri library(lattice) d=data.frame(xx=c(2.2,2.1,3.3),yy=c(0.1,0.2,0.3),zz=c(2.5,2.0,1.8)) d[[2]]<-as.factor(as.numeric((d[[2]]))) trellis.par.set(superpose.line = list(col=c("green","red"), lwd = 2), superpose.symbol = list(col=c("yellow","blue"),cex 1.3, pch = 20), reference.line = list(col = "gray", lty ="dotted")) plot<-dotplot(c(d[[1]],d[[3]])~rep(d[[2]],2), groups=rep(c("Group 1","Group 2"), each=nrow(d)), main=list("Chart Title",cex=1), type="b", auto.key = list(space = "top", points = TRUE, lines = TRUE), xlab=list("Title for X",cex=.9,font=2), ylab=list("Title for Y",cex=.9,font=2), panel = function(y,x,...) { panel.grid(h = -1, v = -1) panel.xyplot(x, y, ...) ltext(x, y, labels=round(y,3), cex=.8,col="black",font=2, adj=c(-0.2,1)) }) print(plot) win.metafile(file="test.emf") print(plot) dev.off() postscript(file="test.ps") print(plot) dev.off() png(file="test.png") print(plot) dev.off() tiff(file="test.tiff") print(plot) dev.off() -- Dimitri Liakhovitski MarketTools, Inc. Dimitri.Liakhovitski at markettools.com
Deepayan Sarkar
2009-Feb-16 17:50 UTC
[R] Printing out a graph using different graphics devices
On 2/16/09, Dimitri Liakhovitski <ld7631 at gmail.com> wrote:> Hello, everyone! > The code below allows me to produce the graph I want (I know - the > colors are strange, but it's just for the sake of an example). > After you run the plot<- part and then do print(plot) - that's what I want. > However, when I run the bits of code below (with graphics devices) - > what they print is different from the original plot. In .png, .emf, > and .tiff - my dots change their shape and my lines (I think) become > thinner. And in .ps format - the dots stay what they are supposed to > be but they change their color. > Could you please explain to me what the reasons are for those changes? > Also - should I specify my chart characteristics differently depending > on the device I'll be using?Device drivers may all be subtly different, which may explain line widths etc. For the things that you set using trellis.par.set(), see ?trellis.device. Basically, each device has its own settings, and calls to trellis.par.set() only change the settings for the current device. If you want to attach custom settings to your "trellis" object, look up 'par.settings' in ?xyplot. -Deepayan> Thank you very much! > Dimitri > > library(lattice) > d=data.frame(xx=c(2.2,2.1,3.3),yy=c(0.1,0.2,0.3),zz=c(2.5,2.0,1.8)) > d[[2]]<-as.factor(as.numeric((d[[2]]))) > > trellis.par.set(superpose.line = list(col=c("green","red"), lwd = 2), > superpose.symbol = list(col=c("yellow","blue"),cex > 1.3, pch = 20), > reference.line = list(col = "gray", lty ="dotted")) > > plot<-dotplot(c(d[[1]],d[[3]])~rep(d[[2]],2), > groups=rep(c("Group 1","Group 2"), each=nrow(d)), > main=list("Chart Title",cex=1), > > type="b", > auto.key = list(space = "top", points = TRUE, lines = TRUE), > > xlab=list("Title for X",cex=.9,font=2), > ylab=list("Title for Y",cex=.9,font=2), > > panel = function(y,x,...) { > panel.grid(h = -1, v = -1) > panel.xyplot(x, y, ...) > > ltext(x, y, labels=round(y,3), > cex=.8,col="black",font=2, > adj=c(-0.2,1)) > > }) > print(plot) > > win.metafile(file="test.emf") > print(plot) > dev.off() > > postscript(file="test.ps") > print(plot) > dev.off() > > png(file="test.png") > print(plot) > dev.off() > > tiff(file="test.tiff") > print(plot) > dev.off() > > -- > Dimitri Liakhovitski > MarketTools, Inc. > Dimitri.Liakhovitski at markettools.com > > ______________________________________________ > R-help at 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. >
You might want to look at the dev.copy function. I just tried with your example and the postscript device, used the print(plot) command with the postscript device open, then switched to the windows graphics device and did dev.copy then closed the postscript file, the result had 2 pages, the first like you described looked different from the screen, the 2nd (from dev.copy) had the same colors and general appearance (I did not compare super closely) as the plot on the screen. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Dimitri Liakhovitski > Sent: Monday, February 16, 2009 10:14 AM > To: R-Help List > Subject: [R] Printing out a graph using different graphics devices > > Hello, everyone! > The code below allows me to produce the graph I want (I know - the > colors are strange, but it's just for the sake of an example). > After you run the plot<- part and then do print(plot) - that's what I > want. > However, when I run the bits of code below (with graphics devices) - > what they print is different from the original plot. In .png, .emf, > and .tiff - my dots change their shape and my lines (I think) become > thinner. And in .ps format - the dots stay what they are supposed to > be but they change their color. > Could you please explain to me what the reasons are for those changes? > Also - should I specify my chart characteristics differently depending > on the device I'll be using? > Thank you very much! > Dimitri > > library(lattice) > d=data.frame(xx=c(2.2,2.1,3.3),yy=c(0.1,0.2,0.3),zz=c(2.5,2.0,1.8)) > d[[2]]<-as.factor(as.numeric((d[[2]]))) > > trellis.par.set(superpose.line = list(col=c("green","red"), lwd = 2), > superpose.symbol = list(col=c("yellow","blue"),cex > 1.3, pch = 20), > reference.line = list(col = "gray", lty ="dotted")) > > plot<-dotplot(c(d[[1]],d[[3]])~rep(d[[2]],2), > groups=rep(c("Group 1","Group 2"), each=nrow(d)), > main=list("Chart Title",cex=1), > > type="b", > auto.key = list(space = "top", points = TRUE, lines = TRUE), > > xlab=list("Title for X",cex=.9,font=2), > ylab=list("Title for Y",cex=.9,font=2), > > panel = function(y,x,...) { > panel.grid(h = -1, v = -1) > panel.xyplot(x, y, ...) > > ltext(x, y, labels=round(y,3), > cex=.8,col="black",font=2, > adj=c(-0.2,1)) > > }) > print(plot) > > win.metafile(file="test.emf") > print(plot) > dev.off() > > postscript(file="test.ps") > print(plot) > dev.off() > > png(file="test.png") > print(plot) > dev.off() > > tiff(file="test.tiff") > print(plot) > dev.off() > > -- > Dimitri Liakhovitski > MarketTools, Inc. > Dimitri.Liakhovitski at markettools.com > > ______________________________________________ > R-help at 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.
Maybe Matching Threads
- changing settings on a barchart (lattice)
- Putting 6 graphs on one page
- Barchart in lattice - wrong order of groups, data labels on top of each other, and a legend question
- Producing customized tickmarks when producing a graph using "curve"
- Question about multiple regression