[R] two scatter plots in one

baptiste auguie ba208 at exeter.ac.uk
Thu Feb 12 10:47:53 CET 2009


lattice and ggplot2 also offer a general way of doing this,

> # first create a data.frame in the long format containing the two  
> data sets
> x1 <- seq(-10, 10)
> x2 <- seq(-8, 12)
> y1 <- sin(x1/3)
> y2 <- cos(x2/2)
>
> d1 <- data.frame(x=x1, y=y1, var="1")
> d2 <- data.frame(x=x2, y=y2, var="2")
>
> library(reshape)
> d <- melt(merge(d1, d2, all=T), id=c("x", "var"))
>
>  # here goes the plotting in a high-level perspective
>
> library(ggplot2)
>
> qplot(x, value, data=d, geom=c("line","point"), colour=var)
>
> or,
>
> library(lattice)
>
> xyplot(value ~ x, data=d, type="b", groups = var)


On 12 Feb 2009, at 09:09, Jim Lemon wrote:

> liujb wrote:
>> Dear R users,
>>
>> I need to compare two scatter plots,
>> plot(x1, y1)
>> plot(x2, y2)
>>
>> and would like to plot them in the same figure. How do I do it?
>>
>>
> Hi liujb,
> How about this:
>
> plot(x1,y1,xlim=range(c(x1,x2)),ylim=range(c(y1,y2)),col="red")
> points(x2,y2,col="blue")
>
>
> Jim
>
> ______________________________________________
> 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.

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag




More information about the R-help mailing list