[R] how to plot a graph with different pch

Greg Snow Greg.Snow at intermountainmail.org
Tue Oct 2 20:09:31 CEST 2007


I don't think your code is doing what you think it is doing.  Try
something like:

with(mtcars, plot(wt, mpg, xlab= "Weight(lbs/1000)", ylab="Miles per
Gallon",
	col= c('','','','blue','','red','','purple')[cyl],
      pch= c(0,0,0,3,0,17,0,19)[cyl]))

legend(4,30,c("4 cylinder","6 cylinder","8 cylinder"),pch=c(3,17,19),
cex=1.2, col=c("blue", "red", "purple4"))



-- 
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 Letticia Ramlal
> Sent: Monday, October 01, 2007 12:34 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] how to plot a graph with different pch
> 
> I am trying to plot a graph but the points on the graph 
> should be different symbols and colors. It should represent 
> what is in the legend.
> I tried using the points command but this does not work. Is 
> there another command in R that would allow me to use 
> different symbols and colors for the points?
> 
> Thank you kindly.
> 
>  
> 
>  
> 
> data(mtcars)
> 
> plot(mtcars$wt,mtcars$mpg,xlab= "Weight(lbs/1000)", ylab="Miles per
> Gallon")
> 
> mtcars$wt.cyl<-mtcars$wt[mtcars$cyl]
> 
> mtcars$mpg.cyl<-mtcars$mpg[mtcars$cyl]
> 
> points(mtcars$wt.cyl, mtcars$mpg.cyl, pch = 17, col = "red")
> 
> title("Motor Trend Data")
> 
> text(2,10,"LTR",cex=1.2,adj=0,col=3)
> 
> legend(4,30,c("4 cylinder","6 cylinder","8 
> cylinder"),pch=c(3,17,19), cex=1.2, col=c("blue", "red", "purple4"))
> 
>  
> 
>  
> 
> LTR
> 
>  
> 
>  
> 
> 
> 	[[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