[R] ScatterPlot

Kingsford Jones kingsfordjones at gmail.com
Wed Oct 21 02:08:33 CEST 2009


Here's an example:

x <- runif(100)
sex <- sample(0:1, 100, replace = TRUE)
y <- sex + 2*x + x*sex + rnorm(100)
dfr <- data.frame(y, x, sex)
f1 <- lm(y ~ x, data = dfr, subset = sex == 0)
f2 <- lm(y ~ x, data = dfr, subset = sex == 1)
plot(x, y, pch = sex+1)
abline(f1, col = 2, lty = 2)
abline(f2, col = 3, lty = 3)
legend('topleft', c('male', 'female'), lty = 2:3, col = 2:3)

For some fancier plots see the ggplot2 examples here:

http://had.co.nz/ggplot2/stat_smooth.html

hth,

Kingsford Jones



On Tue, Oct 20, 2009 at 1:20 PM, Marsha Melnyk <mmelnyk at stevens.edu> wrote:
> I am trying to make a scatterplot with containing three columns.  I
> know how to plot the two columns but now the third column consists of M
> or F (male or female) and I don't know how to separate the data so I
> can make two separate regression lines on the same graph.
>
> meta   #name of file
> plot(meta$mass, meta$rate, xlab="mass", ylab="rate")
>
> I can make the regression line with all the data but I just don't know
> how to split everything up.
>
> abline(lm(rate ~ mass, data=meta), col="red")
>
>
>
> Thanks,
> Marsha
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list