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]]
Perhaps plot(mtcars$wt,mtcars$mpg,xlab= "Weight(lbs/1000)", ylab="Miles per Gallon", pch=c(3,17,19), col=c("blue", "red", "purple4")) On 01/10/2007, Letticia Ramlal <lramlal@claflin.edu> wrote:> > 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@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
On Mon, 1 Oct 2007, Letticia Ramlal wrote:> 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.WHAT "does not work" ??? points() puts points on a plot with colors and symbols as you specify them. example(points) illustrates this with several different setups. It even shows how to construct "red symbols with a yellow interior (where available)"!! Read the help page, run and study the example()'s 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
You might find it easier to use the ggplot2 (http://had.co.nz/ggplot2) package: install.packages("ggplot2") library(ggplot2) qplot(mpg, wt, data=mtcars, colour=factor(cyl)) # or qplot(mpg, wt, data=mtcars, shape=factor(cyl)) ggplot2 takes care of mapping the variable values to colours and sizes, and produces a legend for you. You can tweak the mapping using different scales, see, for example, http://had.co.nz/ggplot2/scale_hue.html. Hadley On 10/1/07, Letticia Ramlal <lramlal at claflin.edu> wrote:> 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. >-- http://had.co.nz/
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. >