[R] how to plot a graph with different pch

hadley wickham h.wickham at gmail.com
Mon Oct 1 21:43:23 CEST 2007


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/



More information about the R-help mailing list