[R] how do i put two scatterplots on same graph
Nordlund, Dan (DSHS/RDA)
NordlDJ at dshs.wa.gov
Wed Oct 5 01:05:57 CEST 2011
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of jricci
> Sent: Tuesday, October 04, 2011 1:28 PM
> To: r-help at r-project.org
> Subject: Re: [R] how do i put two scatterplots on same graph
>
> I am new at this. The two data sets don't have color variable just
> paired data. How should I structure the data sets in R?
> Joe Ricci
>
> ________________________________
> From: William Revelle [via R] <ml-
> node+s789695n3871355h20 at n4.nabble.com>
> To: Joe Ricci
> Sent: Tue Oct 04 11:18:20 2011
> Subject: Re: how do i put two scatterplots on same graph
>
>
>
> If the data are from one data.frame (e.g., the iris data set), then
> simply label the red and white flowers with different colors:
> e.g.,
>
> with the iris data set
>
> plot(iris$Sepal.Length,iris$Sepal.Width,col=c("red","blue","black")[iri
> s$Species],pch=c(16:18)[iris$Species])
>
> Bill
>
>
>
>
> On Oct 4, 2011, at 4:20 AM, Paul Hiemstra wrote:
>
> > On 10/04/2011 06:19 AM, jricci wrote:
> >> Have two sets of scatterplot data
> >> hypothetically
> >> a) stem lenght vs number of petals in red flowers
> >> b) stem lenght vs number of petals in white flowers
> >>
> >> want to place on same scatter plot with same x,y axis but different
> collored
> >> markers
> >>
> >> How do I do this in R
> >>
> >> --
> >> View this message in context: http://r.789695.n4.nabble.com/how-do-
> i-put-two-scatterplots-on-same-graph-tp3870030p3870030.html
> >> Sent from the R help mailing list archive at Nabble.com.
> >>
> >> ______________________________________________
> >> [hidden email]</user/SendEmail.jtp?type=node&node=3871355&i=0>
> 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.
> >
> > Hi,
> >
> > You could take a look at the ggplot2 package.
> >
> > good luck,
> > Paul
> >
> > --
> > Paul Hiemstra, Ph.D.
> > Global Climate Division
> > Royal Netherlands Meteorological Institute (KNMI)
> > Wilhelminalaan 10 | 3732 GK | De Bilt | Kamer B 3.39
> > P.O. Box 201 | 3730 AE | De Bilt
> > tel: +31 30 2206 494
> >
> > http://intamap.geo.uu.nl/~paul
> > http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770
> >
> > ______________________________________________
> > [hidden email]</user/SendEmail.jtp?type=node&node=3871355&i=1>
> 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.
> >
>
> William Revelle http://personality-project.org/revelle.html
> Professor http://personality-project.org
> Department of Psychology http://www.wcas.northwestern.edu/psych/
> Northwestern University http://www.northwestern.edu/
> Use R for psychology http://personality-project.org/r
> It is 6 minutes to midnight http://www.thebulletin.org
>
> ______________________________________________
> [hidden email]</user/SendEmail.jtp?type=node&node=3871355&i=2> 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.
>
>
This where it is important to follow the posting (see note immediately above about self-contained examples). But if you have two data frames, you can plot one and then use the points() function to plot the data from the other on the same graph. Something like
##create some data
red_flowers <- data.frame(stem.len=sample(7:15,25,replace=TRUE), num.petals=sample(35:55,25,replace=TRUE))
white_flowers <- data.frame(stem.len=sample(5:12,25,replace=TRUE), num.petals=sample(45:85,25,replace=TRUE))
##plot the red flowers
plot(red_flowers$stem.len, red_flowers$num.petals, col='red', xlim=c(5,15), ylim=c(35,85))
##use points() to plot the white flowers
points(white_flowers$stem.len, white_flowers$num.petals)
You will need to make sure you set the x and y axis limits so as not to truncate values in either data frame.
Hope this is helpful,
Dan
Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
More information about the R-help
mailing list