[R] plot multiple data sets on same axis

Scott Lamb slamb at slamb.org
Mon Dec 31 02:26:28 CET 2007

Thank you - that was exactly what I needed. I just discovered read.csv
understands URLs, so here it is with my actual data and formatting:

    df <- read.csv("http://www.slamb.org/tmp/one-active.csv")
    split.df <- split(df, df$method)
    plot(0, xlim=range(df$inactive), ylim=range(df$elapsed), type="n",
         ylab="time (µs)", xlab="inactive file descriptors", log="y",
         main="1 active descriptor, 1 write", bty="l")
    for (i in seq_along(split.df)) {
        lines(split.df[[i]]$inactive, split.df[[i]]$elapsed, col=i)
    legend("topleft", legend=labels(split.df),
           fill=seq_along(split.df), bty="n")

Next question. Rather than plot each point, I'd like to use boxplots to
show the interquartile range for each x.

I've tried replacing the for loop body with this:

    this_method <- split.df[[i]]
    boxplot(elapsed~inactive, data=this_method,
            add=TRUE, border=i, boxfill=i, outline=FALSE)

but it has two problems:

* it doesn't plot at the correct x values. It looks like I need to
supply a list of the x values as the "at" parameter, and I don't know
how to get unique values from this_method$inactive. (I tried the clunky
labels(split(this_method, this_method$inactive)), but it returns them as

* it redraws the graph's frame, and it ignores bty="l" when doing so.

Not sure how to correct either.


Scott Lamb <http://www.slamb.org/>

More information about the R-help mailing list