[R] Using windows() and jpeg()

Greg Snow Greg.Snow at intermountainmail.org
Mon Nov 19 23:33:39 CET 2007


Using points and pch only allows the plotting of single letters.  If you
want to plot strings of more than 1 letter use the "text" function in
place of points.

To get text in circles you can either plot the text and the circles in
separate steps, or another possibility (maybe overkill, but lets you set
up code that works for you then reuse it easily) is the my.symbols
function in the TeachingDemos package.  Here is a quick demo:

library(TeachingDemos)

ms.Rx <- function(txt,...){
	text(0,0,txt)
	points(0,0,cex=4)
}

x <- runif(10)
y <- rnorm(10)

my.txt <- sample( c('Rx','Sx','HV','Er'), 10, replace=TRUE)

my.symbols(x,y,ms.Rx, add=FALSE, symb.plots=TRUE, txt=my.txt)


Hope this helps,


-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at intermountainmail.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 Irina Burmenko
> Sent: Monday, November 19, 2007 3:06 PM
> To: Duncan Murdoch
> Cc: r-help at r-project.org
> Subject: Re: [R] Using windows() and jpeg()
> 
> Thank you very much for your help, Duncan.
> 
> I do have another question, unrelated to the first one.  I 
> need to plot points on the graph, but I want points to be 
> letters instead of symbols.  Ideally, I would like to have 
> circles with letters 'Rx' inside, for example.  Is that 
> possible?  If not, is there a way to simply have Rx as a 
> symbol without the circle?  When I tried to do that, only the 
> first letter 'R' gets displayed.  Same thing with the legend. 
>  Here is the sample code.  Any help would be greatly appreciated.
> 
> x<-c(1:7)
> y1 <- c(20,NA,NA,20,20,NA,20)
> y2 <- c(40,40,NA,NA,40,40,NA)
> y3 <- c(NA,60,60,NA,NA,60,60)
> y4 <- c(80,NA,NA,80,80,NA,80)
> windows(width=6.25, height = 3.75)
> par(pin=c(3.5,1.5),bty="l")
> par(mar=c(2.5,4.1,2.2,10.5))
> plot(x,y4,type="n",xlim=c(1,7),ylim=c(0,100),xlab="",xaxt="n")
> axis(1, at=1:7, labels = FALSE, tcl = -0.2) mtext("Week 
> of",side=1, cex = 0.8,line=1.0)
>  
> points(x,y1,pch="HV",bg="blue",col="blue",cex=0.5)
> points(x,y2,pch="ER",bg="orange",col="orange",cex=0.5)
> points(x,y3,pch="Rx",bg="red",col="red",cex=0.5)
> points(x,y4,pch="Sx",bg="green",col="green",cex=0.5)
> par(xpd=TRUE)
> legend(8,40, c("hospital visit", "ER visit","medication 
> change", "symptoms reported"), col = 
> c("blue","orange","red","green"), text.col= "black", lty = 
> c(-1, -1, -1,-1), pch = c("H","E","R","S"), bty="n",cex=0.85)
> 
> Irina
> 
> 
> ----- Original Message ----
> From: Duncan Murdoch <murdoch at stats.uwo.ca>
> To: Irina Burmenko <iburmen at yahoo.com>
> Cc: r-help at r-project.org
> Sent: Monday, November 19, 2007 4:54:13 PM
> Subject: Re: [R] Using windows() and jpeg()
> 
> On 19/11/2007 3:09 PM, Irina Burmenko wrote:
> > Hello,
> > 
> > I have the following question, which I haven't been able to 
> resolve after days of trying.  I used to save my plots as 
> jpegs using the savePlot command.  However, that seems to 
> result in lost resolution.  So now I'm trying to use the 
> jpeg( ) function, but am having trouble because it seems to 
> be incompatible with the windows (width=, height=) command.   
> It's important for me to be able to specify the window size 
> as well as other parameters.  Here is the sample code:
> > 
> > jpeg(filename="myplot.jpg",quality=100)
> > windows(width=6.25, height = 3.75)
> 
> The jpeg() command should *replace* the windows() command.  
> They both open new graphics devices.  One problem you'll have 
> is that jpeg takes dimensions in pixels by default; if you 
> want to express it in inches, you need to say so, and also 
> say what resolution you're assuming.  For example,
> 
> jpeg(filename="myplot.jpg",quality=100,width=6.25, height = 3.75,
> units="in",res=96)
> 
> for 96 dpi resolution.
> 
> Duncan Murdoch
> 
> 
> > par(pin=c(3.5,1.5),bty="l")
> > par(mar=c(2.5,4.1,2.2,10.5))
> > x<-c(1,2,3)
> > y<-c(4,5,6)
> > plot(x,y,"b")
> > dev.off()
> > 
> > Using these commands results in an empty plot being saved.  
> However, skipping lines 2-4 saves the graph, but, of course, 
> without the formatting, which is important to me.  Any help 
> would be greatly appreciated.
> > 
> > Irina
> > 
> > 
> >      
> > 
> ______________________________________________________________________
> > ______________ Never miss a thing.  Make Yahoo your home page.
> > 
> >     [[alternative HTML version deleted]]
> > 
> > ______________________________________________
> > 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.
> 
> 
>       
> ______________________________________________________________
> ______________________
> Never miss a thing.  Make Yahoo your home page. 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.
> 



More information about the R-help mailing list