Skip to content

how do i put two scatterplots on same graph

6 messages · jricci, Daniel Malter, Paul Hiemstra +2 more

#
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.
#
On 10/04/2011 06:19 AM, jricci wrote:
Hi,

You could take a look at the ggplot2 package.

good luck,
Paul
#
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")[iris$Species],pch=c(16:18)[iris$Species])

Bill
On Oct 4, 2011, at 4:20 AM, Paul Hiemstra wrote:

            
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
#
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