[R] Plotting multiple vectors in one window?

Joshua Wiley jwiley.psych at gmail.com
Tue Aug 30 10:33:45 CEST 2011


Hi Caitlin,

I would probably convert the data to a long format so bsa, unknown1,
unknown2 are all in one variable and ditto for abs.  Then control the
plotting symbol using the variable indicator.  abline() will just keep
adding to the existing plot, so you could add all three lines of best
fit using it with three separate calls.  If you wanted to plot them
staying in wide format, try using ?points

## example data (it is polite for the person asking for help to sample
data (real or made up))
dat <- as.data.frame(matrix(rnorm(60), ncol = 6))
colnames(dat) <- c("bsa", "unknown1", "unknown2", "abs", "abs2", "abs3")

## using a wide dataset. Note the use of with() to avoid retyping dat
over and over
## each time I use reference one of the variables
with(dat, {
plot(bsa, abs, col='blue', pch=16, xlim=c(-3, 3), ylim=c(-3, 3), xlab =
expression(paste(BSA, " (", mu, "g)", sep="")), ylab=expression(A[595]))

points(unknown1, abs2, col='blue', pch=17)
points(unknown2, abs3, col='blue', pch=18)

abline(lm(abs ~ bsa))
abline(lm(abs2 ~ unknown1))
abline(lm(abs3 ~ unknown2))

title(main="Quantifying Protein Content via Bradford Microassay", col='blue')
})

## A solution using long format with ggplot2 for graphing
require(ggplot2)
datlong <- reshape(dat, varying = list(1:3, 4:6), v.names = c("bsa",
"abs"), direction = "long")
datlong$time <- factor(datlong$time)
head(datlong) ## look at the first few rows

ggplot(data = datlong, mapping = aes(x = bsa, y = abs, group = time)) +
  stat_smooth(aes(linetype = time), size = 1, method = "lm", se=FALSE) +
  geom_point(aes(shape = time))

Cheers,

Josh

On Mon, Aug 29, 2011 at 9:53 PM, Caitlin <bioprogrammer at gmail.com> wrote:
> Hi all.
>
> Using the following code:
>
> plot(bsa, abs, col='blue', pch=16, xlim=c(0, 25), ylim=c(0.0, 1.0), xlab =
> expression(paste(BSA, " (", mu, "g)", sep="")), ylab=expression(A[595]))
>
> plot(unknown1, abs2, col='blue', pch=16, xlim=c(0, 25), ylim=c(0.0, 1.0),
> xlab = expression(paste(Unknown_1, " (", mu, "g)", sep="")),
> ylab=expression(A[595]))
>
> plot(unknown2, abs3, col='blue', pch=16, xlim=c(0, 25), ylim=c(0.0, 1.0),
> xlab = expression(paste(Unknown_2, " (", mu, "g)", sep="")),
> ylab=expression(A[595]))
>
>
>  myline.fit <- lm(abs ~ bsa)
> summary(myline.fit)
> abline(myline.fit)
>
> axis(side=1, col="gray", tck=1, lty="dotted")
> axis(side=2, col="gray", tck=1, lty="dotted")
> box()
> minor.tick(nx=2, ny=2, tick.ratio=0.5)
> title(main="Quantifying Protein Content via Bradford Microassay",
> col='blue')
>
> ...I have constructed three individual plots. Is there a convenient way to
> plot three vectors (not from a file) into one window? With three individual
> lines of best fit? Probably using three symbols, e.g., pch=16, 17, 18, to
> distinguish them.
>
> Thanks,
>
> ~Caitlin
>
>        [[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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, ATS Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list